Skip to content

Commit 95ed934

Browse files
CDRIVER-3871 implement public setters and getters for timeoutMS
1 parent 20cada3 commit 95ed934

23 files changed

+402
-17
lines changed

src/libmongoc/doc/errors.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ Many C Driver functions report errors by returning ``false`` or -1 and filling o
9797
+-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
9898
| ``MONGOC_ERROR_CLIENT_SIDE_ENCRYPTION`` | Error code produced by libmongocrypt. | An error occurred in the library responsible for Client Side Encryption |
9999
+-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
100+
| ``MONGOC_ERROR_TIMEOUT`` | ``MONGOC_ERROR_TIMEOUT_INVALID`` | You passed an invalid value for a timeout paramenter. |
101+
+-----------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
100102

101103
.. _error_labels:
102104

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
:man_page: mongoc_client_get_timeout_ms
2+
3+
mongoc_client_get_timeout_ms()
4+
==============================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
int64_t
12+
mongoc_client_get_timeout_ms (const mongoc_client_t *client)
13+
14+
Returns the client-side operation timeout value for this object. If ``client`` does not have a timeout currently set, this method returns -1 for MONGOC_TIMEOUTMS_UNSET.
15+
16+
Parameters
17+
----------
18+
19+
* ``client``: A :symbol:`mongoc_client_t`.
20+
21+
Returns
22+
-------
23+
24+
The timeout set on this client or -1 for MONGOC_TIMEOUTMS_UNSET.

src/libmongoc/doc/mongoc_client_t.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Example
6868
mongoc_client_get_server_description
6969
mongoc_client_get_server_descriptions
7070
mongoc_client_get_server_status
71+
mongoc_client_get_timeout_ms
7172
mongoc_client_get_uri
7273
mongoc_client_get_write_concern
7374
mongoc_client_new
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
:man_page: mongoc_collection_get_timeout_ms
2+
3+
mongoc_collection_get_timeout_ms()
4+
==================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
int64_t
12+
mongoc_collection_get_timeout_ms (const mongoc_collection_t *collection)
13+
14+
Returns the client-side operation timeout value for this object. If ``collection`` does not have a timeout currently set, this method will return a value inherited from the parent :symbol:`mongoc_database_t` or :symbol:`mongoc_client_t` object. If no timeout is set on any parent object, returns -1 for MONGOC_TIMEOUTMS_UNSET.
15+
16+
Parameters
17+
----------
18+
19+
* ``collection``: A :symbol:`mongoc_collection_t`.
20+
21+
Returns
22+
-------
23+
24+
The timeout set on this collection or inherited from a parent object.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
:man_page: mongoc_collection_set_timeout_ms
2+
3+
mongoc_collection_set_timeout_ms()
4+
==================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
mongoc_collection_set_timeout_ms (mongoc_collection_t *collection, int64_t timeout_ms, bson_error_t *error)
13+
14+
Sets the client-side operation timeout value for ``collection`` to the given ``timeout_ms``. If there is an error setting the timeout, this method will return false and ``error`` will be set.
15+
16+
17+
This function will set ``error`` and return false in the following cases:
18+
19+
* ``timeout_ms`` is a negative number
20+
21+
Parameters
22+
----------
23+
24+
* ``collection``: A :symbol:`mongoc_collection_t`.
25+
* ``timeout_ms``: A value, in milliseconds, for the timeout. Must be non-negative.
26+
27+
Returns
28+
-------
29+
30+
true if the timeout_ms is set successfully. Otherwise, false.

src/libmongoc/doc/mongoc_collection_t.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Read preferences and write concerns are inherited from the parent client. They c
5858
mongoc_collection_get_name
5959
mongoc_collection_get_read_concern
6060
mongoc_collection_get_read_prefs
61+
mongoc_collection_get_timeout_ms
6162
mongoc_collection_get_write_concern
6263
mongoc_collection_insert
6364
mongoc_collection_insert_bulk
@@ -73,6 +74,7 @@ Read preferences and write concerns are inherited from the parent client. They c
7374
mongoc_collection_save
7475
mongoc_collection_set_read_concern
7576
mongoc_collection_set_read_prefs
77+
mongoc_collection_set_timeout_ms
7678
mongoc_collection_set_write_concern
7779
mongoc_collection_stats
7880
mongoc_collection_update
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
:man_page: mongoc_database_get_timeout_ms
2+
3+
mongoc_database_get_timeout_ms()
4+
================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
int64_t
12+
mongoc_database_get_timeout_ms (const mongoc_database_t *database)
13+
14+
Returns the client-side operation timeout value for this object. If ``database`` does not have a timeout currently set, this method will return a value inherited from the parent :symbol:`mongoc_client_t` object. If no timeout is set on the parent object, returns -1, or MONGOC_TIMEOUTMS_UNSET.
15+
16+
Parameters
17+
----------
18+
19+
* ``database``: A :symbol:`mongoc_database_t`.
20+
21+
Returns
22+
-------
23+
24+
The timeout set on this database or inherited from a parent object.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
:man_page: mongoc_database_set_timeout_ms
2+
3+
mongoc_database_set_timeout_ms()
4+
================================
5+
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
mongoc_database_set_timeout_ms (mongoc_database_t *database, int64_t timeout_ms, bson_error_t *error)
13+
14+
Sets the client-side operation timeout value for ``database`` to the given ``timeout_ms``. If there is an error setting the timeout, this method will return false and ``error`` will be set.
15+
16+
17+
This function will set ``error`` and return false in the following cases:
18+
19+
* ``timeout_ms`` is a negative number
20+
21+
Parameters
22+
----------
23+
24+
* ``database``: A :symbol:`mongoc_database_t`.
25+
* ``timeout_ms``: A value, in milliseconds, for the timeout. Must be non-negative.
26+
27+
Returns
28+
-------
29+
30+
true if the timeout_ms is set successfully. Otherwise, false.

src/libmongoc/doc/mongoc_database_t.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Read preferences and write concerns are inherited from the parent client. They c
4343
mongoc_database_get_name
4444
mongoc_database_get_read_concern
4545
mongoc_database_get_read_prefs
46+
mongoc_database_get_timeout_ms
4647
mongoc_database_get_write_concern
4748
mongoc_database_has_collection
4849
mongoc_database_read_command_with_opts
@@ -51,6 +52,7 @@ Read preferences and write concerns are inherited from the parent client. They c
5152
mongoc_database_remove_user
5253
mongoc_database_set_read_concern
5354
mongoc_database_set_read_prefs
55+
mongoc_database_set_timeout_ms
5456
mongoc_database_set_write_concern
5557
mongoc_database_watch
5658
mongoc_database_write_command_with_opts

src/libmongoc/src/mongoc/mongoc-client-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ struct _mongoc_client_t {
122122
mongoc_set_t *client_sessions;
123123
unsigned int csid_rand_seed;
124124

125+
int64_t timeout_ms;
126+
125127
uint32_t generation;
126128
};
127129

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,9 @@ _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;
1161+
11591162
mongoc_cluster_init (&client->cluster, client->uri, client);
11601163

11611164
#ifdef MONGOC_ENABLE_SSL
@@ -1327,7 +1330,8 @@ mongoc_client_get_database (mongoc_client_t *client, const char *name)
13271330
name,
13281331
client->read_prefs,
13291332
client->read_concern,
1330-
client->write_concern);
1333+
client->write_concern,
1334+
client->timeout_ms);
13311335
}
13321336

13331337

@@ -1406,7 +1410,8 @@ mongoc_client_get_collection (mongoc_client_t *client,
14061410
collection,
14071411
client->read_prefs,
14081412
client->read_concern,
1409-
client->write_concern);
1413+
client->write_concern,
1414+
client->timeout_ms);
14101415
}
14111416

14121417

