MCPcopy
hub / github.com/mongodb/node-mongodb-native / maybeClearPinnedConnection

Function maybeClearPinnedConnection

src/sessions.ts:983–1022  ·  view source on GitHub ↗
(
  session: ClientSession,
  options?: EndSessionOptions
)

Source from the content-addressed store, hash-verified

981}
982
983export function maybeClearPinnedConnection(
984 session: ClientSession,
985 options?: EndSessionOptions
986): void {
987 // unpin a connection if it has been pinned
988 const conn = session.pinnedConnection;
989 const error = options?.error;
990
991 if (
992 session.inTransaction() &&
993 error &&
994 error instanceof MongoError &&
995 error.hasErrorLabel(MongoErrorLabel.TransientTransactionError)
996 ) {
997 return;
998 }
999
1000 const topology = session.client.topology;
1001 // NOTE: the spec talks about what to do on a network error only, but the tests seem to
1002 // to validate that we don't unpin on _all_ errors?
1003 if (conn && topology != null) {
1004 const servers = Array.from(topology.s.servers.values());
1005 const loadBalancer = servers[0];
1006
1007 if (options?.error == null || options?.force) {
1008 loadBalancer.pool.checkIn(conn);
1009 session.pinnedConnection = undefined;
1010 conn.emit(
1011 UNPINNED,
1012 session.transaction.state !== TxnState.NO_TRANSACTION
1013 ? ConnectionPoolMetrics.TXN
1014 : ConnectionPoolMetrics.CURSOR
1015 );
1016
1017 if (options?.forceClear) {
1018 loadBalancer.pool.clear({ serviceId: conn.serviceId });
1019 }
1020 }
1021 }
1022}
1023
1024function isMaxTimeMSExpiredError(err: MongoError): boolean {
1025 if (err == null || !(err instanceof MongoServerError)) {

Callers 4

closeFunction · 0.90
unpinMethod · 0.85
endSessionMethod · 0.85
abortTransactionMethod · 0.85

Calls 5

inTransactionMethod · 0.80
checkInMethod · 0.80
emitMethod · 0.65
hasErrorLabelMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected