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

Method acquire

src/sessions.ts:1106–1130  ·  view source on GitHub ↗

* Acquire a Server Session from the pool. * Iterates through each session in the pool, removing any stale sessions * along the way. The first non-stale session found is removed from the * pool and returned. If no non-stale session is found, a new ServerSession is created.

()

Source from the content-addressed store, hash-verified

1104 * pool and returned. If no non-stale session is found, a new ServerSession is created.
1105 */
1106 acquire(): ServerSession {
1107 const sessionTimeoutMinutes = this.client.topology?.logicalSessionTimeoutMinutes ?? 10;
1108
1109 let session: ServerSession | null = null;
1110
1111 // Try to obtain from session pool
1112 while (this.sessions.length > 0) {
1113 const potentialSession = this.sessions.shift();
1114 if (
1115 potentialSession != null &&
1116 (!!this.client.topology?.loadBalanced ||
1117 !potentialSession.hasTimedOut(sessionTimeoutMinutes))
1118 ) {
1119 session = potentialSession;
1120 break;
1121 }
1122 }
1123
1124 // If nothing valid came from the pool make a new one
1125 if (session == null) {
1126 session = new ServerSession();
1127 }
1128
1129 return session;
1130 }
1131
1132 /**
1133 * Release a session to the session pool

Callers 3

sessions.test.tsFile · 0.80
constructorMethod · 0.80
serverSessionMethod · 0.80

Calls 2

hasTimedOutMethod · 0.80
shiftMethod · 0.45

Tested by

no test coverage detected