Skip to content

Commit c0dd1ca

Browse files
nbbeekenbaileympearson
authored andcommitted
refactor(NODE-6230): executeOperation to use iterative retry mechanism (#4157)
1 parent 06db721 commit c0dd1ca

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/cmap/connection_pool.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
import { CancellationToken, TypedEventEmitter } from '../mongo_types';
2929
import type { Server } from '../sdam/server';
3030
import { type TimeoutContext, TimeoutError } from '../timeout';
31-
import { type Callback, List, makeCounter, promiseWithResolvers } from '../utils';
31+
import { type Callback, List, makeCounter, now, promiseWithResolvers } from '../utils';
3232
import { connect } from './connect';
3333
import { Connection, type ConnectionEvents, type ConnectionOptions } from './connection';
3434
import {
@@ -356,6 +356,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
356356
* explicitly destroyed by the new owner.
357357
*/
358358
async checkOut(options: { timeoutContext: TimeoutContext }): Promise<Connection> {
359+
const checkoutTime = now();
359360
this.emitAndLog(
360361
ConnectionPool.CONNECTION_CHECK_OUT_STARTED,
361362
new ConnectionCheckOutStartedEvent(this)
@@ -367,7 +368,8 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
367368

368369
const waitQueueMember: WaitQueueMember = {
369370
resolve,
370-
reject
371+
reject,
372+
checkoutTime
371373
};
372374

373375
this[kWaitQueue].push(waitQueueMember);

src/operations/execute_operation.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import type { Topology } from '../sdam/topology';
2626
import type { ClientSession } from '../sessions';
2727
import { TimeoutContext } from '../timeout';
28-
import { squashError, supportsRetryableWrites } from '../utils';
28+
import { supportsRetryableWrites } from '../utils';
2929
import { AbstractOperation, Aspect } from './operation';
3030

3131
const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation;
@@ -87,12 +87,6 @@ export async function executeOperation<
8787
);
8888
}
8989

90-
timeoutContext ??= TimeoutContext.create({
91-
serverSelectionTimeoutMS: client.s.options.serverSelectionTimeoutMS,
92-
waitQueueTimeoutMS: client.s.options.waitQueueTimeoutMS,
93-
timeoutMS: operation.options.timeoutMS
94-
});
95-
9690
const readPreference = operation.readPreference ?? ReadPreference.primary;
9791
const inTransaction = !!session?.inTransaction();
9892

@@ -112,12 +106,18 @@ export async function executeOperation<
112106
session.unpin();
113107
}
114108

109+
timeoutContext ??= TimeoutContext.create({
110+
serverSelectionTimeoutMS: client.s.options.serverSelectionTimeoutMS,
111+
waitQueueTimeoutMS: client.s.options.waitQueueTimeoutMS,
112+
timeoutMS: operation.options.timeoutMS
113+
});
114+
115115
try {
116116
return await tryOperation(operation, {
117117
topology,
118+
timeoutContext,
118119
session,
119-
readPreference,
120-
timeoutContext
120+
readPreference
121121
});
122122
} finally {
123123
if (session?.owner != null && session.owner === owner) {
@@ -156,6 +156,7 @@ type RetryOptions = {
156156
session: ClientSession | undefined;
157157
readPreference: ReadPreference;
158158
topology: Topology;
159+
timeoutContext: TimeoutContext;
159160
};
160161

161162
/**
@@ -179,7 +180,10 @@ type RetryOptions = {
179180
async function tryOperation<
180181
T extends AbstractOperation<TResult>,
181182
TResult = ResultTypeFromOperation<T>
182-
>(operation: T, { topology, session, readPreference }: RetryOptions): Promise<TResult> {
183+
>(
184+
operation: T,
185+
{ topology, timeoutContext, session, readPreference }: RetryOptions
186+
): Promise<TResult> {
183187
let selector: ReadPreference | ServerSelector;
184188

185189
if (operation.hasAspect(Aspect.MUST_SELECT_SAME_SERVER)) {
@@ -197,7 +201,8 @@ async function tryOperation<
197201

198202
let server = await topology.selectServer(selector, {
199203
session,
200-
operationName: operation.commandName
204+
operationName: operation.commandName,
205+
timeoutContext
201206
});
202207

203208
const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION);

0 commit comments

Comments
 (0)