forked from samuel/go-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
47 lines (41 loc) · 1.07 KB
/
subscribe.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
package zk
type WatchChange struct {
Ev Event
Children []string
}
type watchSub struct {
conn *Conn
path string
updates chan WatchChange
}
func (conn *Conn) ChildrenWSubscribe(path string) chan WatchChange {
s := &watchSub{
conn: conn,
path: path,
updates: make(chan WatchChange),
}
go s.loop()
return s.updates
}
func (s *watchSub) loop() {
children, _, refresh, err := s.conn.ChildrenW(s.path)
updates := s.updates
ev := Event{EventNodeChildrenChanged, StateSyncConnected, s.path, nil}
ev.Err = err
update := WatchChange{ev, children}
for {
select{
case <-refresh:
children, _, refresh, err = s.conn.ChildrenW(s.path)
if err == ErrConnectionClosed {
close(s.updates)
return
}
updates = s.updates
ev.Err = err
update = WatchChange{ev, children}
case updates <- update:
updates = nil
}
}
}