Skip to content

CDRIVER-5548 new mongoc_client_update_sockettimeoutms() #1556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/libmongoc/doc/mongoc_client_set_sockettimeoutms.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:man_page: mongoc_client_set_sockettimeoutms

mongoc_client_set_sockettimeoutms()
===================================

Synopsis
--------

.. code-block:: c

void
mongoc_client_set_sockettimeoutms (mongoc_client_t *client, int32_t timeoutms);

Change the ``sockettimeoutms`` of the :symbol:`mongoc_client_t`.

If ``client`` was obtained from a :symbol:`mongoc_client_pool_t`, the socket timeout is restored to the previous value when calling :symbol:`mongoc_client_pool_push`.

Parameters
----------

* ``client``: A :symbol:`mongoc_client_t`.
* ``timeoutms``: The requested timeout value in milliseconds.

.. seealso::

| :ref:`URI Connection Options <connection_options>`
1 change: 1 addition & 0 deletions src/libmongoc/doc/mongoc_client_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Example
mongoc_client_set_read_concern
mongoc_client_set_read_prefs
mongoc_client_set_server_api
mongoc_client_set_sockettimeoutms
mongoc_client_set_ssl_opts
mongoc_client_set_stream_initiator
mongoc_client_set_write_concern
Expand Down
2 changes: 2 additions & 0 deletions src/libmongoc/doc/mongoc_uri_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ IPv4 and IPv6

.. include:: includes/ipv4-and-ipv6.txt

.. _connection_options:

Connection Options
------------------

Expand Down
3 changes: 3 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-client-pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ mongoc_client_pool_push (mongoc_client_pool_t *pool, mongoc_client_t *client)
BSON_ASSERT_PARAM (pool);
BSON_ASSERT_PARAM (client);

/* reset sockettimeoutms to the default in case it was changed with mongoc_client_set_sockettimeoutms() */
mongoc_cluster_reset_sockettimeoutms (&client->cluster);

bson_mutex_lock (&pool->mutex);
_mongoc_queue_push_head (&pool->queue, client);

Expand Down
7 changes: 7 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,13 @@ mongoc_client_destroy (mongoc_client_t *client)
}


void
mongoc_client_set_sockettimeoutms (mongoc_client_t *client, int32_t timeoutms)
{
BSON_ASSERT_PARAM (client);
mongoc_cluster_set_sockettimeoutms (&client->cluster, timeoutms);
}

/*
*--------------------------------------------------------------------------
*
Expand Down
2 changes: 2 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ MONGOC_EXPORT (mongoc_client_t *)
mongoc_client_new_from_uri (const mongoc_uri_t *uri) BSON_GNUC_WARN_UNUSED_RESULT;
MONGOC_EXPORT (mongoc_client_t *)
mongoc_client_new_from_uri_with_error (const mongoc_uri_t *uri, bson_error_t *error) BSON_GNUC_WARN_UNUSED_RESULT;
MONGOC_EXPORT (void)
mongoc_client_set_sockettimeoutms (mongoc_client_t *client, int32_t timeoutms);
MONGOC_EXPORT (const mongoc_uri_t *)
mongoc_client_get_uri (const mongoc_client_t *client);
MONGOC_EXPORT (void)
Expand Down
6 changes: 6 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-cluster-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ mongoc_cluster_init (mongoc_cluster_t *cluster, const mongoc_uri_t *uri, void *c
void
mongoc_cluster_destroy (mongoc_cluster_t *cluster);

void
mongoc_cluster_set_sockettimeoutms (mongoc_cluster_t *cluster, int32_t sockettimeoutms);

void
mongoc_cluster_reset_sockettimeoutms (mongoc_cluster_t *cluster);

void
mongoc_cluster_disconnect_node (mongoc_cluster_t *cluster, uint32_t id);

Expand Down
18 changes: 16 additions & 2 deletions src/libmongoc/src/mongoc/mongoc-cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -2513,8 +2513,7 @@ mongoc_cluster_init (mongoc_cluster_t *cluster, const mongoc_uri_t *uri, void *c
cluster->client = (mongoc_client_t *) client;
cluster->requires_auth = (mongoc_uri_get_username (uri) || mongoc_uri_get_auth_mechanism (uri));

cluster->sockettimeoutms =
mongoc_uri_get_option_as_int32 (uri, MONGOC_URI_SOCKETTIMEOUTMS, MONGOC_DEFAULT_SOCKETTIMEOUTMS);
mongoc_cluster_reset_sockettimeoutms (cluster);

cluster->socketcheckintervalms =
mongoc_uri_get_option_as_int32 (uri, MONGOC_URI_SOCKETCHECKINTERVALMS, MONGOC_TOPOLOGY_SOCKET_CHECK_INTERVAL_MS);
Expand Down Expand Up @@ -2562,6 +2561,21 @@ mongoc_cluster_destroy (mongoc_cluster_t *cluster) /* INOUT */
EXIT;
}

