@@ -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 () {
@@ -183,14 +185,17 @@ public long getWriteTimeoutMS() {
183
185
return timeoutOrAlternative (0 );
184
186
}
185
187
188
+ public int getConnectTimeoutMs () {
189
+ return (int ) calculateMin (getTimeoutSettings ().getConnectTimeoutMS ());
190
+ }
186
191
187
192
public void resetTimeout () {
188
193
assertNotNull (timeout );
189
194
timeout = calculateTimeout (timeoutSettings .getTimeoutMS ());
190
195
}
191
196
192
197
/**
193
- * Resest the timeout if this timeout context is being used by pool maintenance
198
+ * Resets the timeout if this timeout context is being used by pool maintenance
194
199
*/
195
200
public void resetMaintenanceTimeout () {
196
201
if (isMaintenanceContext && timeout != null && !timeout .isInfinite ()) {
@@ -258,17 +263,40 @@ public static Timeout calculateTimeout(@Nullable final Long timeoutMS) {
258
263
}
259
264
260
265
public Timeout computedServerSelectionTimeout () {
261
- long ms = getTimeoutSettings ().getServerSelectionTimeoutMS ();
262
- Timeout serverSelectionTimeout = StartTime .now ().timeoutAfterOrInfiniteIfNegative (ms , MILLISECONDS );
263
- return serverSelectionTimeout .orEarlier (timeout );
266
+ if (computedServerSelectionTimeout == null ) {
267
+ computedServerSelectionTimeout = StartTime .now ()
268
+ .timeoutAfterOrInfiniteIfNegative (getTimeoutSettings ().getServerSelectionTimeoutMS (), MILLISECONDS )
269
+ .orEarlier (timeout );
270
+ }
271
+ return computedServerSelectionTimeout ;
272
+ }
273
+
274
+ /**
275
+ * Returns the timeout context to use for the handshake process
276
+ *
277
+ * <p>Uses the current context if:
278
+ * <ul>
279
+ * <li>If has no timeoutMS, keep the same legacy behavior.</li>
280
+ * <li>If in a MaintenanceContext</li>
281
+ * <li>If there is no computedServerSelectionTimeout.q</li>
282
+ * </ul>
283
+ *
284
+ * @return the timeout context
285
+ */
286
+ public TimeoutContext withComputedServerSelectionTimeoutContext () {
287
+ TimeoutContext timeoutContext = this ;
288
+ if (!hasTimeoutMS () && ! isMaintenanceContext && computedServerSelectionTimeout != null ) {
289
+ timeoutContext = Objects .equals (computedServerSelectionTimeout , timeout )
290
+ ? this : new TimeoutContext (false , timeoutSettings , computedServerSelectionTimeout );
291
+ }
292
+ computedServerSelectionTimeout = null ;
293
+ return timeoutContext ;
264
294
}
265
295
266
296
public Timeout startWaitQueueTimeout (final StartTime checkoutStart ) {
267
297
final long ms = getTimeoutSettings ().getMaxWaitTimeMS ();
268
298
return checkoutStart .timeoutAfterOrInfiniteIfNegative (ms , MILLISECONDS );
269
299
}
270
300
271
- public int getConnectTimeoutMs () {
272
- return (int ) getTimeoutSettings ().getConnectTimeoutMS ();
273
- }
301
+
274
302
}
0 commit comments