Skip to content

Commit 6bacf92

Browse files
CDRIVER-3850 allow URI to accept timeoutMS
1 parent 203e6b9 commit 6bacf92

File tree

8 files changed

+470
-346
lines changed

8 files changed

+470
-346
lines changed

src/libmongoc/doc/mongoc_uri_t.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ MONGOC_URI_APPNAME appname Emp
9797
MONGOC_URI_TLS tls Empty (not set, same as false) {true|false}, indicating if TLS must be used. (See also :symbol:`mongoc_client_set_ssl_opts` and :symbol:`mongoc_client_pool_set_ssl_opts`.)
9898
MONGOC_URI_COMPRESSORS compressors Empty (no compressors) Comma separated list of compressors, if any, to use to compress the wire protocol messages. Snappy, zlib, and zstd are optional build time dependencies, and enable the "snappy", "zlib", and "zstd" values respectively.
9999
MONGOC_URI_CONNECTTIMEOUTMS connecttimeoutms 10,000 ms (10 seconds) This setting applies to new server connections. It is also used as the socket timeout for server discovery and monitoring operations.
100-
MONGOC_URI_SOCKETTIMEOUTMS sockettimeoutms 300,000 ms (5 minutes) The time in milliseconds to attempt to send or receive on a socket before the attempt times out.
100+
MONGOC_URI_SOCKETTIMEOUTMS sockettimeoutms 300,000 ms (5 minutes) Deprecated in favor of MONGOC_URI_TIMEOUTMS. The time in milliseconds to attempt to send or receive on a socket before the attempt times out.
101+
MONGOC_URI_TIMEOUTMS timeoutms Empty (no timeout) The time limit for the full execution of an operation.
101102
MONGOC_URI_REPLICASET replicaset Empty (no replicaset) The name of the Replica Set that the driver should connect to.
102103
MONGOC_URI_ZLIBCOMPRESSIONLEVEL zlibcompressionlevel -1 When the MONGOC_URI_COMPRESSORS includes "zlib" this options configures the zlib compression level, when the zlib compressor is used to compress client data.
103104
========================================== ================================= ================================= ============================================================================================================================================================================================================================================
@@ -193,7 +194,7 @@ MONGOC_URI_MAXPOOLSIZE maxpoolsize The
193194
MONGOC_URI_MINPOOLSIZE minpoolsize Deprecated. This option's behavior does not match its name, and its actual behavior will likely hurt performance.
194195
MONGOC_URI_MAXIDLETIMEMS maxidletimems Not implemented.
195196
MONGOC_URI_WAITQUEUEMULTIPLE waitqueuemultiple Not implemented.
196-
MONGOC_URI_WAITQUEUETIMEOUTMS waitqueuetimeoutms The maximum time to wait for a client to become available from the pool.
197+
MONGOC_URI_WAITQUEUETIMEOUTMS waitqueuetimeoutms Deprecated in favor of MONGOC_URI_TIMEOUTMS. The maximum time to wait for a client to become available from the pool.
197198
========================================== ================================= =========================================================================================================================================================================================================================
198199

199200
.. _mongoc_uri_t_write_concern_options:
@@ -211,7 +212,7 @@ MONGOC_URI_W w Det
211212
* majority = For replica sets, if you specify the special majority value to w option, write operations will only return successfully after a majority of the configured replica set members have acknowledged the write operation.
212213
* n = For replica sets, if you specify a number n greater than 1, operations with this write concern return only after n members of the set have acknowledged the write. If you set n to a number that is greater than the number of available set members or members that hold data, MongoDB will wait, potentially indefinitely, for these members to become available.
213214
* tags = For replica sets, you can specify a tag set to require that all members of the set that have these tags configured return confirmation of the write operation.
214-
MONGOC_URI_WTIMEOUTMS wtimeoutms The time in milliseconds to wait for replication to succeed, as specified in the w option, before timing out. When wtimeoutMS is 0, write operations will never time out.
215+
MONGOC_URI_WTIMEOUTMS wtimeoutms Deprecated in favor of MONGOC_URI_TIMEOUTMS. The time in milliseconds to wait for replication to succeed, as specified in the w option, before timing out. When wtimeoutMS is 0, write operations will never time out.
215216
MONGOC_URI_JOURNAL journal Controls whether write operations will wait until the mongod acknowledges the write operations and commits the data to the on disk journal.
216217

