@@ -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,47 @@ 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 in a maintenance context</li>
288
+ * <li>If has no timeoutMS, so to keep the same legacy behavior.</li>
289
+ * <li>If there is no computedServerSelectionTimeout.</li>
290
+ * <li>If the computedServerSelectionTimeout is the same as the timeout.</li>
291
+ * </ul>
292
+ *
293
+ * @return the timeout context
294
+ */
295
+ public TimeoutContext withComputedServerSelectionTimeoutContext () {
296
+ if (isMaintenanceContext || !hasTimeoutMS () || computedServerSelectionTimeout == null
297
+ || Objects .equals (computedServerSelectionTimeout , timeout )) {
298
+ computedServerSelectionTimeout = null ;
299
+ return this ;
300
+ }
301
+
302
+ TimeoutContext timeoutContext = new TimeoutContext (false , timeoutSettings , computedServerSelectionTimeout );
303
+ computedServerSelectionTimeout = null ;
304
+ return timeoutContext ;
272
305
}
273
306
274
307
public Timeout startWaitQueueTimeout (final StartTime checkoutStart ) {
275
308
final long ms = getTimeoutSettings ().getMaxWaitTimeMS ();
276
309
return checkoutStart .timeoutAfterOrInfiniteIfNegative (ms , MILLISECONDS );
277
310
}
278
311
279
- public int getConnectTimeoutMs () {
280
- return (int ) getTimeoutSettings ().getConnectTimeoutMS ();
281
- }
282
-
283
312
@ Nullable
284
313
public Timeout getTimeout () {
285
314
return timeout ;
286
315
}
316
+
287
317
}
0 commit comments