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.go: update Retry backoff to follow gRFC A6 #7870

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions examples/features/retry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ RetryableStatusCodes: Retry only when receiving these status codes.
}`
```

### Backoff Logic

The backoff duration is calculated based on the following logic:

1. **Initial Backoff**: The initial delay before the first retry.
2. **Exponential Backoff**: The delay increases exponentially based on the number of retries, using the formula:
```go
backoffDuration := InitialBackoff * time.Duration(math.Pow(BackoffMultiplier, float64(numRetries)))
```
3. **Max Backoff**: The calculated backoff duration is capped at `MaxBackoff` to prevent excessively long delays.
4. **Jitter**: A random factor between 0.8 and 1.2 is applied to the backoff duration to avoid thundering herd problems. The final backoff duration is calculated as:
```go
jitter := 0.8 + rand.Float64()*0.4 // Random value between 0.8 and 1.2
dur = time.Duration(float64(backoffDuration) * jitter)
```

This means that the backoff delay may be slightly lower than `InitialBackoff` or slightly higher than `MaxBackoff`, allowing for a more distributed retry behavior across clients.

### Providing the retry policy as a DialOption

To use the above service config, pass it with `grpc.WithDefaultServiceConfig` to
Expand Down
13 changes: 8 additions & 5 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,15 @@
dur = time.Millisecond * time.Duration(pushback)
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
// Calculate the backoff duration based on the exponential backoff formula.
backoffDuration := rp.InitialBackoff * time.Duration(math.Pow(rp.BackoffMultiplier, float64(cs.numRetriesSincePushback)))
if backoffDuration > rp.MaxBackoff {
backoffDuration = rp.MaxBackoff

Check warning on line 713 in stream.go

View check run for this annotation

Codecov / codecov/patch

stream.go#L713

Added line #L713 was not covered by tests
}
dur = time.Duration(rand.Int64N(int64(cur)))

// Apply jitter by multiplying with a random factor between 0.8 and 1.2.
jitter := 0.8 + rand.Float64()*0.4
dur = time.Duration(float64(backoffDuration) * jitter)
cs.numRetriesSincePushback++
}

Expand Down