Skip to content

Commit 256b572

Browse files
committed
refactor(sessions): modify how we check for sessions support
The test for whether a deployment supports sessions is to check if `logicalSessionTimeoutMinutes` is set. For more complicated deployments (e.g. not single) this value is not determined by a single ismaster. As a result I've changed this to no longer be a part of our `ServerCapabilities` detection, but read right off the topology. NODE-1188
1 parent f8a5ebf commit 256b572

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

lib/mongo_client.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ MongoClient.prototype.startSession = function(options) {
486486
throw new MongoError('Must connect to a server before calling this method');
487487
}
488488

489-
const capabilities = this.topology.capabilities();
490-
if (capabilities && !capabilities.hasSessionSupport) {
489+
if (!this.topology.hasSessionSupport()) {
491490
throw new MongoError('Current topology does not support sessions');
492491
}
493492

lib/topologies/topology_base.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ var ServerCapabilities = function(ismaster) {
207207
var maxNumberOfDocsInBatch = ismaster.maxWriteBatchSize || 1000;
208208
var commandsTakeWriteConcern = false;
209209
var commandsTakeCollation = false;
210-
var sessionSupport = false;
211210

212211
if (ismaster.minWireVersion >= 0) {
213212
textSearch = true;
@@ -241,10 +240,6 @@ var ServerCapabilities = function(ismaster) {
241240
ismaster.maxWireVersion = 0;
242241
}
243242

244-
if (typeof ismaster.logicalSessionTimeoutMinutes !== 'undefined') {
245-
sessionSupport = true;
246-
}
247-
248243
// Map up read only parameters
249244
setup_get_property(this, 'hasAggregationCursor', aggregationCursor);
250245
setup_get_property(this, 'hasWriteCommands', writeCommands);
@@ -257,7 +252,6 @@ var ServerCapabilities = function(ismaster) {
257252
setup_get_property(this, 'maxNumberOfDocsInBatch', maxNumberOfDocsInBatch);
258253
setup_get_property(this, 'commandsTakeWriteConcern', commandsTakeWriteConcern);
259254
setup_get_property(this, 'commandsTakeCollation', commandsTakeCollation);
260-
setup_get_property(this, 'hasSessionSupport', sessionSupport);
261255
};
262256

263257
// Get package.json variable
@@ -288,10 +282,19 @@ class TopologyBase extends EventEmitter {
288282
};
289283
}
290284

285+
// Sessions related methods
286+
hasSessionSupport() {
287+
return this.logicalSessionTimeoutMinutes != null;
288+
}
289+
291290
startSession(options) {
292291
return new ClientSession(this, this.s.sessionPool, options);
293292
}
294293

294+
endSessions(sessions, callback) {
295+
return this.s.coreTopology.endSessions(sessions, callback);
296+
}
297+
295298
// Server capabilities
296299
capabilities() {
297300
if (this.s.sCapabilities) return this.s.sCapabilities;
@@ -351,10 +354,6 @@ class TopologyBase extends EventEmitter {
351354
return this.s.coreTopology.getServer(options);
352355
}
353356

354-
endSessions(sessions, callback) {
355-
return this.s.coreTopology.endSessions(sessions, callback);
356-
}
357-
358357
/**
359358
* Unref all sockets
360359
* @method

lib/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ const executeOperation = (topology, operation, args, options) => {
406406
// The driver sessions spec mandates that we implicitly create sessions for operations
407407
// that are not explicitly provided with a session.
408408
let session, opOptions;
409-
if (!options.skipSessions && topology.capabilities().hasSessionSupport) {
409+
if (!options.skipSessions && topology.hasSessionSupport()) {
410410
opOptions = args[args.length - 2];
411411
if (opOptions == null || opOptions.session == null) {
412412
session = topology.startSession();

0 commit comments

Comments
 (0)