|
@@ -23,6 +23,7 @@ import (
|
|
|
|
|
|
|
|
type Watcher struct {
|
|
type Watcher struct {
|
|
|
wstream pb.Watch_WatchClient
|
|
wstream pb.Watch_WatchClient
|
|
|
|
|
+ cancel context.CancelFunc
|
|
|
donec chan struct{}
|
|
donec chan struct{}
|
|
|
id storage.WatchID
|
|
id storage.WatchID
|
|
|
recvc chan *storagepb.Event
|
|
recvc chan *storagepb.Event
|
|
@@ -38,7 +39,8 @@ func NewPrefixWatcher(c *EtcdClient, prefix string, rev int64) (*Watcher, error)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func newWatcher(c *EtcdClient, key string, rev int64, isPrefix bool) (*Watcher, error) {
|
|
func newWatcher(c *EtcdClient, key string, rev int64, isPrefix bool) (*Watcher, error) {
|
|
|
- w, err := c.Watch.Watch(context.Background())
|
|
|
|
|
|
|
+ ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
+ w, err := c.Watch.Watch(ctx)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
@@ -50,7 +52,7 @@ func newWatcher(c *EtcdClient, key string, rev int64, isPrefix bool) (*Watcher,
|
|
|
req.Key = []byte(key)
|
|
req.Key = []byte(key)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if err := w.Send(&pb.WatchRequest{CreateRequest: req}); err != nil {
|
|
|
|
|
|
|
+ if err := w.Send(&pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{CreateRequest: req}}); err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -63,6 +65,7 @@ func newWatcher(c *EtcdClient, key string, rev int64, isPrefix bool) (*Watcher,
|
|
|
}
|
|
}
|
|
|
ret := &Watcher{
|
|
ret := &Watcher{
|
|
|
wstream: w,
|
|
wstream: w,
|
|
|
|
|
+ cancel: cancel,
|
|
|
donec: make(chan struct{}),
|
|
donec: make(chan struct{}),
|
|
|
id: storage.WatchID(wresp.WatchId),
|
|
id: storage.WatchID(wresp.WatchId),
|
|
|
recvc: make(chan *storagepb.Event),
|
|
recvc: make(chan *storagepb.Event),
|
|
@@ -72,11 +75,14 @@ func newWatcher(c *EtcdClient, key string, rev int64, isPrefix bool) (*Watcher,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (w *Watcher) Close() error {
|
|
func (w *Watcher) Close() error {
|
|
|
|
|
+ defer w.cancel()
|
|
|
if w.wstream == nil {
|
|
if w.wstream == nil {
|
|
|
return w.lastErr
|
|
return w.lastErr
|
|
|
}
|
|
}
|
|
|
- req := &pb.WatchCancelRequest{WatchId: int64(w.id)}
|
|
|
|
|
- err := w.wstream.Send(&pb.WatchRequest{CancelRequest: req})
|
|
|
|
|
|
|
+ req := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CancelRequest{
|
|
|
|
|
+ CancelRequest: &pb.WatchCancelRequest{
|
|
|
|
|
+ WatchId: int64(w.id)}}}
|
|
|
|
|
+ err := w.wstream.Send(req)
|
|
|
if err != nil && w.lastErr == nil {
|
|
if err != nil && w.lastErr == nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|