clientv3: non-recursive watch

Signed-off-by: Ted Yu <yuzhihong@gmail.com>
This commit is contained in:
Ted Yu 2020-05-20 15:44:41 -07:00
parent 732df43cf8
commit f976138186

View File

@ -311,6 +311,8 @@ func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) Watch
ok := false ok := false
ctxKey := streamKeyFromCtx(ctx) ctxKey := streamKeyFromCtx(ctx)
var closeCh chan WatchResponse
for {
// find or allocate appropriate grpc watch stream // find or allocate appropriate grpc watch stream
w.mu.Lock() w.mu.Lock()
if w.streams == nil { if w.streams == nil {
@ -330,20 +332,24 @@ func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) Watch
w.mu.Unlock() w.mu.Unlock()
// couldn't create channel; return closed channel // couldn't create channel; return closed channel
closeCh := make(chan WatchResponse, 1) if closeCh == nil {
closeCh = make(chan WatchResponse, 1)
}
// submit request // submit request
select { select {
case reqc <- wr: case reqc <- wr:
ok = true ok = true
case <-wr.ctx.Done(): case <-wr.ctx.Done():
ok = false
case <-donec: case <-donec:
ok = false
if wgs.closeErr != nil { if wgs.closeErr != nil {
closeCh <- WatchResponse{Canceled: true, closeErr: wgs.closeErr} closeCh <- WatchResponse{Canceled: true, closeErr: wgs.closeErr}
break break
} }
// retry; may have dropped stream from no ctxs // retry; may have dropped stream from no ctxs
return w.Watch(ctx, key, opts...) continue
} }
// receive channel // receive channel
@ -358,9 +364,11 @@ func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) Watch
break break
} }
// retry; may have dropped stream from no ctxs // retry; may have dropped stream from no ctxs
return w.Watch(ctx, key, opts...) continue
} }
} }
break
}
close(closeCh) close(closeCh)
return closeCh return closeCh