/
watch.go
148 lines (130 loc) · 3.66 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
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
package etcd
import (
"context"
"fmt"
"path"
"strings"
"time"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/client/v3"
)
// DirEvent dir event
type DirEvent struct {
Events []*clientv3.Event
Err error
Revision int64
}
// AddEvent add event
func (w *DirEvent) AddEvent(e *clientv3.Event) {
w.Events = append(w.Events, e)
}
// DebugInfo debug info
func (w *DirEvent) DebugInfo() string {
var buffer strings.Builder
var head = fmt.Sprintf("len event:%d, error:%+v, revision:%+v",
len(w.Events), w.Err, w.Revision)
buffer.WriteString(head)
buffer.WriteString(":")
for _, event := range w.Events {
buffer.WriteString("type:")
buffer.WriteString(event.Type.String())
buffer.WriteString("key:")
buffer.WriteString(string(event.Kv.Key))
buffer.WriteString(",")
buffer.WriteString("val:")
buffer.WriteString(string(event.Kv.Value))
}
return buffer.String()
}
// WatchDir watch dir
// ctx -- cancel controller, it's a parent context for watch
// nodePath -- path
// timeout -- get dir timeout
// ignoreEmpty -- if ignore, even get nothing from dir, watch still continue to work
// return first get dir content/dir watch event/current watch session cancel
func (c *Client) WatchDir(ctx context.Context,
nodePath string, timeout time.Duration, ignoreEmpty bool) (DirRet, <-chan DirEvent, context.CancelFunc) {
//get dir
var gCtx, cancel = context.WithTimeout(context.Background(), timeout)
defer cancel()
var dRet = c.GetDir(gCtx, nodePath, false)
if dRet.Err != nil {
// even node is empty, it's can still watch
var canIgnore = ignoreEmpty && IsNotFoundErr(dRet.Err)
if !canIgnore {
return dRet, nil, nil
}
}
var watchPath = path.Join(c.root, nodePath) + "/"
var outCtx, outCancel = context.WithCancel(ctx)
var watcher = c.eCli.Watch(clientv3.WithRequireLeader(outCtx),
watchPath, clientv3.WithPrefix())
if watcher == nil {
dRet.Err = genErr(watchPath, WatchFail)
return dRet, nil, outCancel
}
// Create the notifications channel, send updates to it.
var nChan = make(chan DirEvent, 1)
go func() {
// loop receive notify
dirLoop(outCtx, nChan, watcher, nodePath)
}()
return dRet, nChan, outCancel
}
// dir watch loop
func dirLoop(ctx context.Context,
notifyChan chan<- DirEvent, watcher clientv3.WatchChan, nodePath string) {
var err error
defer close(notifyChan)
for {
select {
case <-ctx.Done():
// This includes context cancellation errors.
notifyChan <- DirEvent{Err: convertErr(nodePath, ctx.Err())}
return
case wRsp, ok := <-watcher:
if !ok {
notifyChan <- DirEvent{Err: genErr(nodePath, WatchClosed)}
return
}
if wRsp.Canceled {
// Final notification.
err = wRsp.Err()
notifyChan <- DirEvent{Err: convertErr(nodePath, convertErr(nodePath, err))}
return
}
err = wRsp.Err()
if err != nil {
//watch error
notifyChan <- DirEvent{Err: convertErr(nodePath, err)}
}
if len(wRsp.Events) == 0 {
notifyChan <- DirEvent{Err: genErr(nodePath, WatchUnexpected)}
return
}
if procDirEvents(wRsp, nodePath, notifyChan) {
return
}
}
//loop for again
}
}
// proc dir events, if not put and delete, need exist current watch
// return true -- something wrong, need exit current session
// return false -- done current event work, move on
func procDirEvents(wRsp clientv3.WatchResponse, nodePath string, notifyChan chan<- DirEvent) bool {
var watchDir = DirEvent{}
watchDir.Revision = wRsp.Header.Revision
for _, ev := range wRsp.Events {
switch ev.Type {
case mvccpb.PUT, mvccpb.DELETE:
watchDir.AddEvent(ev)
notifyChan <- watchDir
default:
watchDir.Err = genErr(nodePath, WatchUnexpected)
notifyChan <- watchDir
return true
}
}
return false
}