GODRIVER-3849 Update backpressure errors handling examples.#2365
GODRIVER-3849 Update backpressure errors handling examples.#2365matthewdale merged 1 commit intomongodb:masterfrom
Conversation
🧪 Performance ResultsCommit SHA: 097b4acThe following benchmark tests for version 69e6993da119bf000724d300 had statistically significant changes (i.e., |z-score| > 1.96):
For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch. |
API Change ReportNo changes found! |
There was a problem hiding this comment.
Pull request overview
Updates the overload/backpressure retry example to remove the previously documented token-bucket approach and simplify the sample retry logic.
Changes:
- Removes token-bucket constants/types and related retry-limiting logic from the example.
- Updates
executeWithRetriesto takemaxAttemptsas a parameter (with a newdefaultMaxAttempts). - Simplifies overload label detection and updates the example’s error handling.
Comments suppressed due to low confidence (1)
examples/_example_overload_error_test.go:47
executeWithRetriesacceptsmaxAttemptsbut doesn’t validate it. If a caller passes 0 (or a negative value), the loop never runs and the function returns(nil, nil), which looks like a successful call with no result. Consider enforcingmaxAttempts >= 1(e.g., clamp to 1) or returning an explicit error for invalid values.
func executeWithRetries(
ctx context.Context, maxAttempts int,
fn func(ctx context.Context) (any, error),
) (any, error) {
var result any
var err error
expDur := baseBackoff
for attempts := 0; attempts < maxAttempts; attempts++ {
isRetry := attempts > 0
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if !isSystemOverloadedError(err) { | ||
| tb.Deposit(retryToken) | ||
| break | ||
| } | ||
| var lerr mongo.LabeledError | ||
| if !errors.As(err, &lerr) || !lerr.HasErrorLabel(errRetryableError) { | ||
| break |
There was a problem hiding this comment.
The code calls errors.As(err, &lerr) twice in the same iteration: once indirectly via isSystemOverloadedError(err) and then again before checking RetryableError. Since isSystemOverloadedError already requires errors.As to succeed, consider restructuring to do a single errors.As and then check both labels on the same lerr value to reduce duplication and keep the control flow clearer.
| func executeWithRetries( | ||
| ctx context.Context, tb *tokenBucket, | ||
| ctx context.Context, maxAttempts int, | ||
| fn func(ctx context.Context) (any, error), | ||
| ) (any, error) { |
There was a problem hiding this comment.
This isn't updated in this PR, but this func should use generics.
func executeWithRetries[T any](
ctx context.Context, maxAttempts int,
fn func(ctx context.Context) (T, error),
) (T, error) {
// ...I've filed GODRIVER-3870 to make that improvement.
GODRIVER-3849
Summary
Remove all references to token buckets.
Background & Motivation