@@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
22import type { TcpNetConnectOpts } from 'net' ;
33import type { ConnectionOptions as TLSConnectionOptions , TLSSocketOptions } from 'tls' ;
44
5- import { type ServerCommandOptions , type TimeoutContext } from '.' ;
5+ import { TopologyType } from '.' ;
66import { type BSONSerializeOptions , type Document , resolveBSONOptions } from './bson' ;
77import { ChangeStream , type ChangeStreamDocument , type ChangeStreamOptions } from './change_stream' ;
88import type { AutoEncrypter , AutoEncryptionOptions } from './client-side-encryption/auto_encrypter' ;
@@ -21,7 +21,6 @@ import {
2121 makeClientMetadata
2222} from './cmap/handshake/client_metadata' ;
2323import type { CompressorName } from './cmap/wire_protocol/compression' ;
24- import { MongoDBResponse } from './cmap/wire_protocol/responses' ;
2524import { parseOptions , resolveSRVRecord } from './connection_string' ;
2625import { MONGO_CLIENT_EVENTS } from './constants' ;
2726import { type AbstractCursor } from './cursor/abstract_cursor' ;
@@ -43,8 +42,8 @@ import {
4342 type ClientBulkWriteResult
4443} from './operations/client_bulk_write/common' ;
4544import { ClientBulkWriteExecutor } from './operations/client_bulk_write/executor' ;
45+ import { EndSessionsOperation } from './operations/end_sessions' ;
4646import { executeOperation } from './operations/execute_operation' ;
47- import { AbstractOperation } from './operations/operation' ;
4847import type { ReadConcern , ReadConcernLevel , ReadConcernLike } from './read_concern' ;
4948import { ReadPreference , type ReadPreferenceMode } from './read_preference' ;
5049import type { ServerMonitoringMode } from './sdam/monitor' ;
@@ -61,7 +60,7 @@ import {
6160 type HostAddress ,
6261 hostMatchesWildcards ,
6362 isHostMatch ,
64- MongoDBNamespace ,
63+ type MongoDBNamespace ,
6564 noop ,
6665 ns ,
6766 resolveOptions ,
@@ -763,40 +762,12 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
763762 return ;
764763 }
765764
766- // If we would attempt to select a server and get nothing back we short circuit
767- // to avoid the server selection timeout.
768- const selector = readPreferenceServerSelector ( ReadPreference . primaryPreferred ) ;
769- const topologyDescription = this . topology . description ;
770- const serverDescriptions = Array . from ( topologyDescription . servers . values ( ) ) ;
771- const servers = selector ( topologyDescription , serverDescriptions ) ;
772- if ( servers . length !== 0 ) {
773- const endSessions = Array . from ( this . s . sessionPool . sessions , ( { id } ) => id ) ;
774- if ( endSessions . length !== 0 ) {
775- try {
776- class EndSessionsOperation extends AbstractOperation < void > {
777- override ns = MongoDBNamespace . fromString ( 'admin.$cmd' ) ;
778- override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
779- override buildCommand ( _connection : Connection , _session ?: ClientSession ) : Document {
780- return {
781- endSessions
782- } ;
783- }
784- override buildOptions ( timeoutContext : TimeoutContext ) : ServerCommandOptions {
785- return {
786- timeoutContext,
787- readPreference : ReadPreference . primaryPreferred ,
788- noResponse : true
789- } ;
790- }
791- override get commandName ( ) : string {
792- return 'endSessions' ;
793- }
794- }
795- await executeOperation ( this , new EndSessionsOperation ( ) ) ;
796- } catch ( error ) {
797- squashError ( error ) ;
798- }
799- }
765+ const supportsSessions =
766+ this . topology . description . type === TopologyType . LoadBalanced ||
767+ this . topology . description . logicalSessionTimeoutMinutes != null ;
768+
769+ if ( supportsSessions ) {
770+ await endSessions ( this , this . topology ) ;
800771 }
801772
802773 // clear out references to old topology
@@ -809,6 +780,27 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
809780 if ( encrypter ) {
810781 await encrypter . close ( this ) ;
811782 }
783+
784+ async function endSessions (
785+ client : MongoClient ,
786+ { description : topologyDescription } : Topology
787+ ) {
788+ // If we would attempt to select a server and get nothing back we short circuit
789+ // to avoid the server selection timeout.
790+ const selector = readPreferenceServerSelector ( ReadPreference . primaryPreferred ) ;
791+ const serverDescriptions = Array . from ( topologyDescription . servers . values ( ) ) ;
792+ const servers = selector ( topologyDescription , serverDescriptions ) ;
793+ if ( servers . length !== 0 ) {
794+ const endSessions = Array . from ( client . s . sessionPool . sessions , ( { id } ) => id ) ;
795+ if ( endSessions . length !== 0 ) {
796+ try {
797+ await executeOperation ( client , new EndSessionsOperation ( endSessions ) ) ;
798+ } catch ( error ) {
799+ squashError ( error ) ;
800+ }
801+ }
802+ }
803+ }
812804 }
813805
814806 /**
0 commit comments