-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add backoff/retry to all client operations
injected retry with backoff and jitter for all client operations extended ListModules server handler to include the latest version in the result
- Loading branch information
Dylan Bourque
committed
Jul 29, 2024
1 parent
6bdd47d
commit eb09bfe
Showing
4 changed files
with
105 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
"time" | ||
|
||
"connectrpc.com/connect" | ||
) | ||
|
||
var ( | ||
// subsequent retry delays for retryOp() | ||
// - use the first 5 Fibonacci numbers for semi-exponential growth | ||
// - the extra 0 value is a sentinel so we don't do another wait after we've exhausted all 5 retries | ||
backoffDelays = []time.Duration{ | ||
100 * time.Millisecond, | ||
200 * time.Millisecond, | ||
300 * time.Millisecond, | ||
500 * time.Millisecond, | ||
800 * time.Millisecond, | ||
0, | ||
} | ||
) | ||
|
||
// retryOp performs the specified operation, retrying up to 5 times if the request returns a 502-Unavailable | ||
// status to provide resiliency for transient failures due to LB flakiness (especially within K8S). | ||
func retryOp[T any](op func() (T, error)) (result T, err error) { | ||
var zero T | ||
for _, wait := range backoffDelays { | ||
result, err = op() | ||
switch { | ||
case err == nil: | ||
return result, nil | ||
case connect.CodeOf(err) == connect.CodeUnavailable: | ||
if wait > 0 { | ||
// inject up to 20% jitter | ||
maxJitter := big.NewInt(int64(float64(int64(wait)) * 0.2)) | ||
jitter, _ := rand.Int(rand.Reader, maxJitter) | ||
wait += time.Duration(jitter.Int64()) | ||
<-time.After(wait) | ||
} | ||
default: | ||
return zero, err | ||
} | ||
} | ||
// if we get here, err is non-nil and from a 502 but we have exhausted all retries | ||
return zero, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters