Skip to content

Commit cc3053a

Browse files
W-A-Jamesnbbeeken
authored andcommitted
Update pinning logic
1 parent 9a99c8a commit cc3053a

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/sdam/server.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,25 @@ export class Server extends TypedEventEmitter<ServerEvents> {
313313
// Incrementing the operation count above the logic to handle load balanced mode would
314314
// require special logic to decrement it again, or would double increment. Instead, we
315315
// increment the count after this check.
316-
if (this.loadBalanced && session && conn == null && isPinnableCommand(cmd, session)) {
317-
conn = await this.pool.checkOut();
318-
session.pin(conn);
319-
}
316+
317+
const connShouldBePinned =
318+
this.loadBalanced && session != null && isPinnableCommand(cmd, session);
320319

321320
this.incrementOperationCount();
322321
if (!conn) {
323322
try {
324323
conn = await this.pool.checkOut();
325324
} catch (checkoutError) {
326325
if (!(checkoutError instanceof PoolClearedError)) this.handleError(checkoutError);
327-
328326
this.decrementOperationCount();
329327
throw checkoutError;
330328
}
331329
}
332330

331+
if (connShouldBePinned && !session.isPinned) {
332+
session.pin(conn);
333+
}
334+
333335
// Reauth flow
334336
try {
335337
return await this.executeCommand(conn, ns, cmd, finalOptions);
@@ -344,8 +346,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
344346
throw operationError;
345347
}
346348
} finally {
347-
this.pool.checkIn(conn);
348-
this.decrementOperationCount();
349+
if (!connShouldBePinned) {
350+
this.pool.checkIn(conn);
351+
this.decrementOperationCount();
352+
}
349353
}
350354
}
351355

@@ -362,8 +366,8 @@ export class Server extends TypedEventEmitter<ServerEvents> {
362366
): Promise<Document> {
363367
try {
364368
return await conn.command(ns, cmd, options);
365-
} catch (commandOptions) {
366-
throw this.decorateAndHandleError(conn, cmd, options, commandOptions);
369+
} catch (commandError) {
370+
throw this.decorateAndHandleError(conn, cmd, options, commandError);
367371
}
368372
}
369373

@@ -526,6 +530,7 @@ function isPinnableCommand(cmd: Document, session?: ClientSession): boolean {
526530
if (session) {
527531
return (
528532
session.inTransaction() ||
533+
(session.transaction.isCommitted && 'commitTransaction' in cmd) ||
529534
'aggregate' in cmd ||
530535
'find' in cmd ||
531536
'getMore' in cmd ||

0 commit comments

Comments
 (0)