Skip to content

Commit dcff494

Browse files
committed
CDRIVER-2056: Parse zero values for localThresholdMS URI option
1 parent 7dd7285 commit dcff494

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

src/mongoc/mongoc-topology.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mongoc-topology-private.h"
2525
#include "mongoc-topology-description-apm-private.h"
2626
#include "mongoc-client-private.h"
27+
#include "mongoc-uri-private.h"
2728
#include "mongoc-util-private.h"
2829

2930
#include "utlist.h"
@@ -253,8 +254,7 @@ mongoc_topology_new (const mongoc_uri_t *uri, bool single_threaded)
253254
"serverselectiontimeoutms",
254255
MONGOC_TOPOLOGY_SERVER_SELECTION_TIMEOUT_MS);
255256

256-
topology->local_threshold_msec = mongoc_uri_get_option_as_int32 (
257-
topology->uri, "localthresholdms", MONGOC_TOPOLOGY_LOCAL_THRESHOLD_MS);
257+
topology->local_threshold_msec = mongoc_uri_get_local_threshold_option (topology->uri);
258258

259259
/* Total time allowed to check a server is connectTimeoutMS.
260260
* Server Discovery And Monitoring Spec:

src/mongoc/mongoc-uri-private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ mongoc_uri_append_host (mongoc_uri_t *uri, const char *host, uint16_t port);
3434
bool
3535
mongoc_uri_parse_host (mongoc_uri_t *uri, const char *str);
3636

37+
int32_t
38+
mongoc_uri_get_local_threshold_option (const mongoc_uri_t *uri);
39+
3740
BSON_END_DECLS
3841

3942

src/mongoc/mongoc-uri.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "mongoc-log.h"
3131
#include "mongoc-handshake-private.h"
3232
#include "mongoc-socket.h"
33+
#include "mongoc-topology-private.h"
3334
#include "mongoc-uri-private.h"
3435
#include "mongoc-read-concern-private.h"
3536
#include "mongoc-write-concern-private.h"
@@ -1251,6 +1252,24 @@ mongoc_uri_set_appname (mongoc_uri_t *uri, const char *value)
12511252
return true;
12521253
}
12531254

1255+
/* can't use mongoc_uri_get_option_as_int32, it treats 0 specially */
1256+
int32_t
1257+
mongoc_uri_get_local_threshold_option (const mongoc_uri_t *uri)
1258+
{
1259+
const bson_t *options;
1260+
bson_iter_t iter;
1261+
int32_t retval = MONGOC_TOPOLOGY_LOCAL_THRESHOLD_MS;
1262+
1263+
if ((options = mongoc_uri_get_options (uri)) &&
1264+
bson_iter_init_find_case (&iter, options, "localthresholdms") &&
1265+
BSON_ITER_HOLDS_INT32 (&iter)) {
1266+
1267+
retval = bson_iter_int32 (&iter);
1268+
}
1269+
1270+
return retval;
1271+
}
1272+
12541273
const bson_t *
12551274
mongoc_uri_get_options (const mongoc_uri_t *uri)
12561275
{

tests/test-mongoc-uri.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <mongoc.h>
22

33
#include "mongoc-client-private.h"
4+
#include "mongoc-topology-private.h"
45
#include "mongoc-uri-private.h"
56
#include "mongoc-host-list-private.h"
67

@@ -482,7 +483,6 @@ test_mongoc_uri_functions (void)
482483
uri = mongoc_uri_new ("mongodb://localhost/?serverselectiontimeoutms=3"
483484
"&journal=true"
484485
"&wtimeoutms=42"
485-
"&localthresholdms=17"
486486
"&canonicalizeHostname=false");
487487

488488
ASSERT_CMPINT (
@@ -502,12 +502,6 @@ test_mongoc_uri_functions (void)
502502
ASSERT_CMPINT (
503503
mongoc_uri_get_option_as_int32 (uri, "wtimeoutms", 19), ==, 18);
504504

505-
ASSERT_CMPINT (
506-
mongoc_uri_get_option_as_int32 (uri, "localthresholdms", 99), ==, 17);
507-
ASSERT (mongoc_uri_set_option_as_int32 (uri, "localthresholdms", 99));
508-
ASSERT_CMPINT (
509-
mongoc_uri_get_option_as_int32 (uri, "localthresholdms", 42), ==, 99);
510-
511505
/* socketcheckintervalms isn't set, return our fallback */
512506
ASSERT_CMPINT (
513507
mongoc_uri_get_option_as_int32 (uri, "socketcheckintervalms", 123),
@@ -1070,6 +1064,33 @@ test_mongoc_uri_long_hostname (void)
10701064
bson_free (host);
10711065
}
10721066

1067+
test_mongoc_uri_local_threshold_ms (void)
1068+
{
1069+
mongoc_uri_t *uri;
1070+
1071+
uri = mongoc_uri_new ("mongodb://localhost/");
1072+
1073+
/* localthresholdms isn't set, return the default */
1074+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==,
1075+
MONGOC_TOPOLOGY_LOCAL_THRESHOLD_MS);
1076+
ASSERT (
1077+
mongoc_uri_set_option_as_int32 (uri, "localthresholdms", 99));
1078+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==, 99);
1079+
1080+
mongoc_uri_destroy(uri);
1081+
1082+
1083+
uri = mongoc_uri_new (
1084+
"mongodb://localhost/?localthresholdms=0");
1085+
1086+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==, 0);
1087+
ASSERT (
1088+
mongoc_uri_set_option_as_int32 (uri, "localthresholdms", 99));
1089+
ASSERT_CMPINT (mongoc_uri_get_local_threshold_option (uri), ==, 99);
1090+
1091+
mongoc_uri_destroy(uri);
1092+
}
1093+
10731094
void
10741095
test_uri_install (TestSuite *suite)
10751096
{
@@ -1089,4 +1110,5 @@ test_uri_install (TestSuite *suite)
10891110
TestSuite_Add (
10901111
suite, "/Uri/compound_setters", test_mongoc_uri_compound_setters);
10911112
TestSuite_Add (suite, "/Uri/long_hostname", test_mongoc_uri_long_hostname);
1113+
TestSuite_Add (suite, "/Uri/local_threshold_ms", test_mongoc_uri_local_threshold_ms);
10921114
}

0 commit comments

Comments
 (0)