mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
contrib/recipes/watch.go : cancel() the watch after desired watch event
fixes #9882
This commit is contained in:
parent
484f622034
commit
9bad6fd442
@ -23,7 +23,9 @@ import (
|
|||||||
|
|
||||||
// WaitEvents waits on a key until it observes the given events and returns the final one.
|
// WaitEvents waits on a key until it observes the given events and returns the final one.
|
||||||
func WaitEvents(c *clientv3.Client, key string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
|
func WaitEvents(c *clientv3.Client, key string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
|
||||||
wc := c.Watch(context.Background(), key, clientv3.WithRev(rev))
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
wc := c.Watch(ctx, key, clientv3.WithRev(rev))
|
||||||
if wc == nil {
|
if wc == nil {
|
||||||
return nil, ErrNoWatcher
|
return nil, ErrNoWatcher
|
||||||
}
|
}
|
||||||
@ -31,7 +33,9 @@ func WaitEvents(c *clientv3.Client, key string, rev int64, evs []mvccpb.Event_Ev
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WaitPrefixEvents(c *clientv3.Client, prefix string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
|
func WaitPrefixEvents(c *clientv3.Client, prefix string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
|
||||||
wc := c.Watch(context.Background(), prefix, clientv3.WithPrefix(), clientv3.WithRev(rev))
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
wc := c.Watch(ctx, prefix, clientv3.WithPrefix(), clientv3.WithRev(rev))
|
||||||
if wc == nil {
|
if wc == nil {
|
||||||
return nil, ErrNoWatcher
|
return nil, ErrNoWatcher
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user