* 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.
()
| 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 |
no test coverage detected