-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupstreamSessionPool.go
More file actions
68 lines (59 loc) · 1.3 KB
/
upstreamSessionPool.go
File metadata and controls
68 lines (59 loc) · 1.3 KB
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
package mgop
import (
"sync"
_"fmt"
)
type upstreamSessionPool interface {
appendSession(*sessionWrapper)
getBest() *sessionWrapper
foreach(func(sw *sessionWrapper), bool)
}
/**
one kind of upstream for polling, just for test
suppose support for insert,update and findAndModify is OK
*/
type pollingSessionPool struct {
wrappers []*sessionWrapper
maxSize int
next int
mutex sync.RWMutex
}
func (p *pollingSessionPool)getBest() *sessionWrapper {
p.mutex.Lock()
p.next = (p.next + 1) % len(p.wrappers)
cur := p.next
p.mutex.Unlock()
//fmt.Printf("get %d\n", cur)
p.wrappers[cur].atomicAcquire()
return p.wrappers[cur]
}
func (p *pollingSessionPool)foreach(eachFunc func(sw *sessionWrapper), readonly bool) {
if readonly {
p.mutex.RLock()
defer p.mutex.RUnlock()
} else {
p.mutex.Lock()
defer p.mutex.Unlock()
}
for _, s := range p.wrappers {
eachFunc(s)
}
}
func newPollingSessionPool(maxSize int) upstreamSessionPool {
p := &pollingSessionPool{
maxSize:maxSize,
next:-1,
}
p.wrappers = make([]*sessionWrapper, 0, maxSize)
return p
}
func (p *pollingSessionPool)appendSession(sw *sessionWrapper) {
p.mutex.Lock()
p.wrappers = append(p.wrappers, sw)
p.mutex.Unlock()
}
func (p *pollingSessionPool)size() int {
p.mutex.RLock()
p.mutex.RUnlock()
return len(p.wrappers)
}