-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpool.go
99 lines (88 loc) · 2.05 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package glua
type vmPool struct {
maxVmCount int
vmQueue []*luaVm
idleVmDic map[uintptr]*luaVm
inUseVmDic map[uintptr]*luaVm
}
func newVMPool(maxVmCount int) *vmPool {
if maxVmCount < 0 {
maxVmCount = 4
}
if maxVmCount > 16 {
maxVmCount = 16
}
return &vmPool{
maxVmCount: maxVmCount,
vmQueue: make([]*luaVm, 0),
inUseVmDic: make(map[uintptr]*luaVm),
idleVmDic: make(map[uintptr]*luaVm),
}
}
func (vp *vmPool) accquire() *luaVm {
defer func() {
metricGauge("glua_vm_idle_count", int64(len(vp.idleVmDic)), nil)
metricGauge("glua_vm_inuse_count", int64(len(vp.inUseVmDic)), nil)
}()
// check idle vm
if len(vp.vmQueue) > 0 {
vm := vp.vmQueue[0]
if len(vp.vmQueue) == 1 {
vp.vmQueue = []*luaVm{}
} else {
vp.vmQueue = vp.vmQueue[1:]
}
delete(vp.idleVmDic, vm.stateId)
vp.inUseVmDic[vm.stateId] = vm
return vm
}
// create new vm
if len(vp.inUseVmDic) == vp.maxVmCount {
return nil
}
vm := newLuaVm()
vp.inUseVmDic[vm.stateId] = vm
return vm
}
func (vp *vmPool) release(vm *luaVm) {
defer func() {
metricGauge("glua_vm_idle_count", int64(len(vp.idleVmDic)), nil)
metricGauge("glua_vm_inuse_count", int64(len(vp.inUseVmDic)), nil)
}()
delete(vp.inUseVmDic, vm.stateId)
if vm.needDestory && vm.resumeCount == 0 {
vm.destory()
} else {
vp.idleVmDic[vm.stateId] = vm
vp.vmQueue = append(vp.vmQueue, vm)
}
}
func (vp *vmPool) find(stateId uintptr) *luaVm {
defer func() {
metricGauge("glua_vm_idle_count", int64(len(vp.idleVmDic)), nil)
metricGauge("glua_vm_inuse_count", int64(len(vp.inUseVmDic)), nil)
}()
vm, ok := vp.idleVmDic[stateId]
if !ok {
return nil
}
vp.inUseVmDic[vm.stateId] = vm
delete(vp.idleVmDic, stateId)
index := 0
for index, _ = range vp.vmQueue {
if vp.vmQueue[index].stateId == vm.stateId {
break
}
}
switch {
case len(vp.vmQueue) == 1:
vp.vmQueue = []*luaVm{}
case index == len(vp.vmQueue)-1:
vp.vmQueue = vp.vmQueue[:index]
case index == 0:
vp.vmQueue = vp.vmQueue[1:]
default:
vp.vmQueue = append(vp.vmQueue[:index], vp.vmQueue[index+1:]...)
}
return vm
}