Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: add jitter to retry backoff in accordance with gRFC A6 #7869

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func init() {
return parseServiceConfig(js, defaultMaxCallAttempts)
}
}

func parseServiceConfig(js string, maxAttempts int) *serviceconfig.ParseResult {
if len(js) == 0 {
return &serviceconfig.ParseResult{Err: fmt.Errorf("no JSON service config provided")}
Expand Down Expand Up @@ -297,7 +298,7 @@ func convertRetryPolicy(jrp *jsonRetryPolicy, maxAttempts int) (p *internalservi
return rp, nil
}

func min(a, b *int) *int {
func minPointers(a, b *int) *int {
if *a < *b {
return a
}
Expand All @@ -309,7 +310,7 @@ func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
return &defaultVal
}
if mcMax != nil && doptMax != nil {
return min(mcMax, doptMax)
return minPointers(mcMax, doptMax)
}
if mcMax != nil {
return mcMax
Expand Down
11 changes: 5 additions & 6 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth

var mc serviceconfig.MethodConfig
var onCommit func()
var newStream = func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
newStream := func(ctx context.Context, done func()) (iresolver.ClientStream, error) {
return newClientStreamWithParams(ctx, desc, cc, method, mc, onCommit, done, opts...)
}

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

Expand Down