Skip to content

GODRIVER-3849 Update backpressure errors handling examples.#2365

Merged
matthewdale merged 1 commit intomongodb:masterfrom
qingyang-hu:godriver3849
Apr 24, 2026
Merged

GODRIVER-3849 Update backpressure errors handling examples.#2365
matthewdale merged 1 commit intomongodb:masterfrom
qingyang-hu:godriver3849

Conversation

@qingyang-hu
Copy link
Copy Markdown
Contributor

GODRIVER-3849

Summary

Remove all references to token buckets.

Background & Motivation

@github-actions github-actions Bot added the documentation Pull requests that update documentation or examples label Apr 20, 2026
@mongodb-drivers-pr-bot
Copy link
Copy Markdown
Contributor

🧪 Performance Results

Commit SHA: 097b4ac

The following benchmark tests for version 69e6993da119bf000724d300 had statistically significant changes (i.e., |z-score| > 1.96):

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkMultiInsertSmallDocument total_time_seconds -12.9789 1.0063 Avg: 1.1564
Med: 1.1832
Stdev: 0.0619
0.8198 -2.4251
BenchmarkSingleRunCommand total_mem_allocs -11.6110 971252.0000 Avg: 1098838.5301
Med: 1109443.0000
Stdev: 50100.6026
0.7913 -2.5466
BenchmarkSingleRunCommand total_bytes_allocated -11.4994 90360184.0000 Avg: 102101267.4699
Med: 103109048.0000
Stdev: 4641687.4931
0.7900 -2.5295
BenchmarkMultiFindMany ns_per_op 9.5345 2802.0000 Avg: 2558.0973
Med: 2560.0000
Stdev: 101.7649
0.7626 2.3967
BenchmarkSingleRunCommand total_time_seconds -6.7279 1.0422 Avg: 1.1174
Med: 1.1196
Stdev: 0.0344
0.7460 -2.1825
BenchmarkBSONFullDocumentDecoding ns_per_op 4.8383 77406.0000 Avg: 73833.6952
Med: 73647.0000
Stdev: 1564.4269
0.7666 2.2835
BenchmarkBSONFullDocumentDecoding ops_per_second_med -4.3809 13956.9289 Avg: 14596.3777
Med: 14628.6517
Stdev: 273.8032
0.7727 -2.3354
BenchmarkBSONFlatDocumentDecoding total_mem_allocs -4.2242 10198817.0000 Avg: 10648631.8476
Med: 10666111.0000
Stdev: 223100.6125
0.7248 -2.0162
BenchmarkBSONFlatDocumentDecoding total_bytes_allocated -4.2170 401125488.0000 Avg: 418785585.5238
Med: 419474440.0000
Stdev: 8756986.1562
0.7249 -2.0167
BenchmarkBSONDeepDocumentDecoding ns_per_op 3.3035 65426.0000 Avg: 63333.7810
Med: 63366.0000
Stdev: 1010.5562
0.7361 2.0704
BenchmarkSingleRunCommand allocated_bytes_per_op 0.1180 12210.0000 Avg: 12195.6093
Med: 12196.0000
Stdev: 5.4857
0.8023 2.6233
BenchmarkBSONFlatDocumentDecoding allocated_bytes_per_op 0.0099 18054.0000 Avg: 18052.2190
Med: 18052.0000
Stdev: 0.8202
0.7597 2.1714

For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.

@mongodb-drivers-pr-bot
Copy link
Copy Markdown
Contributor

API Change Report

No changes found!

@qingyang-hu qingyang-hu marked this pull request as ready for review April 21, 2026 13:14
@qingyang-hu qingyang-hu requested a review from a team as a code owner April 21, 2026 13:14
Copilot AI review requested due to automatic review settings April 21, 2026 13:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 executeWithRetries to take maxAttempts as a parameter (with a new defaultMaxAttempts).
  • 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

  • executeWithRetries accepts maxAttempts but 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 enforcing maxAttempts >= 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.

Comment on lines 73 to 78
if !isSystemOverloadedError(err) {
tb.Deposit(retryToken)
break
}
var lerr mongo.LabeledError
if !errors.As(err, &lerr) || !lerr.HasErrorLabel(errRetryableError) {
break
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@matthewdale matthewdale self-requested a review April 21, 2026 15:38
Comment on lines 39 to 42
func executeWithRetries(
ctx context.Context, tb *tokenBucket,
ctx context.Context, maxAttempts int,
fn func(ctx context.Context) (any, error),
) (any, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

@matthewdale matthewdale merged commit 00ab776 into mongodb:master Apr 24, 2026
38 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Pull requests that update documentation or examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants