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

Method clear

src/cmap/connection_pool.ts:417–468  ·  view source on GitHub ↗

* Clear the pool * * Pool reset is handled by incrementing the pool's generation count. Any existing connection of a * previous generation will eventually be pruned during subsequent checkouts.

(options: { serviceId?: ObjectId; interruptInUseConnections?: boolean } = {})

Source from the content-addressed store, hash-verified

415 * previous generation will eventually be pruned during subsequent checkouts.
416 */
417 clear(options: { serviceId?: ObjectId; interruptInUseConnections?: boolean } = {}): void {
418 if (this.closed) {
419 return;
420 }
421
422 // handle load balanced case
423 if (this.loadBalanced) {
424 const { serviceId } = options;
425 if (!serviceId) {
426 throw new MongoRuntimeError(
427 'ConnectionPool.clear() called in load balanced mode with no serviceId.'
428 );
429 }
430 const sid = serviceId.toHexString();
431 const generation = this.serviceGenerations.get(sid);
432 // Only need to worry if the generation exists, since it should
433 // always be there but typescript needs the check.
434 if (generation == null) {
435 throw new MongoRuntimeError('Service generations are required in load balancer mode.');
436 } else {
437 // Increment the generation for the service id.
438 this.serviceGenerations.set(sid, generation + 1);
439 }
440 this.emitAndLog(
441 ConnectionPool.CONNECTION_POOL_CLEARED,
442 new ConnectionPoolClearedEvent(this, { serviceId })
443 );
444 return;
445 }
446 // handle non load-balanced case
447 const interruptInUseConnections = options.interruptInUseConnections ?? false;
448 const oldGeneration = this.generation;
449 this.generation += 1;
450 const alreadyPaused = this.poolState === PoolState.paused;
451 this.poolState = PoolState.paused;
452
453 this.clearMinPoolSizeTimer();
454 if (!alreadyPaused) {
455 this.emitAndLog(
456 ConnectionPool.CONNECTION_POOL_CLEARED,
457 new ConnectionPoolClearedEvent(this, {
458 interruptInUseConnections
459 })
460 );
461 }
462
463 if (interruptInUseConnections) {
464 queueMicrotask(() => this.interruptInUseConnections(oldGeneration));
465 }
466
467 this.processWaitQueue();
468 }
469
470 /**
471 * Closes all stale in-use connections in the pool with a resumable PoolClearedOnNetworkError.

Callers 3

checkOutMethod · 0.45
closeMethod · 0.45
writeCommandMethod · 0.45

Calls 6

clearMinPoolSizeTimerMethod · 0.95
processWaitQueueMethod · 0.95
setMethod · 0.80
emitAndLogMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected