-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
57 lines (52 loc) · 1.03 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
package etcd
import (
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)
type watcher struct {
etcdConfig *etcdConfig
ch clientv3.WatchChan
closeChan chan struct{}
}
func newWatcher(s *etcdConfig) *watcher {
w := &watcher{
etcdConfig: s,
ch: nil,
closeChan: make(chan struct{}),
}
w.ch = s.client.Watch(s.ctx, s.options.root, clientv3.WithPrefix())
return w
}
func (w *watcher) Watcher() {
for {
select {
case <-w.etcdConfig.ctx.Done():
return
case <-w.closeChan:
return
case kv, ok := <-w.ch:
if !ok {
return
}
var storgeData []*mvccpb.KeyValue
var delData []*mvccpb.KeyValue
for _, v := range kv.Events {
if v.Type == mvccpb.DELETE {
delData = append(delData, v.Kv)
} else {
storgeData = append(storgeData, v.Kv)
}
}
if len(storgeData) > 0 {
w.etcdConfig.storge(storgeData)
}
if len(delData) > 0 {
w.etcdConfig.del(delData)
}
}
}
}
func (w *watcher) Close() {
close(w.closeChan)
close(w.etcdConfig.reloadChan)
}