217218
* true = Enables journal commit acknowledgement write concern. Equivalent to specifying the getLastError command with the j option enabled.

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,7 @@ _mongoc_client_new_from_uri (mongoc_topology_t *topology)
11561156
BSON_ASSERT (mongoc_client_set_appname (client, appname));
11571157
}
11581158

1159-
/* TODO CDRIVER-3850 pull initial timeoutMS value from the URI */
1160-
client->timeout_ms = MONGOC_TIMEOUTMS_UNSET;
1159+
client->timeout_ms = mongoc_uri_get_option_as_int64 (client->uri, MONGOC_URI_TIMEOUTMS, MONGOC_TIMEOUTMS_UNSET);
11611160

11621161
mongoc_cluster_init (&client->cluster, client->uri, client);
11631162

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,21 +727,23 @@ mongoc_uri_option_is_int32 (const char *key)
727727
!strcasecmp (key, MONGOC_URI_HEARTBEATFREQUENCYMS) ||
728728
!strcasecmp (key, MONGOC_URI_SERVERSELECTIONTIMEOUTMS) ||
729729
!strcasecmp (key, MONGOC_URI_SOCKETCHECKINTERVALMS) ||
730-
!strcasecmp (key, MONGOC_URI_SOCKETTIMEOUTMS) ||
731730
!strcasecmp (key, MONGOC_URI_LOCALTHRESHOLDMS) ||
732731
!strcasecmp (key, MONGOC_URI_MAXPOOLSIZE) ||
733732
!strcasecmp (key, MONGOC_URI_MAXSTALENESSSECONDS) ||
734733
!strcasecmp (key, MONGOC_URI_MINPOOLSIZE) ||
735734
!strcasecmp (key, MONGOC_URI_MAXIDLETIMEMS) ||
736735
!strcasecmp (key, MONGOC_URI_WAITQUEUEMULTIPLE) ||
736+
!strcasecmp (key, MONGOC_URI_ZLIBCOMPRESSIONLEVEL) ||
737+
/* deprecated options */
737738
!strcasecmp (key, MONGOC_URI_WAITQUEUETIMEOUTMS) ||
738-
!strcasecmp (key, MONGOC_URI_ZLIBCOMPRESSIONLEVEL);
739+
!strcasecmp (key, MONGOC_URI_SOCKETTIMEOUTMS);
739740
}
740741

741742
bool
742743
mongoc_uri_option_is_int64 (const char *key)
743744
{
744-
return !strcasecmp (key, MONGOC_URI_WTIMEOUTMS);
745+
return !strcasecmp (key, MONGOC_URI_WTIMEOUTMS) ||
746+
!strcasecmp (key, MONGOC_URI_TIMEOUTMS);
745747
}
746748

