@@ -237,14 +237,9 @@ public void openAsync(final OperationContext operationContext, final AsyncComple
237
237
socketChannel , () -> initializeTslChannel (handler , socketChannel ));
238
238
239
239
int connectTimeoutMs = getSettings ().getConnectTimeout (TimeUnit .MILLISECONDS );
240
-
241
- group .getTimeoutExecutor ().schedule (() -> {
242
- boolean markedTimedOut = socketRegistration .markConnectionEstablishmentTimedOut ();
243
- if (markedTimedOut ) {
244
- closeAndTimeout (handler , socketChannel );
245
- }
246
- }, connectTimeoutMs , TimeUnit .MILLISECONDS );
247
-
240
+ if (connectTimeoutMs > 0 ) {
241
+ scheduleTimeoutInterruption (handler , socketRegistration , socketChannel , connectTimeoutMs );
242
+ }
248
243
selectorMonitor .register (socketRegistration );
249
244
} catch (IOException e ) {
250
245
handler .failed (new MongoSocketOpenException ("Exception opening socket" , getServerAddress (), e ));
@@ -253,6 +248,18 @@ public void openAsync(final OperationContext operationContext, final AsyncComple
253
248
}
254
249
}
255
250
251
+ private void scheduleTimeoutInterruption (final AsyncCompletionHandler <Void > handler ,
252
+ final SelectorMonitor .SocketRegistration socketRegistration ,
253
+ final SocketChannel socketChannel ,
254
+ final int connectTimeoutMs ) {
255
+ group .getTimeoutExecutor ().schedule (() -> {
256
+ boolean markedTimedOut = socketRegistration .markConnectionEstablishmentTimedOut ();
257
+ if (markedTimedOut ) {
258
+ closeAndTimeout (handler , socketChannel );
259
+ }
260
+ }, connectTimeoutMs , TimeUnit .MILLISECONDS );
261
+ }
262
+
256
263
private void closeAndTimeout (final AsyncCompletionHandler <Void > handler , final SocketChannel socketChannel ) {
257
264
// We check if this stream was closed before timeout exception.
258
265
boolean streamClosed = isClosed ();
0 commit comments