Skip to content

Commit

Permalink
Merge pull request #14582 from tomari/tomari/watch-backoff-for-3.5
Browse files Browse the repository at this point in the history
[3.5] client/v3: Add backoff before retry when watch stream returns unavailable
  • Loading branch information
ahrtr authored Oct 12, 2022
2 parents acc7463 + d3da22f commit 62169d1
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions client/v3/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ func (w *watchGrpcStream) run() {
cancelSet := make(map[int64]struct{})

var cur *pb.WatchResponse
backoff := time.Millisecond
for {
select {
// Watch() requested
Expand Down Expand Up @@ -677,6 +678,7 @@ func (w *watchGrpcStream) run() {
closeErr = err
return
}
backoff = w.backoffIfUnavailable(backoff, err)
if wc, closeErr = w.newWatchClient(); closeErr != nil {
return
}
Expand Down Expand Up @@ -996,6 +998,21 @@ func (w *watchGrpcStream) joinSubstreams() {

var maxBackoff = 100 * time.Millisecond

func (w *watchGrpcStream) backoffIfUnavailable(backoff time.Duration, err error) time.Duration {
if isUnavailableErr(w.ctx, err) {
// retry, but backoff
if backoff < maxBackoff {
// 25% backoff factor
backoff = backoff + backoff/4
if backoff > maxBackoff {
backoff = maxBackoff
}
}
time.Sleep(backoff)
}
return backoff
}

// openWatchClient retries opening a watch client until success or halt.
// manually retry in case "ws==nil && err==nil"
// TODO: remove FailFast=false
Expand All @@ -1016,17 +1033,7 @@ func (w *watchGrpcStream) openWatchClient() (ws pb.Watch_WatchClient, err error)
if isHaltErr(w.ctx, err) {
return nil, v3rpc.Error(err)
}
if isUnavailableErr(w.ctx, err) {
// retry, but backoff
if backoff < maxBackoff {
// 25% backoff factor
backoff = backoff + backoff/4
if backoff > maxBackoff {
backoff = maxBackoff
}
}
time.Sleep(backoff)
}
backoff = w.backoffIfUnavailable(backoff, err)
}
return ws, nil
}
Expand Down

0 comments on commit 62169d1

Please sign in to comment.