void
mongoc_cluster_set_sockettimeoutms (mongoc_cluster_t *cluster, int32_t timeoutms)
{
BSON_ASSERT_PARAM (cluster);
cluster->sockettimeoutms = timeoutms;
}

void
mongoc_cluster_reset_sockettimeoutms (mongoc_cluster_t *cluster)
{
BSON_ASSERT_PARAM (cluster);
cluster->sockettimeoutms =
mongoc_uri_get_option_as_int32 (cluster->uri, MONGOC_URI_SOCKETTIMEOUTMS, MONGOC_DEFAULT_SOCKETTIMEOUTMS);
}

static uint32_t
_mongoc_cluster_select_server_id (mongoc_client_session_t *cs,
mongoc_topology_t *topology,
Expand Down
28 changes: 28 additions & 0 deletions src/libmongoc/tests/test-mongoc-client-pool.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <mongoc/mongoc.h>
#include "mongoc/mongoc-client-pool-private.h"
#include <mongoc/mongoc-client-private.h>
#include "mongoc/mongoc-util-private.h"


Expand Down Expand Up @@ -454,6 +455,32 @@ test_client_pool_max_pool_size_exceeded (void)
bson_free (args);
}

static void
test_client_pool_can_override_sockettimeoutms (void)
{
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://localhost:27017/?socketTimeoutMS=1000");
mongoc_client_pool_t *pool = mongoc_client_pool_new (uri);

// Override the client's socketTimeoutMS.
{
mongoc_client_t *client = mongoc_client_pool_pop (pool);
ASSERT_CMPINT32 (client->cluster.sockettimeoutms, ==, 1000);
mongoc_client_set_sockettimeoutms (client, 2000);
ASSERT_CMPINT32 (client->cluster.sockettimeoutms, ==, 2000);
mongoc_client_pool_push (pool, client);
}

// Pop again. Expect the newly popped client to have the socketTimeoutMS from the URI.
{
mongoc_client_t *client = mongoc_client_pool_pop (pool);
ASSERT_CMPINT32 (client->cluster.sockettimeoutms, ==, 1000);
mongoc_client_pool_push (pool, client);
}

mongoc_client_pool_destroy (pool);
mongoc_uri_destroy (uri);
}

void
test_client_pool_install (TestSuite *suite)
{
Expand All @@ -478,4 +505,5 @@ test_client_pool_install (TestSuite *suite)
#endif
TestSuite_AddLive (suite, "/ClientPool/destroy_without_push", test_client_pool_destroy_without_pushing);
TestSuite_AddLive (suite, "/ClientPool/max_pool_size_exceeded", test_client_pool_max_pool_size_exceeded);
TestSuite_Add (suite, "/ClientPool/can_override_sockettimeoutms", test_client_pool_can_override_sockettimeoutms);
}