@@ -3070,6 +3075,8 @@ mongoc_client_enable_auto_encryption (mongoc_client_t *client,
30703075
mongoc_auto_encryption_opts_t *opts,
30713076
bson_error_t *error)
30723077
{
3078+
BSON_ASSERT_PARAM (client);
3079+
30733080
if (!client->topology->single_threaded) {
30743081
bson_set_error (error,
30753082
MONGOC_ERROR_CLIENT,
@@ -3080,3 +3087,11 @@ mongoc_client_enable_auto_encryption (mongoc_client_t *client,
30803087
}
30813088
return _mongoc_cse_client_enable_auto_encryption (client, opts, error);
30823089
}
3090+
3091+
int64_t
3092+
mongoc_client_get_timeout_ms (const mongoc_client_t *client)
3093+
{
3094+
BSON_ASSERT_PARAM (client);
3095+
3096+
return client->timeout_ms;
3097+
}

src/libmongoc/src/mongoc/mongoc-client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ mongoc_client_enable_auto_encryption (mongoc_client_t *client,
268268
mongoc_auto_encryption_opts_t *opts,
269269
bson_error_t *error);
270270

271+
MONGOC_EXPORT (int64_t)
272+
mongoc_client_get_timeout_ms (const mongoc_client_t *client);
273+
271274
BSON_END_DECLS
272275

273276

src/libmongoc/src/mongoc/mongoc-collection-private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct _mongoc_collection_t {
3737
mongoc_read_concern_t *read_concern;
3838
mongoc_write_concern_t *write_concern;
3939
bson_t *gle;
40+
int64_t timeout_ms;
4041
};
4142

4243

@@ -46,7 +47,8 @@ _mongoc_collection_new (mongoc_client_t *client,
4647
const char *collection,
4748
const mongoc_read_prefs_t *read_prefs,
4849
const mongoc_read_concern_t *read_concern,
49-
const mongoc_write_concern_t *write_concern);
50+
const mongoc_write_concern_t *write_concern,
51+
int64_t timeout_ms);
5052

5153
bool
5254
_mongoc_collection_create_index_if_not_exists (mongoc_collection_t *collection,

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@
2222
#include "mongoc-bulk-operation-private.h"
2323
#include "mongoc-change-stream-private.h"
2424
#include "mongoc-client-private.h"
25-
#include "mongoc-find-and-modify-private.h"
26-
#include "mongoc-find-and-modify.h"
2725
#include "mongoc-collection.h"
2826
#include "mongoc-collection-private.h"
2927
#include "mongoc-cursor-private.h"
28+
#include "mongoc-database-private.h"
3029
#include "mongoc-error.h"
30+
#include "mongoc-error-private.h"
31+
#include "mongoc-find-and-modify-private.h"
32+
#include "mongoc-find-and-modify.h"
3133
#include "mongoc-index.h"
3234
#include "mongoc-log.h"
33-
#include "mongoc-trace-private.h"
35+
#include "mongoc-opts-private.h"
3436
#include "mongoc-read-concern-private.h"
35-
#include "mongoc-write-concern-private.h"
3637
#include "mongoc-read-prefs-private.h"
38+
#include "mongoc-trace-private.h"
39+
#include "mongoc-uri.h"
3740
#include "mongoc-util-private.h"
3841
#include "mongoc-write-command-private.h"
39-
#include "mongoc-opts-private.h"
4042
#include "mongoc-write-command-private.h"
41-
#include "mongoc-error-private.h"
43+
#include "mongoc-write-concern-private.h"
4244

4345
#undef MONGOC_LOG_DOMAIN
4446
#define MONGOC_LOG_DOMAIN "collection"
@@ -166,7 +168,8 @@ _mongoc_collection_new (mongoc_client_t *client,
166168
const char *collection,
167169
const mongoc_read_prefs_t *read_prefs,
168170
const mongoc_read_concern_t *read_concern,
169-
const mongoc_write_concern_t *write_concern)
171+
const mongoc_write_concern_t *write_concern,
172+
int64_t timeout_ms)
170173
{
171174
mongoc_collection_t *col;
172175

@@ -195,6 +198,8 @@ _mongoc_collection_new (mongoc_client_t *client,
195198

196199
col->gle = NULL;
197200

201+
col->timeout_ms = timeout_ms;
202+
198203
RETURN (col);
199204
}
200205

@@ -280,7 +285,8 @@ mongoc_collection_copy (mongoc_collection_t *collection) /* IN */
280285
collection->collection,
281286
collection->read_prefs,
282287
collection->read_concern,
283-
collection->write_concern));
288+
collection->write_concern,
289+
collection->timeout_ms));
284290
}
285291

286292

@@ -3507,3 +3513,31 @@ mongoc_collection_watch (const mongoc_collection_t *coll,
35073513
{
35083514
return _mongoc_change_stream_new_from_collection (coll, pipeline, opts);
35093515
}
3516+
3517+
bool
3518+
mongoc_collection_set_timeout_ms (mongoc_collection_t *coll,
3519+
int64_t timeout_ms,
3520+
bson_error_t *error)
3521+
{
3522+
BSON_ASSERT_PARAM (coll);
3523+
3524+
if (timeout_ms < 0) {
3525+
bson_set_error (error,
3526+
MONGOC_ERROR_TIMEOUT,
3527+
MONGOC_ERROR_TIMEOUT_INVALID,
3528+
"timeoutMS must be a non-negative integer");
3529+
return false;
3530+
}
3531+
3532+
coll->timeout_ms = timeout_ms;
3533+
3534+
return true;
3535+
}
3536+
3537+
int64_t
3538+
mongoc_collection_get_timeout_ms (const mongoc_collection_t *coll)
3539+
{
3540+
BSON_ASSERT_PARAM (coll);
3541+
3542+
return coll->timeout_ms;
3543+
}

src/libmongoc/src/mongoc/mongoc-collection.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ mongoc_collection_estimated_document_count (
351351
bson_t *reply,
352352
bson_error_t *error);
353353

354+
MONGOC_EXPORT (bool)
355+
mongoc_collection_set_timeout_ms (mongoc_collection_t *coll,
356+
int64_t timeout_ms,
357+
bson_error_t *error);
358+
359+
MONGOC_EXPORT (int64_t)
360+
mongoc_collection_get_timeout_ms (const mongoc_collection_t *coll);
361+
354362
BSON_END_DECLS
355363

356364

src/libmongoc/src/mongoc/mongoc-database-private.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct _mongoc_database_t {
3535
mongoc_read_prefs_t *read_prefs;
3636
mongoc_read_concern_t *read_concern;
3737
mongoc_write_concern_t *write_concern;
38+
int64_t timeout_ms;
3839
};
3940

4041

@@ -43,7 +44,8 @@ _mongoc_database_new (mongoc_client_t *client,
4344
const char *name,
4445
const mongoc_read_prefs_t *read_prefs,
4546
const mongoc_read_concern_t *read_concern,
46-
const mongoc_write_concern_t *write_concern);
47+
const mongoc_write_concern_t *write_concern,
48+
int64_t timeout_ms);
4749

4850
BSON_END_DECLS
4951

0 commit comments

Comments
 (0)