@@ -40,6 +40,8 @@ public class TimeoutContext {
40
40
41
41
@ Nullable
42
42
private Timeout timeout ;
43
+ @ Nullable
44
+ private Timeout computedServerSelectionTimeout ;
43
45
private long minRoundTripTimeMS = 0 ;
44
46
45
47
public static MongoOperationTimeoutException createMongoTimeoutException () {
@@ -191,14 +193,17 @@ public long getWriteTimeoutMS() {
191
193
return timeoutOrAlternative (0 );
192
194
}
193
195
196
+ public int getConnectTimeoutMs () {
197
+ return (int ) calculateMin (getTimeoutSettings ().getConnectTimeoutMS ());
198
+ }
194
199
195
200
public void resetTimeout () {
196
201
assertNotNull (timeout );
197
202
timeout = calculateTimeout (timeoutSettings .getTimeoutMS ());
198
203
}
199
204
200
205
/**
201
- * Resest the timeout if this timeout context is being used by pool maintenance
206
+ * Resets the timeout if this timeout context is being used by pool maintenance
202
207
*/
203
208
public void resetMaintenanceTimeout () {
204
209
if (isMaintenanceContext && timeout != null && !timeout .isInfinite ()) {
@@ -266,22 +271,44 @@ public static Timeout calculateTimeout(@Nullable final Long timeoutMS) {
266
271
}
267
272
268
273
public Timeout computedServerSelectionTimeout () {
269
- long ms = getTimeoutSettings ().getServerSelectionTimeoutMS ();
270
- Timeout serverSelectionTimeout = StartTime .now ().timeoutAfterOrInfiniteIfNegative (ms , MILLISECONDS );
271
- return serverSelectionTimeout .orEarlier (timeout );
274
+ if (computedServerSelectionTimeout == null ) {
275
+ computedServerSelectionTimeout = StartTime .now ()
276
+ .timeoutAfterOrInfiniteIfNegative (getTimeoutSettings ().getServerSelectionTimeoutMS (), MILLISECONDS )
277
+ .orEarlier (timeout );
278
+ }
279
+ return computedServerSelectionTimeout ;
280
+ }
281
+
282
+ /**
283
+ * Returns the timeout context to use for the handshake process
284
+ *
285
+ * <p>Uses the current context if:
286
+ * <ul>
287
+ * <li>If has no timeoutMS, keep the same legacy behavior.</li>
288
+ * <li>If in a MaintenanceContext</li>
289
+ * <li>If there is no computedServerSelectionTimeout.q</li>
290
+ * </ul>
291
+ *
292
+ * @return the timeout context
293
+ */
294
+ public TimeoutContext withComputedServerSelectionTimeoutContext () {
295
+ TimeoutContext timeoutContext = this ;
296
+ if (!hasTimeoutMS () && !isMaintenanceContext && computedServerSelectionTimeout != null ) {
297
+ timeoutContext = Objects .equals (computedServerSelectionTimeout , timeout )
298
+ ? this : new TimeoutContext (false , timeoutSettings , computedServerSelectionTimeout );
299
+ }
300
+ computedServerSelectionTimeout = null ;
301
+ return timeoutContext ;
272
302
}
273
303
274
304
public Timeout startWaitQueueTimeout (final StartTime checkoutStart ) {
275
305
final long ms = getTimeoutSettings ().getMaxWaitTimeMS ();
276
306
return checkoutStart .timeoutAfterOrInfiniteIfNegative (ms , MILLISECONDS );
277
307
}
278
308
279
- public int getConnectTimeoutMs () {
280
- return (int ) getTimeoutSettings ().getConnectTimeoutMS ();
281
- }
282
-
283
309
@ Nullable
284
310
public Timeout getTimeout () {
285
311
return timeout ;
286
312
}
313
+
287
314
}
0 commit comments