Skip to content

Commit

Permalink
Merge pull request #10058 from vimalk78/9882-contrib-queue-memleak
Browse files Browse the repository at this point in the history
contrib/recipes/watch.go : cancel() the watch after desired watch event
  • Loading branch information
xiang90 authored Sep 1, 2018
2 parents 206b012 + 9bad6fd commit ca7dc4f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions contrib/recipes/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ import (

// 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) {
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 {
return nil, ErrNoWatcher
}
return waitEvents(wc, evs), nil
}

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 {
return nil, ErrNoWatcher
}
Expand Down

0 comments on commit ca7dc4f

Please sign in to comment.