forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
67 lines (60 loc) · 1014 Bytes
/
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
package rueidis
import "sync"
func newPool(cap int, dead wire, makeFn func() wire) *pool {
if cap <= 0 {
cap = DefaultPoolSize
}
return &pool{
size: 0,
dead: dead,
make: makeFn,
list: make([]wire, 0, cap),
cond: sync.NewCond(&sync.Mutex{}),
}
}
type pool struct {
dead wire
cond *sync.Cond
make func() wire
list []wire
size int
down bool
}
func (p *pool) Acquire() (v wire) {
p.cond.L.Lock()
for len(p.list) == 0 && p.size == cap(p.list) && !p.down {
p.cond.Wait()
}
if p.down {
v = p.dead
} else if len(p.list) == 0 {
p.size++
v = p.make()
} else {
i := len(p.list) - 1
v = p.list[i]
p.list = p.list[:i]
}
p.cond.L.Unlock()
return v
}
func (p *pool) Store(v wire) {
p.cond.L.Lock()
if !p.down && v.Error() == nil {
p.list = append(p.list, v)
} else {
p.size--
v.Close()
}
p.cond.L.Unlock()
p.cond.Signal()
}
func (p *pool) Close() {
p.cond.L.Lock()
p.down = true
for _, w := range p.list {
w.Close()
}
p.cond.L.Unlock()
p.cond.Broadcast()
}