Skip to content

Commit ccbfd55

Browse files
fix lb mode
1 parent 97470e4 commit ccbfd55

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/mongo_client.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
22
import type { TcpNetConnectOpts } from 'net';
33
import type { ConnectionOptions as TLSConnectionOptions, TLSSocketOptions } from 'tls';
44

5-
import { type ServerCommandOptions, type TimeoutContext } from '.';
5+
import { type ServerCommandOptions, type TimeoutContext, TopologyType } from '.';
66
import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './bson';
77
import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream';
88
import type { AutoEncrypter, AutoEncryptionOptions } from './client-side-encryption/auto_encrypter';
@@ -795,11 +795,11 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
795795
return;
796796
}
797797

798-
const topologyDescription = this.topology.description;
799-
const areSessionsSupported = topologyDescription.logicalSessionTimeoutMinutes != null;
800-
801-
if (areSessionsSupported) {
802-
await endSessions(this);
798+
const {
799+
description: { type, logicalSessionTimeoutMinutes }
800+
} = this.topology;
801+
if (type === TopologyType.LoadBalanced || logicalSessionTimeoutMinutes != null) {
802+
await endSessions(this, this.topology);
803803
}
804804

805805
// clear out references to old topology
@@ -813,7 +813,10 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
813813
await encrypter.close(this);
814814
}
815815

816-
async function endSessions(client: MongoClient) {
816+
async function endSessions(
817+
client: MongoClient,
818+
{ description: topologyDescription }: Topology
819+
) {
817820
// If we would attempt to select a server and get nothing back we short circuit
818821
// to avoid the server selection timeout.
819822
const selector = readPreferenceServerSelector(ReadPreference.primaryPreferred);

src/sdam/topology.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
393393
return this.s.options.serverApi;
394394
}
395395

396+
get supportsSessions(): boolean {
397+
return (
398+
this.description.type === TopologyType.LoadBalanced ||
399+
this.description.logicalSessionTimeoutMinutes != null
400+
);
401+
}
402+
396403
/** Initiate server connect */
397404
async connect(options?: ConnectOptions): Promise<Topology> {
398405
this.connectionLock ??= this._connect(options);

0 commit comments

Comments
 (0)