@@ -7,8 +7,7 @@ const calculateDurationInMs = require('../utils').calculateDurationInMs;
7
7
const MongoTimeoutError = require ( '../error' ) . MongoTimeoutError ;
8
8
9
9
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 ;
12
11
const TOPOLOGY_DEFAULTS = common . TOPOLOGY_DEFAULTS ;
13
12
const drainTimerQueue = common . drainTimerQueue ;
14
13
const clearAndRemoveTimerFrom = common . clearAndRemoveTimerFrom ;
@@ -266,26 +265,9 @@ function selectServers(topology, selector, timeout, start, callback) {
266
265
) ;
267
266
}
268
267
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' ) ) ;
289
271
return ;
290
272
}
291
273
0 commit comments