forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
220 lines (190 loc) · 4.43 KB
/
watcher.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package zookeeper
import (
"errors"
"path"
"github.com/micro/go-micro/registry"
"github.com/samuel/go-zookeeper/zk"
)
type zookeeperWatcher struct {
wo registry.WatchOptions
client *zk.Conn
stop chan bool
results chan result
}
type watchResponse struct {
event zk.Event
service *registry.Service
err error
}
type result struct {
res *registry.Result
err error
}
func newZookeeperWatcher(r *zookeeperRegistry, opts ...registry.WatchOption) (registry.Watcher, error) {
var wo registry.WatchOptions
for _, o := range opts {
o(&wo)
}
zw := &zookeeperWatcher{
wo: wo,
client: r.client,
stop: make(chan bool),
results: make(chan result),
}
go zw.watch()
return zw, nil
}
func (zw *zookeeperWatcher) watchDir(key string, respChan chan watchResponse) {
for {
// get current children for a key
children, _, childEventCh, err := zw.client.ChildrenW(key)
if err != nil {
respChan <- watchResponse{zk.Event{}, nil, err}
return
}
select {
case e := <-childEventCh:
if e.Type != zk.EventNodeChildrenChanged {
continue
}
newChildren, _, err := zw.client.Children(e.Path)
if err != nil {
respChan <- watchResponse{e, nil, err}
return
}
// a node was added -- watch the new node
for _, i := range newChildren {
if contains(children, i) {
continue
}
newNode := path.Join(e.Path, i)
if key == prefix {
// a new service was created under prefix
go zw.watchDir(newNode, respChan)
nodes, _, _ := zw.client.Children(newNode)
for _, node := range nodes {
n := path.Join(newNode, node)
go zw.watchKey(n, respChan)
s, _, err := zw.client.Get(n)
e.Type = zk.EventNodeCreated
srv, err := decode(s)
if err != nil {
continue
}
respChan <- watchResponse{e, srv, err}
}
} else {
go zw.watchKey(newNode, respChan)
s, _, err := zw.client.Get(newNode)
e.Type = zk.EventNodeCreated
srv, err := decode(s)
if err != nil {
continue
}
respChan <- watchResponse{e, srv, err}
}
}
case <-zw.stop:
// There is no way to stop GetW/ChildrenW so just quit
return
}
}
}
func (zw *zookeeperWatcher) watchKey(key string, respChan chan watchResponse) {
for {
s, _, keyEventCh, err := zw.client.GetW(key)
if err != nil {
respChan <- watchResponse{zk.Event{}, nil, err}
return
}
select {
case e := <-keyEventCh:
switch e.Type {
case zk.EventNodeDataChanged, zk.EventNodeCreated, zk.EventNodeDeleted:
if e.Type != zk.EventNodeDeleted {
// get the updated service
s, _, err = zw.client.Get(e.Path)
}
srv, err := decode(s)
if err != nil {
continue
}
respChan <- watchResponse{e, srv, err}
}
if e.Type == zk.EventNodeDeleted {
//The Node was deleted - stop watching
return
}
case <-zw.stop:
// There is no way to stop GetW/ChildrenW so just quit
return
}
}
}
func (zw *zookeeperWatcher) watch() {
watchPath := prefix
if len(zw.wo.Service) > 0 {
watchPath = servicePath(zw.wo.Service)
}
//get all Services
services, _, err := zw.client.Children(watchPath)
if err != nil {
zw.results <- result{nil, err}
}
respChan := make(chan watchResponse)
//watch the prefix for new child nodes
go zw.watchDir(watchPath, respChan)
//watch every service
for _, service := range services {
sPath := childPath(watchPath, service)
go zw.watchDir(sPath, respChan)
children, _, err := zw.client.Children(sPath)
if err != nil {
zw.results <- result{nil, err}
}
for _, c := range children {
go zw.watchKey(path.Join(sPath, c), respChan)
}
}
var service *registry.Service
var action string
for {
select {
case <-zw.stop:
return
case rsp := <-respChan:
if rsp.err != nil {
zw.results <- result{nil, err}
continue
}
switch rsp.event.Type {
case zk.EventNodeDataChanged:
action = "update"
service = rsp.service
case zk.EventNodeDeleted:
action = "delete"
service = rsp.service
case zk.EventNodeCreated:
action = "create"
service = rsp.service
}
}
zw.results <- result{®istry.Result{Action: action, Service: service}, nil}
}
}
func (zw *zookeeperWatcher) Stop() {
select {
case <-zw.stop:
return
default:
close(zw.stop)
}
}
func (zw *zookeeperWatcher) Next() (*registry.Result, error) {
select {
case <-zw.stop:
return nil, errors.New("watcher stopped")
case r := <-zw.results:
return r.res, r.err
}
}