747749
bool
@@ -1131,7 +1133,8 @@ mongoc_uri_apply_options (mongoc_uri_t *uri,
11311133
MONGOC_ERROR_COMMAND,
11321134
MONGOC_ERROR_COMMAND_INVALID_ARG,
11331135
"Failed to set %s to %d",
1134-
canon, bval);
1136+
canon,
1137+
bval);
11351138
return false;
11361139
}
11371140
} else {
@@ -2503,6 +2506,30 @@ mongoc_uri_get_option_as_int32 (const mongoc_uri_t *uri,
25032506
return (int32_t) retval;
25042507
}
25052508

2509+
2510+
static void
2511+
_mongoc_uri_warn_for_bad_int_option_combos (const mongoc_uri_t *uri,
2512+
const char *option)
2513+
{
2514+
/* Warn for deprecated option combinations */
2515+
if (!strcasecmp (option, MONGOC_URI_TIMEOUTMS)) {
2516+
if (mongoc_uri_get_option_as_int64 (uri, MONGOC_URI_WAITQUEUETIMEOUTMS, -1) > 0 ||
2517+
mongoc_uri_get_option_as_int64 (uri, MONGOC_URI_SOCKETTIMEOUTMS, -1) > 0 ||
2518+
mongoc_uri_get_option_as_int64 (uri, MONGOC_URI_WTIMEOUTMS, -1) > 0) {
2519+
MONGOC_WARNING ("Setting a deprecated timeout option %s in combination with timeoutMS", option);
2520+
}
2521+
}
2522+
2523+
if (!strcasecmp (option, MONGOC_URI_WAITQUEUETIMEOUTMS) ||
2524+
!strcasecmp (option, MONGOC_URI_SOCKETTIMEOUTMS) ||
2525+
!strcasecmp (option, MONGOC_URI_WTIMEOUTMS)) {
2526+
if (mongoc_uri_get_option_as_int64 (uri, MONGOC_URI_TIMEOUTMS, -1) > 0) {
2527+
MONGOC_WARNING ("Setting a deprecated timeout option %s in combination with timeoutMS", option);
2528+
}
2529+
}
2530+
}
2531+
2532+
25062533
/*
25072534
*--------------------------------------------------------------------------
25082535
*
@@ -2624,6 +2651,9 @@ _mongoc_uri_set_option_as_int32_with_error (mongoc_uri_t *uri,
26242651
return false;
26252652
}
26262653
}
2654+
2655+
_mongoc_uri_warn_for_bad_int_option_combos (uri, option);
2656+
26272657
option_lowercase = lowercase_str_new (option);
26282658
if (!bson_append_int32 (&uri->options, option_lowercase, -1, value)) {
26292659
bson_free (option_lowercase);
@@ -2817,6 +2847,16 @@ _mongoc_uri_set_option_as_int64_with_error (mongoc_uri_t *uri,
28172847

28182848
option = mongoc_uri_canonicalize_option (option_orig);
28192849

2850+
/* timeoutMS may not be a negative number. */
2851+
if (!bson_strcasecmp (option, MONGOC_URI_TIMEOUTMS) && value < 0) {
2852+
MONGOC_URI_ERROR (
2853+
error,
2854+
"Invalid \"%s\" of %lld: must be a non-negative integer",
2855+
option_orig,
2856+
value);
2857+
return false;
2858+
}
2859+
28202860
if ((options = mongoc_uri_get_options (uri)) &&
28212861
bson_iter_init_find_case (&iter, options, option)) {
28222862
if (BSON_ITER_HOLDS_INT64 (&iter)) {
@@ -2833,6 +2873,8 @@ _mongoc_uri_set_option_as_int64_with_error (mongoc_uri_t *uri,
28332873
}
28342874
}
28352875

2876+
_mongoc_uri_warn_for_bad_int_option_combos (uri, option);
2877+
28362878
option_lowercase = lowercase_str_new (option);
28372879
if (!bson_append_int64 (&uri->options, option_lowercase, -1, value)) {
28382880
bson_free (option_lowercase);

src/libmongoc/src/mongoc/mongoc-uri.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#define MONGOC_URI_SLAVEOK "slaveok"
6464
#define MONGOC_URI_SOCKETCHECKINTERVALMS "socketcheckintervalms"
6565
#define MONGOC_URI_SOCKETTIMEOUTMS "sockettimeoutms"
66+
#define MONGOC_URI_TIMEOUTMS "timeoutms"
6667
#define MONGOC_URI_TLS "tls"
6768
#define MONGOC_URI_TLSCERTIFICATEKEYFILE "tlscertificatekeyfile"
6869
#define MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD "tlscertificatekeyfilepassword"

src/libmongoc/tests/test-mongoc-client.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,12 +3860,14 @@ test_mongoc_client_timeout_ms (void)
38603860

38613861
client = mongoc_client_new ("mongodb://localhost");
38623862

3863-
/* no timeout returns -1 */
3863+
/* No timeout returns -1 */
38643864
BSON_ASSERT (mongoc_client_get_timeout_ms (client) ==
38653865
MONGOC_TIMEOUTMS_UNSET);
3866+
mongoc_client_destroy (client);
38663867

3867-
/* TODO CDRIVER-3850 implement additional tests once timeout can be set via
3868-
* URI */
3868+
/* Client gets timeout through URI */
3869+
client = mongoc_client_new ("mongodb://localhost/?timeoutms=100");
3870+
BSON_ASSERT (mongoc_client_get_timeout_ms (client) == 100);
38693871

38703872
mongoc_client_destroy (client);
38713873
}

src/libmongoc/tests/test-mongoc-collection.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6407,21 +6407,17 @@ test_fam_no_error_on_retry (void *unused)
64076407
static void
64086408
test_timeout_ms (void)
64096409
{
6410-
mongoc_client_t *client = mongoc_client_new ("mongodb://host/db_name");
6410+
mongoc_client_t *client = mongoc_client_new ("mongodb://localhost/?timeoutms=100");
64116411
mongoc_database_t *db = NULL;
64126412
mongoc_collection_t *coll =
64136413
mongoc_client_get_collection (client, "db", "test");
64146414
mongoc_collection_t *coll2 = NULL;
64156415
bool res;
64166416
bson_error_t error;
64176417

6418-
/* TODO CDRIVER-3850 no timeout MS returns client's timeout */
6419-
/* BSON_ASSERT (mongoc_client_set_timeout_ms (client, 10, &error));
6420-
ASSERT_CMPINT (mongoc_collection_get_timeout_ms (coll), ==,
6421-
mongoc_client_get_timeout_ms (client)); */
6422-
6423-
BSON_ASSERT (mongoc_collection_get_timeout_ms (coll) ==
6424-
MONGOC_TIMEOUTMS_UNSET);
6418+
/* no timeoutMS returns client's timeoutMS */
6419+
ASSERT_CMPINT (mongoc_collection_get_timeout_ms (coll), ==,
6420+
mongoc_client_get_timeout_ms (client));
64256421

64266422
/* negative timeouts are invalid */
64276423
res = mongoc_collection_set_timeout_ms (coll, -1, &error);

src/libmongoc/tests/test-mongoc-database.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,17 +1206,14 @@ test_get_default_database (void)
12061206
static void
12071207
test_timeout_ms (void)
12081208
{
1209-
mongoc_client_t *client = mongoc_client_new ("mongodb://host/db_name");
1210-
mongoc_database_t *db = mongoc_client_get_default_database (client);
1209+
mongoc_client_t *client = mongoc_client_new ("mongodb://localhost/?timeoutms=100");
1210+
mongoc_database_t *db = mongoc_client_get_database (client, "test");
12111211
bool res;
12121212
bson_error_t error;
12131213

1214-
/* TODO CDRIVER-3850 no timeout MS returns client's timeout */
1215-
/* BSON_ASSERT (mongoc_client_set_timeout_ms (client, 10, &error));
1216-
ASSERT_CMPINT (mongoc_database_get_timeout_ms (db), ==,
1217-
mongoc_client_get_timeout_ms (client));*/
1218-
1219-
BSON_ASSERT (mongoc_database_get_timeout_ms (db) == MONGOC_TIMEOUTMS_UNSET);
1214+
/* no timeoutMS returns client's timeoutMS */
1215+
ASSERT_CMPINT (mongoc_database_get_timeout_ms (db), ==,
1216+
mongoc_client_get_timeout_ms (client));
12201217

12211218
/* negative timeouts are invalid */
12221219
res = mongoc_database_set_timeout_ms (db, -1, &error);

0 commit comments

Comments
 (0)