@@ -29,13 +29,6 @@ import {
2929import type { Topology } from '../sdam/topology' ;
3030import type { ClientSession } from '../sessions' ;
3131import { TimeoutContext } from '../timeout' ;
32- import {
33- BASE_BACKOFF_MS ,
34- MAX_BACKOFF_MS ,
35- MAX_RETRIES ,
36- RETRY_COST ,
37- RETRY_TOKEN_RETURN_RATE
38- } from '../token_bucket' ;
3932import { abortable , maxWireVersion , supportsRetryableWrites } from '../utils' ;
4033import { AggregateOperation } from './aggregate' ;
4134import { AbstractOperation , Aspect } from './operation' ;
@@ -176,6 +169,10 @@ type RetryOptions = {
176169 topology : Topology ;
177170 timeoutContext : TimeoutContext ;
178171} ;
172+ /** @internal The base backoff duration in milliseconds */
173+ const BASE_BACKOFF_MS = 100 ;
174+ /** @internal The maximum backoff duration in milliseconds */
175+ const MAX_BACKOFF_MS = 10_000 ;
179176
180177/**
181178 * Executes an operation and retries as appropriate
@@ -267,13 +264,6 @@ async function executeOperationWithRetries<
267264 try {
268265 try {
269266 const result = await server . command ( operation , timeoutContext ) ;
270- if ( topology . s . options . adaptiveRetries ) {
271- topology . tokenBucket . deposit (
272- attempt > 0
273- ? RETRY_TOKEN_RETURN_RATE + RETRY_COST // on successful retry
274- : RETRY_TOKEN_RETURN_RATE // otherwise
275- ) ;
276- }
277267 return operation . handleOk ( result ) ;
278268 } catch ( error ) {
279269 return operation . handleError ( error ) ;
@@ -282,15 +272,6 @@ async function executeOperationWithRetries<
282272 // Should never happen but if it does - propagate the error.
283273 if ( ! ( operationError instanceof MongoError ) ) throw operationError ;
284274
285- if (
286- topology . s . options . adaptiveRetries &&
287- attempt > 0 &&
288- ! operationError . hasErrorLabel ( MongoErrorLabel . SystemOverloadedError )
289- ) {
290- // if a retry attempt fails with a non-overload error, deposit 1 token.
291- topology . tokenBucket . deposit ( RETRY_COST ) ;
292- }
293-
294275 // Preserve the original error once a write has been performed.
295276 // Only update to the latest error if no writes were performed.
296277 if ( error == null ) {
@@ -317,7 +298,8 @@ async function executeOperationWithRetries<
317298 }
318299
319300 if ( operationError . hasErrorLabel ( MongoErrorLabel . SystemOverloadedError ) ) {
320- maxAttempts = Math . min ( MAX_RETRIES + 1 , operation . maxAttempts ?? MAX_RETRIES + 1 ) ;
301+ const maxOverloadAttempts = topology . s . options . maxAdaptiveRetries + 1 ;
302+ maxAttempts = Math . min ( maxOverloadAttempts , operation . maxAttempts ?? maxOverloadAttempts ) ;
321303 }
322304
323305 if ( attempt + 1 >= maxAttempts ) {
@@ -352,16 +334,13 @@ async function executeOperationWithRetries<
352334 throw error ;
353335 }
354336
355- if ( topology . s . options . adaptiveRetries && ! topology . tokenBucket . consume ( RETRY_COST ) ) {
356- throw error ;
357- }
358-
359337 await setTimeout ( backoffMS ) ;
360338 }
361339
362340 if (
363341 topology . description . type === TopologyType . Sharded ||
364- operationError . hasErrorLabel ( MongoErrorLabel . SystemOverloadedError )
342+ ( operationError . hasErrorLabel ( MongoErrorLabel . SystemOverloadedError ) &&
343+ topology . s . options . enableOverloadRetargeting )
365344 ) {
366345 deprioritizedServers . add ( server . description ) ;
367346 }
0 commit comments