/
watch.go
69 lines (61 loc) · 1.45 KB
/
watch.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
package keyencode
import (
"sync"
etcd "github.com/coreos/etcd/clientv3"
"golang.org/x/net/context"
)
type watcherKeyEncode struct {
etcd.Watcher
xfm KeyEncoder
wg sync.WaitGroup
stopc chan struct{}
stopOnce sync.Once
}
func NewWatcher(w etcd.Watcher, xfm KeyEncoder) etcd.Watcher {
return &watcherKeyEncode{Watcher: w, xfm: xfm, stopc: make(chan struct{})}
}
func (w *watcherKeyEncode) Watch(ctx context.Context, key string, opts ...etcd.OpOption) etcd.WatchChan {
encKey := w.xfm.Encode(key)
// TODO: handle ranges
wch := w.Watcher.Watch(ctx, encKey, opts...)
// translate watch events from prefixed to unprefixed
plaintextWch := make(chan etcd.WatchResponse)
w.wg.Add(1)
go func() {
defer func() {
close(plaintextWch)
w.wg.Done()
}()
for wr := range wch {
for i := range wr.Events {
if kv := wr.Events[i].Kv; len(kv.Key) > 0 {
v, err := w.xfm.Decode(string(kv.Key))
if err != nil {
panic(err)
}
kv.Key = []byte(v)
}
if wr.Events[i].PrevKv != nil && len(wr.Events[i].PrevKv.Key) > 0 {
v, err := w.xfm.Decode(string(wr.Events[i].PrevKv.Key))
if err != nil {
panic(err)
}
wr.Events[i].PrevKv.Key = []byte(v)
}
}
select {
case plaintextWch <- wr:
case <-ctx.Done():
return
case <-w.stopc:
return
}
}
}()
return plaintextWch
}
func (w *watcherKeyEncode) Close() error {
w.stopOnce.Do(func() { close(w.stopc) })
w.wg.Wait()
return nil
}