-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
68 lines (61 loc) · 1.4 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
package etcdcrypto
import (
"sync"
"github.com/coreos/etcd/clientv3"
"golang.org/x/net/context"
)
type watcherCipher struct {
clientv3.Watcher
Cipher
wg sync.WaitGroup
stopc chan struct{}
stopOnce sync.Once
}
func NewWatcher(w clientv3.Watcher, cipher Cipher) clientv3.Watcher {
return &watcherCipher{Watcher: w, Cipher: cipher, stopc: make(chan struct{})}
}
func (w *watcherCipher) Watch(ctx context.Context, key string, opts ...clientv3.OpOption) clientv3.WatchChan {
wch := w.Watcher.Watch(ctx, key, opts...)
// translate watch events from prefixed to unprefixed
plaintextWch := make(chan clientv3.WatchResponse)
w.wg.Add(1)
go func() {
defer func() {
close(plaintextWch)
w.wg.Done()
}()
for wr := range wch {
for i := range wr.Events {
kv := wr.Events[i].Kv
if len(kv.Value) > 0 {
v, err := w.Decrypt(kv.Value)
if err != nil {
panic(err)
}
kv.Value = v
}
if wr.Events[i].PrevKv != nil && len(wr.Events[i].PrevKv.Value) > 0 {
v, err := w.Decrypt(wr.Events[i].PrevKv.Value)
if err != nil {
panic(err)
}
wr.Events[i].PrevKv.Value = v
}
}
select {
case plaintextWch <- wr:
case <-ctx.Done():
return
case <-w.stopc:
return
}
}
}()
return plaintextWch
}
func (w *watcherCipher) Close() error {
err := w.Watcher.Close()
w.stopOnce.Do(func() { close(w.stopc) })
w.wg.Wait()
return err
}