This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
/
watch.go
203 lines (175 loc) · 4.33 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2017 Ross Peoples
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mydis
import (
"strings"
"sync"
"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/mvcc"
"github.com/deejross/mydis/pb"
"github.com/deejross/mydis/util"
)
// WatchController object.
type WatchController struct {
closeCh chan struct{}
server *etcdserver.EtcdServer
watchers map[int64]*Watcher
nextID int64
lock sync.RWMutex
}
// NewWatchController returns a new WatchController object.
func NewWatchController(server *etcdserver.EtcdServer) *WatchController {
wc := &WatchController{
closeCh: make(chan struct{}),
server: server,
watchers: map[int64]*Watcher{},
}
return wc
}
// NewWatcher returns a new Watcher object.
func (w *WatchController) NewWatcher() *Watcher {
watcher := &Watcher{
closeCh: make(chan struct{}),
reqCh: make(chan *pb.WatchRequest),
resCh: make(chan struct{}),
eventCh: make(chan *pb.Event),
controller: w,
watching: map[string]*pb.WatchRequest{},
}
w.lock.Lock()
w.watchers[w.nextID] = watcher
go func(id int64) {
watcher.backgroundProcess()
w.lock.Lock()
delete(w.watchers, id)
w.lock.Unlock()
}(w.nextID)
w.nextID++
w.lock.Unlock()
return watcher
}
// Close the WatchController.
func (w *WatchController) Close() {
watchers := []*Watcher{}
// get list of watchers then loop through and Close to avoid deadlock.
w.lock.RLock()
for _, watcher := range w.watchers {
watchers = append(watchers, watcher)
}
w.lock.RUnlock()
for _, watcher := range watchers {
watcher.Close()
}
}
// Watcher object.
type Watcher struct {
controller *WatchController
closeCh chan struct{}
reqCh chan *pb.WatchRequest
resCh chan struct{}
eventCh chan *pb.Event
server *etcdserver.EtcdServer
watching map[string]*pb.WatchRequest
}
// RequestID gets the string ID from the WatchRequest.
func (w *Watcher) RequestID(r *pb.WatchRequest) string {
id := r.Key
if r.Prefix {
id += suffixForKeysUsingPrefix
}
return id
}
// Close the Watcher.
func (w *Watcher) Close() {
w.closeCh <- struct{}{}
}
func (w *Watcher) backgroundProcess() {
stream := w.controller.server.Watchable().NewWatchStream()
streamCh := stream.Chan()
defer func() {
stream.Close()
close(w.closeCh)
close(w.reqCh)
close(w.resCh)
close(w.eventCh)
}()
for {
select {
case <-w.closeCh:
return
case r := <-w.reqCh:
hash := w.RequestID(r)
if wr, ok := w.watching[hash]; ok && r.Cancel {
// process cancelation.
stream.Cancel(mvcc.WatchID(wr.Id))
delete(w.watching, hash)
w.resCh <- struct{}{}
continue
} else if ok {
// prevent duplcates.
w.resCh <- struct{}{}
continue
}
bkey := util.StringToBytes(r.Key)
end := []byte{}
if r.Prefix {
end = getPrefix(r.Key)
}
r.Id = int64(stream.Watch(bkey, end, r.Rev))
w.watching[hash] = r
w.resCh <- struct{}{}
case r := <-streamCh:
for _, e := range r.Events {
ev := &pb.Event{
Type: pb.Event_EventType(e.Type),
Current: &pb.ByteValue{},
Previous: &pb.ByteValue{},
}
if e.Kv != nil {
ev.Current = &pb.ByteValue{Key: util.BytesToString(e.Kv.Key), Value: e.Kv.Value}
}
if e.PrevKv != nil {
ev.Previous = &pb.ByteValue{Key: util.BytesToString(e.PrevKv.Key), Value: e.PrevKv.Value}
}
for _, r := range w.watching {
key := util.BytesToString(e.Kv.Key)
if r.Key == key || (r.Prefix && strings.HasPrefix(key, r.Key)) {
w.eventCh <- ev
}
}
}
}
}
}
// Watch a key for changes.
func (s *Server) Watch(stream pb.Mydis_WatchServer) error {
watcher := s.wc.NewWatcher()
defer watcher.Close()
// sender
go func() {
for {
ev := <-watcher.eventCh
stream.Send(ev)
}
}()
// receiver
for {
r, err := stream.Recv()
if err != nil {
return err
}
watcher.reqCh <- r
<-watcher.resCh
}
}