Skip to content

Commit 4c07bca

Browse files
authored
stream: add jitter to retry backoff in accordance with gRFC A6 (#7869)
1 parent 967ba46 commit 4c07bca

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

service_config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func init() {
168168
return parseServiceConfig(js, defaultMaxCallAttempts)
169169
}
170170
}
171+
171172
func parseServiceConfig(js string, maxAttempts int) *serviceconfig.ParseResult {
172173
if len(js) == 0 {
173174
return &serviceconfig.ParseResult{Err: fmt.Errorf("no JSON service config provided")}
@@ -297,7 +298,7 @@ func convertRetryPolicy(jrp *jsonRetryPolicy, maxAttempts int) (p *internalservi
297298
return rp, nil
298299
}
299300

300-
func min(a, b *int) *int {
301+
func minPointers(a, b *int) *int {
301302
if *a < *b {
302303
return a
303304
}
@@ -309,7 +310,7 @@ func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
309310
return &defaultVal
310311
}
311312
if mcMax != nil && doptMax != nil {
312-
return min(mcMax, doptMax)
313+
return minPointers(mcMax, doptMax)
313314
}
314315
if mcMax != nil {
315316
return mcMax

stream.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
218218

219219
var mc serviceconfig.MethodConfig
220220
var onCommit func()
221-
var newStream = func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
221+
newStream := func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
222222
return newClientStreamWithParams(ctx, desc, cc, method, mc, onCommit, done, opts...)
223223
}
224224

@@ -708,11 +708,10 @@ func (a *csAttempt) shouldRetry(err error) (bool, error) {
708708
cs.numRetriesSincePushback = 0
709709
} else {
710710
fact := math.Pow(rp.BackoffMultiplier, float64(cs.numRetriesSincePushback))
711-
cur := float64(rp.InitialBackoff) * fact
712-
if max := float64(rp.MaxBackoff); cur > max {
713-
cur = max
714-
}
715-
dur = time.Duration(rand.Int64N(int64(cur)))
711+
cur := min(float64(rp.InitialBackoff)*fact, float64(rp.MaxBackoff))
712+
// Apply jitter by multiplying with a random factor between 0.8 and 1.2
713+
cur *= 0.8 + 0.4*rand.Float64()
714+
dur = time.Duration(int64(cur))
716715
cs.numRetriesSincePushback++
717716
}
718717

0 commit comments

Comments
 (0)