122
122
* <li>{@code sslInvalidHostNameAllowed=true|false}: Whether to allow invalid host names for TLS connections.</li>
123
123
* <li>{@code tlsAllowInvalidHostnames=true|false}: Whether to allow invalid host names for TLS connections. Supersedes the
124
124
* sslInvalidHostNameAllowed option</li>
125
+ * <li>{@code timeoutMS=ms}: Time limit for the full execution of an operation.</li>
125
126
* <li>{@code connectTimeoutMS=ms}: How long a connection can take to be opened before timing out.</li>
126
127
* <li>{@code socketTimeoutMS=ms}: How long a receive on a socket can take before timing out.
127
- * This option is the same as {@link SocketSettings#getReadTimeout(TimeUnit)}.</li>
128
+ * This option is the same as {@link SocketSettings#getReadTimeout(TimeUnit)}.
129
+ * Deprecated, use {@code timeoutMS} instead.</li>
128
130
* <li>{@code maxIdleTimeMS=ms}: Maximum idle time of a pooled connection. A connection that exceeds this limit will be closed</li>
129
131
* <li>{@code maxLifeTimeMS=ms}: Maximum life time of a pooled connection. A connection that exceeds this limit will be closed</li>
130
132
* </ul>
133
135
* <li>{@code maxPoolSize=n}: The maximum number of connections in the connection pool.</li>
134
136
* <li>{@code minPoolSize=n}: The minimum number of connections in the connection pool.</li>
135
137
* <li>{@code waitQueueTimeoutMS=ms}: The maximum wait time in milliseconds that a thread may wait for a connection to
136
- * become available.</li>
138
+ * become available. Deprecated, use {@code timeoutMS} instead. </li>
137
139
* <li>{@code maxConnecting=n}: The maximum number of connections a pool may be establishing concurrently.</li>
138
140
* </ul>
139
141
* <p>Write concern configuration:</p>
161
163
* <li>{@code wtimeoutMS=ms}
162
164
* <ul>
163
165
* <li>The driver adds { wtimeout : ms } to all write commands. Implies {@code safe=true}.</li>
164
- * <li>Used in combination with {@code w}</li>
166
+ * <li>Used in combination with {@code w}. Deprecated, use {@code timeoutMS} instead </li>
165
167
* </ul>
166
168
* </li>
167
169
* </ul>
@@ -280,6 +282,7 @@ public class ConnectionString {
280
282
private Integer maxConnectionLifeTime ;
281
283
private Integer maxConnecting ;
282
284
private Integer connectTimeout ;
285
+ private Long timeout ;
283
286
private Integer socketTimeout ;
284
287
private Boolean sslEnabled ;
285
288
private Boolean sslInvalidHostnameAllowed ;
@@ -463,6 +466,7 @@ public ConnectionString(final String connectionString, @Nullable final DnsClient
463
466
464
467
credential = createCredentials (combinedOptionsMaps , userName , password );
465
468
warnOnUnsupportedOptions (combinedOptionsMaps );
469
+ warnDeprecatedTimeouts (combinedOptionsMaps );
466
470
}
467
471
468
472
private static final Set <String > GENERAL_OPTIONS_KEYS = new LinkedHashSet <>();
@@ -471,16 +475,18 @@ public ConnectionString(final String connectionString, @Nullable final DnsClient
471
475
private static final Set <String > WRITE_CONCERN_KEYS = new HashSet <>();
472
476
private static final Set <String > COMPRESSOR_KEYS = new HashSet <>();
473
477
private static final Set <String > ALL_KEYS = new HashSet <>();
478
+ private static final Set <String > DEPRECATED_TIMEOUT_KEYS = new HashSet <>();
474
479
475
480
static {
476
481
GENERAL_OPTIONS_KEYS .add ("minpoolsize" );
477
482
GENERAL_OPTIONS_KEYS .add ("maxpoolsize" );
483
+ GENERAL_OPTIONS_KEYS .add ("timeoutms" );
484
+ GENERAL_OPTIONS_KEYS .add ("sockettimeoutms" );
478
485
GENERAL_OPTIONS_KEYS .add ("waitqueuetimeoutms" );
479
486
GENERAL_OPTIONS_KEYS .add ("connecttimeoutms" );
480
487
GENERAL_OPTIONS_KEYS .add ("maxidletimems" );
481
488
GENERAL_OPTIONS_KEYS .add ("maxlifetimems" );
482
489
GENERAL_OPTIONS_KEYS .add ("maxconnecting" );
483
- GENERAL_OPTIONS_KEYS .add ("sockettimeoutms" );
484
490
485
491
// Order matters here: Having tls after ssl means than the tls option will supersede the ssl option when both are set
486
492
GENERAL_OPTIONS_KEYS .add ("ssl" );
@@ -536,6 +542,10 @@ public ConnectionString(final String connectionString, @Nullable final DnsClient
536
542
ALL_KEYS .addAll (READ_PREFERENCE_KEYS );
537
543
ALL_KEYS .addAll (WRITE_CONCERN_KEYS );
538
544
ALL_KEYS .addAll (COMPRESSOR_KEYS );
545
+
546
+ DEPRECATED_TIMEOUT_KEYS .add ("sockettimeoutms" );
547
+ DEPRECATED_TIMEOUT_KEYS .add ("waitqueuetimeoutms" );
548
+ DEPRECATED_TIMEOUT_KEYS .add ("wtimeoutms" );
539
549
}
540
550
541
551
// Any options contained in the connection string completely replace the corresponding options specified in TXT records,
@@ -549,15 +559,23 @@ private Map<String, List<String>> combineOptionsMaps(final Map<String, List<Stri
549
559
550
560
551
561
private void warnOnUnsupportedOptions (final Map <String , List <String >> optionsMap ) {
552
- for (final String key : optionsMap .keySet ()) {
553
- if (!ALL_KEYS .contains (key )) {
554
- if (LOGGER .isWarnEnabled ()) {
555
- LOGGER .warn (format ("Connection string contains unsupported option '%s'." , key ));
556
- }
557
- }
562
+ if (LOGGER .isWarnEnabled ()) {
563
+ optionsMap .keySet ()
564
+ .stream ()
565
+ .filter (k -> !ALL_KEYS .contains (k ))
566
+ .forEach (k -> LOGGER .warn (format ("Connection string contains unsupported option '%s'." , k )));
567
+ }
568
+ }
569
+ private void warnDeprecatedTimeouts (final Map <String , List <String >> optionsMap ) {
570
+ if (LOGGER .isWarnEnabled ()) {
571
+ optionsMap .keySet ()
572
+ .stream ()
573
+ .filter (DEPRECATED_TIMEOUT_KEYS ::contains )
574
+ .forEach (k -> LOGGER .warn (format ("Use of deprecated timeout option: '%s'. Prefer 'timeoutMS' instead." , k )));
558
575
}
559
576
}
560
577
578
+
561
579
private void translateOptions (final Map <String , List <String >> optionsMap ) {
562
580
boolean tlsInsecureSet = false ;
563
581
boolean tlsAllowInvalidHostnamesSet = false ;
@@ -592,6 +610,9 @@ private void translateOptions(final Map<String, List<String>> optionsMap) {
592
610
case "sockettimeoutms" :
593
611
socketTimeout = parseInteger (value , "sockettimeoutms" );
594
612
break ;
613
+ case "timeoutms" :
614
+ timeout = parseLong (value , "timeoutms" );
615
+ break ;
595
616
case "tlsallowinvalidhostnames" :
596
617
sslInvalidHostnameAllowed = parseBoolean (value , "tlsAllowInvalidHostnames" );
597
618
tlsAllowInvalidHostnamesSet = true ;
@@ -1100,6 +1121,15 @@ private int parseInteger(final String input, final String key) {
1100
1121
}
1101
1122
}
1102
1123
1124
+ private long parseLong (final String input , final String key ) {
1125
+ try {
1126
+ return Long .parseLong (input );
1127
+ } catch (NumberFormatException e ) {
1128
+ throw new IllegalArgumentException (format ("The connection string contains an invalid value for '%s'. "
1129
+ + "'%s' is not a valid long" , key , input ));
1130
+ }
1131
+ }
1132
+
1103
1133
private List <String > parseHosts (final List <String > rawHosts ) {
1104
1134
if (rawHosts .size () == 0 ){
1105
1135
throw new IllegalArgumentException ("The connection string must contain at least one host" );
@@ -1412,6 +1442,37 @@ public Integer getMaxConnecting() {
1412
1442
return maxConnecting ;
1413
1443
}
1414
1444
1445
+ /**
1446
+ * The time limit for the full execution of an operation in milliseconds.
1447
+ *
1448
+ * <p>If set the following deprecated options will be ignored:
1449
+ * {@code waitQueueTimeoutMS}, {@code socketTimeoutMS}, {@code wTimeoutMS}, {@code maxTimeMS} and {@code maxCommitTimeMS}</p>
1450
+ *
1451
+ * <ul>
1452
+ * <li>{@code null} means that the timeout mechanism for operations will defer to using:
1453
+ * <ul>
1454
+ * <li>{@code waitQueueTimeoutMS}: The maximum wait time in milliseconds that a thread may wait for a connection to become
1455
+ * available</li>
1456
+ * <li>{@code socketTimeoutMS}: How long a send or receive on a socket can take before timing out.</li>
1457
+ * <li>{@code wTimeoutMS}: How long the server will wait for the write concern to be fulfilled before timing out.</li>
1458
+ * <li>{@code maxTimeMS}: The cumulative time limit for processing operations on a cursor.
1459
+ * See: <a href="https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS">cursor.maxTimeMS</a>.</li>
1460
+ * <li>{@code maxCommitTimeMS}: The maximum amount of time to allow a single {@code commitTransaction} command to execute.
1461
+ * See: {@link TransactionOptions#getMaxCommitTime}.</li>
1462
+ * </ul>
1463
+ * </li>
1464
+ * <li>{@code 0} means infinite timeout.</li>
1465
+ * <li>{@code > 0} The time limit to use for the full execution of an operation.</li>
1466
+ * </ul>
1467
+ *
1468
+ * @return the time limit for the full execution of an operation in milliseconds or null.
1469
+ * @since 4.x
1470
+ */
1471
+ @ Nullable
1472
+ public Long getTimeout () {
1473
+ return timeout ;
1474
+ }
1475
+
1415
1476
/**
1416
1477
* Gets the socket connect timeout specified in the connection string.
1417
1478
* @return the socket connect timeout
@@ -1559,6 +1620,7 @@ public boolean equals(final Object o) {
1559
1620
&& Objects .equals (maxConnectionLifeTime , that .maxConnectionLifeTime )
1560
1621
&& Objects .equals (maxConnecting , that .maxConnecting )
1561
1622
&& Objects .equals (connectTimeout , that .connectTimeout )
1623
+ && Objects .equals (timeout , that .timeout )
1562
1624
&& Objects .equals (socketTimeout , that .socketTimeout )
1563
1625
&& Objects .equals (sslEnabled , that .sslEnabled )
1564
1626
&& Objects .equals (sslInvalidHostnameAllowed , that .sslInvalidHostnameAllowed )
@@ -1577,7 +1639,7 @@ public boolean equals(final Object o) {
1577
1639
public int hashCode () {
1578
1640
return Objects .hash (credential , isSrvProtocol , hosts , database , collection , directConnection , readPreference ,
1579
1641
writeConcern , retryWrites , retryReads , readConcern , minConnectionPoolSize , maxConnectionPoolSize , maxWaitTime ,
1580
- maxConnectionIdleTime , maxConnectionLifeTime , maxConnecting , connectTimeout , socketTimeout , sslEnabled ,
1642
+ maxConnectionIdleTime , maxConnectionLifeTime , maxConnecting , connectTimeout , timeout , socketTimeout , sslEnabled ,
1581
1643
sslInvalidHostnameAllowed , requiredReplicaSetName , serverSelectionTimeout , localThreshold , heartbeatFrequency ,
1582
1644
applicationName , compressorList , uuidRepresentation , srvServiceName , srvMaxHosts );
1583
1645
}
0 commit comments