-
Notifications
You must be signed in to change notification settings - Fork 7
/
pool.go
48 lines (42 loc) · 1.13 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
package cluster
import (
"github.com/gallir/smart-relayer/redis/radix.improved/pool"
"github.com/gallir/smart-relayer/redis/radix.improved/redis"
)
// clusterPool wraps the normal pool fairly transparently. The major change
// being made is that, once Empty is called, all Put calls on clusterPool will
// always close the connection, never put it back in the pool like in the normal
// Pool implementation.
//
// A side effect is that Empty may only be called once.
//
// This is all to prevent a race condition in cluster's Put method, since
// retrieving the pool and calling Put on it aren't synchronous in there, so the
// pool could potentially be cleaned up after it's gotten but before Put is
// called on it.
type clusterPool struct {
*pool.Pool
closeCh chan struct{}
}
func newClusterPool(p *pool.Pool) clusterPool {
return clusterPool{
Pool: p,
closeCh: make(chan struct{}),
}
}
func (cp clusterPool) Put(conn *redis.Client) {
select {
case <-cp.closeCh:
conn.Close()
default:
cp.Pool.Put(conn)
}
}
func (cp clusterPool) Empty() {
if cp.closeCh != nil {
close(cp.closeCh)
}
if cp.Pool != nil {
cp.Pool.Empty()
}
}