-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_robin_pool.go
172 lines (158 loc) · 4.62 KB
/
round_robin_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package godis
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"github.com/leyantech/godis/internal"
"github.com/pkg/errors"
"github.com/samuel/go-zookeeper/zk"
"log"
"sort"
"sync/atomic"
)
// RoundRobinPool is a round-robin redis client pool for connecting multiple codis proxies based on
// zookeeper-go and redis-go.
type RoundRobinPool struct {
zkConn *zk.Conn
zkProxyDir string
pools atomic.Value
childCh <-chan zk.Event
childrenData atomic.Value
options redis.Options
nextIdx int64
}
// NewRoundRobinPool return a round-robin redis client pool specified by
// zk client and redis options.
func NewRoundRobinPool(zkConn *zk.Conn, zkProxyDir string, options redis.Options) (*RoundRobinPool, error) {
pool := &RoundRobinPool{
zkConn: zkConn,
zkProxyDir: zkProxyDir,
nextIdx: -1,
pools: atomic.Value{},
}
pool.pools.Store([]*internal.PooledObject{})
_, _, childCh, err := zkConn.ChildrenW(zkProxyDir)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to watch %s", zkProxyDir))
}
pool.childCh = childCh
pool.resetPools()
go pool.watch()
return pool, nil
}
func (p *RoundRobinPool) resetPools() {
children, _, err := p.zkConn.Children(p.zkProxyDir)
if err != nil {
log.Printf("Failed to get children from %s, %v", p.zkProxyDir, err)
return
}
childrenData := make([]string, 0)
for _, child := range children {
data, _, err := p.zkConn.Get(p.zkProxyDir + "/" + child)
if err != nil {
log.Printf("Failed to get children data from %s, %v", p.zkProxyDir+"/"+child, err)
continue
}
childrenData = append(childrenData, (string)(data))
}
sort.Strings(childrenData)
pools := p.pools.Load().([]*internal.PooledObject)
addr2Pool := make(map[string]*internal.PooledObject, len(pools))
for _, pool := range pools {
addr2Pool[pool.Addr] = pool
}
newPools := make([]*internal.PooledObject, 0)
for _, childData := range childrenData {
proxyInfo := internal.ProxyInfo{}
err := json.Unmarshal([]byte(childData), &proxyInfo)
if err != nil {
log.Printf("Parse %s failed", childData)
continue
}
if proxyInfo.State != "online" {
continue
}
addr := proxyInfo.Addr
if pooledObject, ok := addr2Pool[addr]; ok {
newPools = append(newPools, pooledObject)
delete(addr2Pool, addr)
} else {
options := p.cloneOptions()
options.Addr = addr
options.Network = "tcp"
pooledObject := internal.NewPooledObject(
addr,
redis.NewClient(&options),
)
newPools = append(newPools, pooledObject)
log.Printf("Add new proxy: %s", addr)
}
}
p.pools.Store(newPools)
for _, pooledObject := range addr2Pool {
log.Printf("Remove proxy: %s", pooledObject.Addr)
pooledObject.Client.Close()
}
}
// GetClient can get a redis client from pool with round-robin policy.
// It's safe for concurrent use by multiple goroutines.
func (p *RoundRobinPool) GetClient() *redis.Client {
pools := p.pools.Load().([]*internal.PooledObject)
for {
current := atomic.LoadInt64(&p.nextIdx)
var next int64
if (current) >= (int64)(len(pools))-1 {
next = 0
} else {
next = current + 1
}
if atomic.CompareAndSwapInt64(&p.nextIdx, current, next) {
return pools[next].Client
}
}
}
func (p *RoundRobinPool) watch() {
for {
select {
case event := <-p.childCh:
if event.Path != p.zkProxyDir {
continue
}
log.Printf("Receive child event: type=%s, path=%s, state=%s, err=%v\n",
event.Type.String(), event.Path, event.State, event.Err)
if event.Type == zk.EventNodeChildrenChanged {
p.resetPools()
_, _, p.childCh, _ = p.zkConn.ChildrenW(p.zkProxyDir)
}
}
}
}
func (p *RoundRobinPool) cloneOptions() redis.Options {
options := redis.Options{
Network: p.options.Network,
Addr: p.options.Addr,
Dialer: p.options.Dialer,
OnConnect: p.options.OnConnect,
Password: p.options.Password,
DB: p.options.DB,
MaxRetries: p.options.MaxRetries,
MinRetryBackoff: p.options.MinRetryBackoff,
MaxRetryBackoff: p.options.MaxRetryBackoff,
DialTimeout: p.options.DialTimeout,
ReadTimeout: p.options.ReadTimeout,
WriteTimeout: p.options.WriteTimeout,
PoolSize: p.options.PoolSize,
PoolTimeout: p.options.PoolTimeout,
IdleTimeout: p.options.IdleTimeout,
IdleCheckFrequency: p.options.IdleCheckFrequency,
TLSConfig: p.options.TLSConfig,
}
return options
}
// Close closes the pool, releasing all resources except zookeeper client.
func (p *RoundRobinPool) Close() {
pools := p.pools.Load().([]*internal.PooledObject)
for _, pool := range pools {
pool.Client.Close()
}
}