Skip to content

Commit 85f10c3

Browse files
committed
define constants
1 parent 50d5c2c commit 85f10c3

4 files changed

Lines changed: 32 additions & 9 deletions

File tree

src/operations/execute_operation.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ import {
2828
import type { Topology } from '../sdam/topology';
2929
import type { ClientSession } from '../sessions';
3030
import { 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';
3238
import { abortable, maxWireVersion, supportsRetryableWrites } from '../utils';
3339
import { AggregateOperation } from './aggregate';
3440
import { 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 (

src/sdam/topology.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
213213
hello?: Document;
214214
_type?: string;
215215

216+
/** @internal */
216217
tokenBucket = new TokenBucket(INITIAL_TOKEN_BUCKET_SIZE);
217218

218219
client!: MongoClient;

src/sessions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { ReadConcernLevel } from './read_concern';
3030
import { ReadPreference } from './read_preference';
3131
import { _advanceClusterTime, type ClusterTime, TopologyType } from './sdam/common';
3232
import { TimeoutContext } from './timeout';
33+
import { MAX_RETRIES } from './token_bucket';
3334
import {
3435
isTransactionCommand,
3536
Transaction,
@@ -497,7 +498,7 @@ export class ClientSession
497498
readPreference: ReadPreference.primary,
498499
bypassPinningCheck: true
499500
});
500-
operation.maxAttempts = 6;
501+
operation.maxAttempts = MAX_RETRIES + 1;
501502

502503
const timeoutContext =
503504
this.timeoutContext ??
@@ -516,7 +517,7 @@ export class ClientSession
516517
} catch (firstCommitError) {
517518
this.commitAttempted = true;
518519

519-
const remainingAttempts = 6 - (operation.attemptsMade ?? 1);
520+
const remainingAttempts = MAX_RETRIES + 1 - (operation.attemptsMade ?? 1);
520521
if (remainingAttempts <= 0) {
521522
throw firstCommitError;
522523
}

src/token_bucket.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,24 @@ export const RETRY_TOKEN_RETURN_RATE = 0.1;
3131
* @internal
3232
* The initial size of the token bucket, as defined in the backpressure specification.
3333
*/
34-
export const INITIAL_TOKEN_BUCKET_SIZE = 1000;
34+
export const INITIAL_TOKEN_BUCKET_SIZE = 1_000;
3535
/**
3636
* @internal
3737
* The cost of a retry, as defined in the backpressure specification.
3838
*/
3939
export const RETRY_COST = 1;
40+
/**
41+
* @internal
42+
* The maximum number of retries for overload errors
43+
* */
44+
export const MAX_RETRIES = 5;
45+
/**
46+
* @internal
47+
* The base backoff duration in milliseconds
48+
* */
49+
export const BASE_BACKOFF_MS = 100;
50+
/**
51+
* @internal
52+
* The maximum backoff duration in milliseconds
53+
* */
54+
export const MAX_BACKOFF_MS = 10_000;

0 commit comments

Comments
 (0)