@@ -28,7 +28,13 @@ import {
2828import type { Topology } from '../sdam/topology' ;
2929import type { ClientSession } from '../sessions' ;
3030import { TimeoutContext } from '../timeout' ;
31- import { RETRY_COST , RETRY_TOKEN_RETURN_RATE } from '../token_bucket' ;
31+ import {
32+ BASE_BACKOFF_MS ,
33+ MAX_BACKOFF_MS ,
34+ MAX_RETRIES ,
35+ RETRY_COST ,
36+ RETRY_TOKEN_RETURN_RATE
37+ } from '../token_bucket' ;
3238import { abortable , maxWireVersion , supportsRetryableWrites } from '../utils' ;
3339import { AggregateOperation } from './aggregate' ;
3440import { AbstractOperation , Aspect } from './operation' ;
@@ -302,7 +308,7 @@ async function executeOperationWithRetries<
302308 }
303309
304310 if ( operationError . hasErrorLabel ( MongoErrorLabel . SystemOverloadedError ) ) {
305- maxAttempts = Math . min ( 6 , operation . maxAttempts ?? 6 ) ;
311+ maxAttempts = Math . min ( MAX_RETRIES + 1 , operation . maxAttempts ?? MAX_RETRIES + 1 ) ;
306312 }
307313
308314 if ( attempt + 1 >= maxAttempts ) {
@@ -314,14 +320,14 @@ async function executeOperationWithRetries<
314320 throw error ;
315321 }
316322
317- const delayMS = Math . random ( ) * Math . min ( 10_000 , 100 * 2 ** attempt ) ;
323+ const backoffMS = Math . random ( ) * Math . min ( MAX_BACKOFF_MS , BASE_BACKOFF_MS * 2 ** attempt ) ;
318324
319- // if the delay would exhaust the CSOT timeout, short-circuit.
320- if ( timeoutContext . csotEnabled ( ) && delayMS > timeoutContext . remainingTimeMS ) {
325+ // if the backoff would exhaust the CSOT timeout, short-circuit.
326+ if ( timeoutContext . csotEnabled ( ) && backoffMS > timeoutContext . remainingTimeMS ) {
321327 throw error ;
322328 }
323329
324- await setTimeout ( delayMS ) ;
330+ await setTimeout ( backoffMS ) ;
325331 }
326332
327333 if (
0 commit comments