-
Notifications
You must be signed in to change notification settings - Fork 2
/
codis.go
139 lines (120 loc) · 3.56 KB
/
codis.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
package codis
import (
"time"
"sync"
"errors"
"encoding/json"
"github.com/samuel/go-zookeeper/zk"
"github.com/garyburd/redigo/redis"
"github.com/op/go-logging"
)
type proxyInfo struct {
Proto_type string
Proxy_addr string
}
type Pool struct {
nextIdx int
pools []redis.Conn
zk zk.Conn
ZkServers []string
ZkTimeout time.Duration
ZkDir string
Dial func(network, address string) (redis.Conn, error)
}
var (
mu = &sync.Mutex{}
Log *logging.Logger = logging.MustGetLogger("Codis")
)
func (this *Pool) initFromZk() {
this.initZk()
this.pools = []redis.Conn{}
children, _, err := this.zk.Children(this.ZkDir)
if err != nil {
Log.Fatal(err)
}
for _, child := range children {
data, _, err := this.zk.Get(this.ZkDir + "/" + child)
if err != nil {
continue
}
var p proxyInfo
json.Unmarshal(data, &p)
conn, err := this.Dial(p.Proto_type, p.Proxy_addr)
if err != nil {
Log.Errorf("Create redis connection failed: %s", err.Error())
continue
}
this.pools = append(this.pools, conn)
}
go this.watch(this.ZkDir)
}
func (this *Pool) watch(node string) {
for {
_, _, ch, err := this.zk.ChildrenW(node)
if err != nil {
Log.Error(err)
return
}
evt := <-ch
if evt.Type == zk.EventSession {
if evt.State == zk.StateConnecting {
continue
}
if evt.State == zk.StateExpired {
this.zk.Close()
Log.Info("Zookeeper session expired, reconnecting...")
this.initZk()
}
}
if evt.State == zk.StateConnected {
switch evt.Type {
case
zk.EventNodeCreated,
zk.EventNodeDeleted,
zk.EventNodeChildrenChanged,
zk.EventNodeDataChanged:
this.initFromZk()
return
}
continue
}
}
}
func (this *Pool) initZk() {
zkConn, _, err := zk.Connect(this.ZkServers, this.ZkTimeout)
if err != nil {
Log.Fatalf("Failed to connect to zookeeper: %+v", err)
}
this.zk = *zkConn
}
func (this *Pool) Get() redis.Conn {
mu.Lock()
if len(this.pools) == 0 {
this.initFromZk()
}
this.nextIdx += 1
if this.nextIdx >= len(this.pools) {
this.nextIdx = 0
}
if len(this.pools) == 0 {
mu.Unlock()
err := errors.New("Proxy list empty")
Log.Error(err)
return errorConnection{err: err}
} else {
c := this.pools[this.nextIdx]
mu.Unlock()
return c
}
}
func (this *Pool) Close() {
this.zk.Close()
this.pools = []redis.Conn{}
}
type errorConnection struct{ err error }
func (ec errorConnection) Do(string, ...interface{}) (interface{}, error) { return nil, ec.err }
func (ec errorConnection) Send(string, ...interface{}) error { return ec.err }
func (ec errorConnection) Err() error { return ec.err }
func (ec errorConnection) Close() error { return ec.err }
func (ec errorConnection) Flush() error { return ec.err }
func (ec errorConnection) Receive() (interface{}, error) { return nil, ec.err }