Skip to content

Commit be0d36e

Browse files
committed
refactor: explicitly disallow selection if topology is closed
This is not our eventual desire, but until we can remove the concept of `connect`, we need to abide by the rules of it.
1 parent f07a03f commit be0d36e

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

lib/core/sdam/monitoring.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ function monitorServer(server, options) {
130130
options = options || {};
131131
const heartbeatFrequencyMS = options.heartbeatFrequencyMS || 10000;
132132

133+
if (options.initial === true) {
134+
server.s.monitorId = setTimeout(() => monitorServer(server), heartbeatFrequencyMS);
135+
return;
136+
}
137+
133138
const rescheduleMonitoring = () => {
134139
server.s.monitoring = false;
135140
server.s.monitorId = setTimeout(() => {

lib/core/sdam/server_selection.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const calculateDurationInMs = require('../utils').calculateDurationInMs;
77
const MongoTimeoutError = require('../error').MongoTimeoutError;
88

99
const common = require('./common');
10-
const STATE_CONNECTED = common.STATE_CONNECTED;
11-
const STATE_CONNECTING = common.STATE_CONNECTING;
10+
const STATE_CLOSED = common.STATE_CLOSED;
1211
const TOPOLOGY_DEFAULTS = common.TOPOLOGY_DEFAULTS;
1312
const drainTimerQueue = common.drainTimerQueue;
1413
const clearAndRemoveTimerFrom = common.clearAndRemoveTimerFrom;
@@ -266,26 +265,9 @@ function selectServers(topology, selector, timeout, start, callback) {
266265
);
267266
}
268267

269-
// ensure we are connected
270-
if (topology.s.state !== STATE_CONNECTED && topology.s.state !== STATE_CONNECTING) {
271-
topology.connect();
272-
273-
// we want to make sure we're still within the requested timeout window
274-
const failToConnectTimer = setTimeout(() => {
275-
topology.removeListener('connect', connectHandler);
276-
callback(
277-
new MongoTimeoutError('Server selection timed out waiting to connect'),
278-
topology.description.error
279-
);
280-
}, timeout - duration);
281-
282-
const connectHandler = () => {
283-
clearAndRemoveTimerFrom(failToConnectTimer, topology.s.connectionTimers);
284-
selectServers(topology, selector, timeout, process.hrtime(), callback);
285-
};
286-
287-
topology.s.connectionTimers.add(failToConnectTimer);
288-
topology.once('connect', connectHandler);
268+
// explicitly disallow selection if client is closed
269+
if (topology.s.state === STATE_CLOSED) {
270+
callback(new MongoError('Topology is closed, please connect'));
289271
return;
290272
}
291273

test/unit/sdam/server_selection/select_servers_tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,19 @@ describe('selectServers', function() {
7171
});
7272
});
7373
});
74+
75+
it('should disallow selection when the topology is explicitly closed', function(done) {
76+
const topology = new Topology('someserver:27019');
77+
this.sinon.stub(Server.prototype, 'connect').callsFake(function() {
78+
this.emit('connect');
79+
});
80+
81+
topology.close(() => {
82+
selectServers(topology, ReadPreference.primary, 2000, process.hrtime(), err => {
83+
expect(err).to.exist;
84+
expect(err).to.match(/Topology is closed/);
85+
done();
86+
});
87+
});
88+
});
7489
});

0 commit comments

Comments
 (0)