Skip to content

Commit d9dfa56

Browse files
author
Christian Hergert
committed
client: translate slaveok into secondary preferred.
This will allow clients to dispatch to secondaries for reads if slaveok=true in the MongoDB connection URI as the default read preference. We might want to determine if SECONDARY_PREFERRED is the ideal, or if we want NEAREST. Since NEAREST is a bit confusing in how it actually works, SECONDARY_PREFERRED is probably the way to go.
1 parent 128f92f commit d9dfa56

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/mongoc/mongoc-client.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ mongoc_client_new (const char *uri_string)
673673
const bson_t *options;
674674
bson_iter_t iter;
675675
bool has_ssl = false;
676+
bool slave_okay = false;
676677

677678
if (!uri_string) {
678679
uri_string = "mongodb://127.0.0.1/";
@@ -690,6 +691,12 @@ mongoc_client_new (const char *uri_string)
690691
has_ssl = true;
691692
}
692693

694+
if (bson_iter_init_find_case (&iter, options, "slaveok") &&
695+
BSON_ITER_HOLDS_BOOL (&iter) &&
696+
bson_iter_bool (&iter)) {
697+
slave_okay = true;
698+
}
699+
693700
client = bson_malloc0(sizeof *client);
694701
client->uri = uri;
695702
client->request_id = rand ();
@@ -699,7 +706,11 @@ mongoc_client_new (const char *uri_string)
699706
write_concern = mongoc_uri_get_write_concern (uri);
700707
client->write_concern = mongoc_write_concern_copy (write_concern);
701708

702-
client->read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
709+
if (slave_okay) {
710+
client->read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY_PREFERRED);
711+
} else {
712+
client->read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY);
713+
}
703714

704715
_mongoc_cluster_init (&client->cluster, client->uri, client);
705716

0 commit comments

Comments
 (0)