Skip to content

Commit ca7dc4f

Browse files
authored
Merge pull request #10058 from vimalk78/9882-contrib-queue-memleak
contrib/recipes/watch.go : cancel() the watch after desired watch event
2 parents 206b012 + 9bad6fd commit ca7dc4f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

contrib/recipes/watch.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ import (
2323

2424
// WaitEvents waits on a key until it observes the given events and returns the final one.
2525
func WaitEvents(c *clientv3.Client, key string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
26-
wc := c.Watch(context.Background(), key, clientv3.WithRev(rev))
26+
ctx, cancel := context.WithCancel(context.Background())
27+
defer cancel()
28+
wc := c.Watch(ctx, key, clientv3.WithRev(rev))
2729
if wc == nil {
2830
return nil, ErrNoWatcher
2931
}
3032
return waitEvents(wc, evs), nil
3133
}
3234

3335
func WaitPrefixEvents(c *clientv3.Client, prefix string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
34-
wc := c.Watch(context.Background(), prefix, clientv3.WithPrefix(), clientv3.WithRev(rev))
36+
ctx, cancel := context.WithCancel(context.Background())
37+
defer cancel()
38+
wc := c.Watch(ctx, prefix, clientv3.WithPrefix(), clientv3.WithRev(rev))
3539
if wc == nil {
3640
return nil, ErrNoWatcher
3741
}

0 commit comments

Comments
 (0)