Skip to content

Commit a794de8

Browse files
committed
mongoc_client_get_handshake_description establishes connection
1 parent d77d90f commit a794de8

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

src/libmongoc/doc/mongoc_client_get_handshake_description.rst

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,14 @@ Description
2121

2222
:symbol:`mongoc_client_get_handshake_description` is distinct from :symbol:`mongoc_client_get_server_description`. :symbol:`mongoc_client_get_server_description` returns a server description constructed from monitoring, which may differ from the server description constructed from the connection handshake.
2323

24-
Use this function only for building a language driver that wraps the C Driver. When writing applications in C, higher-level functions automatically select a suitable server.
24+
:symbol:`mongoc_client_get_handshake_description` will attempt to establish a connection to the server if a connection was not already established. It will perform the MongoDB handshake and authentication if required.
2525

26-
:symbol:`mongoc_client_get_handshake_description` does not attempt to establish a connection to the server if a connection was not already established. If a connection has not been established, this returns ``NULL`` and sets ``error``.
26+
Use this function only for building a language driver that wraps the C Driver. When writing applications in C, higher-level functions automatically select a suitable server.
2727

2828
Single-threaded client behavior
2929
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
To ensure a connection has been established ensure that the server is selectable (e.g. by calling :symbol:`mongoc_client_select_server`) or that an operation has successfully run on the server (e.g. by sending "ping").
31-
32-
If the established connection has not completed authentication, calling :symbol:`mongoc_client_get_handshake_description` will complete authentication on the connection.
33-
34-
Single-threaded clients only have one active connection to each server. The one connection is used for both monitoring and application operations. However, the server description returned by :symbol:`mongoc_client_get_handshake_description` may still differ from :symbol:`mongoc_client_get_server_description`. Notably, if connected to a load balanced cluster, the :symbol:`mongoc_client_get_server_description` will describe the load balancer server (:symbol:`mongoc_server_description_type` will return "LoadBalancer"). And :symbol:`mongoc_client_get_handshake_description` will describe the backing server.
3530

36-
Pooled client behavior
37-
^^^^^^^^^^^^^^^^^^^^^^
38-
To ensure a connection has been established ensure an operation has successfully run on a server (e.g. by sending "ping").
31+
Single-threaded clients only have one active connection to each server. The one connection is used for both monitoring and application operations. However, the server description returned by :symbol:`mongoc_client_get_handshake_description` may still differ from the server description returned by :symbol:`mongoc_client_get_server_description`. Notably, if connected to a load balanced cluster, the :symbol:`mongoc_client_get_server_description` will describe the load balancer server (:symbol:`mongoc_server_description_type` will return "LoadBalancer"). And :symbol:`mongoc_client_get_handshake_description` will describe the backing server.
3932

4033
Parameters
4134
----------
@@ -48,7 +41,7 @@ Parameters
4841
Returns
4942
-------
5043

51-
A :symbol:`mongoc_server_description_t` that must be freed with :symbol:`mongoc_server_description_destroy`. If a connection has not been successfully established to a server, returns NULL and ``error`` is filled out.
44+
A :symbol:`mongoc_server_description_t` that must be freed with :symbol:`mongoc_server_description_destroy`. If a connection has not been successfully established to a server, returns ``NULL`` and ``error`` is filled out.
5245

5346

5447
See Also

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3154,7 +3154,7 @@ mongoc_client_get_handshake_description (mongoc_client_t *client,
31543154

31553155
server_stream = mongoc_cluster_stream_for_server (&client->cluster,
31563156
server_id,
3157-
false /* reconnect */,
3157+
true /* reconnect */,
31583158
NULL /* client session */,
31593159
NULL /* reply */,
31603160
error);

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4080,8 +4080,8 @@ test_mongoc_client_get_handshake_hello_response_pooled (void)
40804080
handshake_sd = mongoc_client_get_handshake_description (
40814081
client, monitor_sd->id, NULL /* opts */, &error);
40824082
ASSERT_OR_PRINT (handshake_sd, error);
4083-
BSON_ASSERT (MONGOC_SERVER_UNKNOWN !=
4084-
mongoc_server_description_type (handshake_sd));
4083+
BSON_ASSERT (
4084+
0 != strcmp ("Unknown", mongoc_server_description_type (handshake_sd)));
40854085

40864086
mongoc_server_description_destroy (handshake_sd);
40874087
mongoc_server_description_destroy (invalidated_sd);
@@ -4090,6 +4090,53 @@ test_mongoc_client_get_handshake_hello_response_pooled (void)
40904090
mongoc_client_pool_destroy (pool);
40914091
}
40924092

4093+
/* Test that calling mongoc_client_get_handshake_description establishes a
4094+
* connection if a connection has not already been established. */
4095+
void
4096+
test_mongoc_client_get_handshake_establishes_connection_single (void)
4097+
{
4098+
mongoc_client_t *client;
4099+
mongoc_server_description_t *handshake_sd;
4100+
bson_error_t error = {0};
4101+
uint32_t server_id = 1;
4102+
4103+
client = test_framework_new_default_client ();
4104+
4105+
handshake_sd = mongoc_client_get_handshake_description (
4106+
client, server_id, NULL /* opts */, &error);
4107+
ASSERT_OR_PRINT (handshake_sd, error);
4108+
BSON_ASSERT (
4109+
0 != strcmp ("Unknown", mongoc_server_description_type (handshake_sd)));
4110+
4111+
mongoc_server_description_destroy (handshake_sd);
4112+
mongoc_client_destroy (client);
4113+
}
4114+
4115+
void
4116+
test_mongoc_client_get_handshake_establishes_connection_pooled (void)
4117+
{
4118+
mongoc_client_pool_t *pool;
4119+
mongoc_client_t *client;
4120+
mongoc_server_description_t *handshake_sd;
4121+
bson_error_t error = {0};
4122+
uint32_t server_id = 1;
4123+
4124+
pool = test_framework_new_default_client_pool ();
4125+
client = mongoc_client_pool_pop (pool);
4126+
4127+
/* The previously established connection should have a valid server
4128+
* description. */
4129+
handshake_sd = mongoc_client_get_handshake_description (
4130+
client, server_id, NULL /* opts */, &error);
4131+
ASSERT_OR_PRINT (handshake_sd, error);
4132+
BSON_ASSERT (
4133+
0 != strcmp ("Unknown", mongoc_server_description_type (handshake_sd)));
4134+
4135+
mongoc_server_description_destroy (handshake_sd);
4136+
mongoc_client_pool_push (pool, client);
4137+
mongoc_client_pool_destroy (pool);
4138+
}
4139+
40934140
void
40944141
test_client_install (TestSuite *suite)
40954142
{
@@ -4389,4 +4436,10 @@ test_client_install (TestSuite *suite)
43894436
TestSuite_AddLive (suite,
43904437
"/Client/get_handshake_hello_response/pooled",
43914438
test_mongoc_client_get_handshake_hello_response_pooled);
4439+
TestSuite_AddLive (suite,
4440+
"/Client/get_handshake_establishes_connection/single",
4441+
test_mongoc_client_get_handshake_establishes_connection_single);
4442+
TestSuite_AddLive (suite,
4443+
"/Client/get_handshake_establishes_connection/pooled",
4444+
test_mongoc_client_get_handshake_establishes_connection_pooled);
43924445
}

0 commit comments

Comments
 (0)