* Check a connection out of this pool. The connection will continue to be tracked, but no reference to it * will be held by the pool. This means that if a connection is checked out it MUST be checked back in or * explicitly destroyed by the new owner.
(options: { timeoutContext: TimeoutContext } & Abortable)
| 319 | * explicitly destroyed by the new owner. |
| 320 | */ |
| 321 | async checkOut(options: { timeoutContext: TimeoutContext } & Abortable): Promise<Connection> { |
| 322 | const checkoutTime = processTimeMS(); |
| 323 | this.emitAndLog( |
| 324 | ConnectionPool.CONNECTION_CHECK_OUT_STARTED, |
| 325 | new ConnectionCheckOutStartedEvent(this) |
| 326 | ); |
| 327 | |
| 328 | const { promise, resolve, reject } = promiseWithResolvers<Connection>(); |
| 329 | |
| 330 | const timeout = options.timeoutContext.connectionCheckoutTimeout; |
| 331 | |
| 332 | const waitQueueMember: WaitQueueMember = { |
| 333 | resolve, |
| 334 | reject, |
| 335 | cancelled: false, |
| 336 | checkoutTime |
| 337 | }; |
| 338 | |
| 339 | const abortListener = addAbortListener(options.signal, function () { |
| 340 | waitQueueMember.cancelled = true; |
| 341 | reject(this.reason); |
| 342 | }); |
| 343 | |
| 344 | this.waitQueue.push(waitQueueMember); |
| 345 | queueMicrotask(() => this.processWaitQueue()); |
| 346 | |
| 347 | try { |
| 348 | timeout?.throwIfExpired(); |
| 349 | return await (timeout ? Promise.race([promise, timeout]) : promise); |
| 350 | } catch (error) { |
| 351 | if (TimeoutError.is(error)) { |
| 352 | timeout?.clear(); |
| 353 | waitQueueMember.cancelled = true; |
| 354 | |
| 355 | this.emitAndLog( |
| 356 | ConnectionPool.CONNECTION_CHECK_OUT_FAILED, |
| 357 | new ConnectionCheckOutFailedEvent(this, 'timeout', waitQueueMember.checkoutTime) |
| 358 | ); |
| 359 | const timeoutError = new WaitQueueTimeoutError( |
| 360 | this.loadBalanced |
| 361 | ? this.waitQueueErrorMetrics() |
| 362 | : 'Timed out while checking out a connection from connection pool', |
| 363 | this.address |
| 364 | ); |
| 365 | if (options.timeoutContext.csotEnabled()) { |
| 366 | throw new MongoOperationTimeoutError('Timed out during connection checkout', { |
| 367 | cause: timeoutError |
| 368 | }); |
| 369 | } |
| 370 | throw timeoutError; |
| 371 | } |
| 372 | throw error; |
| 373 | } finally { |
| 374 | abortListener?.[kDispose](); |
| 375 | timeout?.clear(); |
| 376 | } |
| 377 | } |
| 378 |
no test coverage detected