Skip to content

Commit 83c9b33

Browse files
initial POC
1 parent b091159 commit 83c9b33

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const MongoErrorLabel = Object.freeze({
100100
PoolRequestedRetry: 'PoolRequestedRetry',
101101
InterruptInUseConnections: 'InterruptInUseConnections',
102102
NoWritesPerformed: 'NoWritesPerformed',
103-
SystemOverloadError: 'SystemOverloadError',
103+
SystemOverloadedError: 'SystemOverloadedError',
104104
RetryableError: 'RetryableError'
105105
} as const);
106106

src/operations/execute_operation.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type ResultTypeFromOperation<TOperation extends AbstractOperation> = ReturnType<
5959
* The expectation is that this function:
6060
* - Connects the MongoClient if it has not already been connected, see {@link autoConnect}
6161
* - Creates a session if none is provided and cleans up the session it creates
62-
* - Tries an operation and retries under certain conditions, see {@link tryOperation}
62+
* - Tries an operation and retries under certain conditions, see {@link executeOperationWithRetries}
6363
*
6464
* @typeParam T - The operation's type
6565
* @typeParam TResult - The type of the operation's result, calculated from T
@@ -129,7 +129,7 @@ export async function executeOperation<
129129
});
130130

131131
try {
132-
return await tryOperation(operation, {
132+
return await executeOperationWithRetries(operation, {
133133
topology,
134134
timeoutContext,
135135
session,
@@ -193,7 +193,10 @@ type RetryOptions = {
193193
*
194194
* @param operation - The operation to execute
195195
* */
196-
async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFromOperation<T>>(
196+
async function executeOperationWithRetries<
197+
T extends AbstractOperation,
198+
TResult = ResultTypeFromOperation<T>
199+
>(
197200
operation: T,
198201
{ topology, timeoutContext, session, readPreference }: RetryOptions
199202
): Promise<TResult> {
@@ -247,7 +250,7 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro
247250
const maxNonOverloadRetryAttempts = willRetry ? (timeoutContext.csotEnabled() ? Infinity : 2) : 1;
248251
let previousOperationError: MongoError | undefined;
249252
const deprioritizedServers = new DeprioritizedServers();
250-
const nonOverloadRetryAttempt = 0;
253+
let nonOverloadRetryAttempt = 0;
251254

252255
let systemOverloadRetryAttempt = 0;
253256
const maxSystemOverloadRetryAttempts = 5;
@@ -259,7 +262,7 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro
259262

260263
while (true) {
261264
if (previousOperationError) {
262-
if (previousOperationError.hasErrorLabel(MongoErrorLabel.SystemOverloadError)) {
265+
if (previousOperationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)) {
263266
systemOverloadRetryAttempt += 1;
264267

265268
if (
@@ -277,7 +280,7 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro
277280
}
278281

279282
// if we have exhausted overload retry attempts, throw.
280-
if (systemOverloadRetryAttempt > maxSystemOverloadRetryAttempts) {
283+
if (systemOverloadRetryAttempt >= maxSystemOverloadRetryAttempts) {
281284
throw previousOperationError;
282285
}
283286

@@ -307,6 +310,7 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro
307310
signal: operation.options.signal
308311
});
309312
} else {
313+
nonOverloadRetryAttempt++;
310314
// we have no more retry attempts, throw.
311315
if (nonOverloadRetryAttempt >= maxNonOverloadRetryAttempts) {
312316
throw previousOperationError;
@@ -378,7 +382,7 @@ async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFro
378382
} catch (operationError) {
379383
if (!(operationError instanceof MongoError)) throw operationError;
380384

381-
if (!operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadError)) {
385+
if (!operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)) {
382386
// if an operation fails with an error that does not contain the SystemOverloadError, deposit 1 token.
383387
topology.tokenBucket.deposit(RETRY_COST);
384388
}

0 commit comments

Comments
 (0)