Skip to content

Commit 5f33f7d

Browse files
committed
CDRIVER-1639 seed the PRNG used to select servers
1 parent 5a46ba9 commit 5f33f7d

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ New features and bug fixes:
3838
* New CMake option ENABLE_TRACING allows debug output, which before had only
3939
been available with "configure --enable-tracing".
4040
* Bugfix: "PossiblePrimary"-type replicas could be selected for reads
41+
* The random number generator used to select servers is now properly seeded.
4142

4243
Removed configure flags:
4344
* --enable-experimental has been removed and all of its features

src/mongoc/mongoc-topology-description-private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct _mongoc_topology_description_t
4848
char *compatibility_error;
4949
uint32_t max_server_id;
5050
bool stale;
51+
unsigned int rand_seed;
5152

5253
mongoc_apm_callbacks_t apm_callbacks;
5354
void *apm_context;

src/mongoc/mongoc-topology-description.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ mongoc_topology_description_init (mongoc_topology_description_t *descriptio
6969
description->compatible = true;
7070
description->compatibility_error = NULL;
7171
description->stale = true;
72+
description->rand_seed = (unsigned int) bson_get_monotonic_time ();
7273

7374
EXIT;
7475
}
@@ -82,6 +83,8 @@ mongoc_topology_description_init (mongoc_topology_description_t *descriptio
8283
* @dst must not already point to any allocated resources. Clean
8384
* up with mongoc_topology_description_destroy.
8485
*
86+
* WARNING: @dst's rand_seed is not initialized.
87+
*
8588
* Returns:
8689
* None.
8790
*
@@ -678,6 +681,7 @@ mongoc_topology_description_select (mongoc_topology_description_t *topology,
678681
{
679682
mongoc_array_t suitable_servers;
680683
mongoc_server_description_t *sd = NULL;
684+
int rand_n;
681685

682686
ENTRY;
683687

@@ -704,8 +708,9 @@ mongoc_topology_description_select (mongoc_topology_description_t *topology,
704708
topology, read_pref,
705709
local_threshold_ms);
706710
if (suitable_servers.len != 0) {
711+
rand_n = MONGOC_RAND_R (&topology->rand_seed);
707712
sd = _mongoc_array_index(&suitable_servers, mongoc_server_description_t*,
708-
rand() % suitable_servers.len);
713+
rand_n % suitable_servers.len);
709714
}
710715

711716
_mongoc_array_destroy (&suitable_servers);

src/mongoc/mongoc-util-private.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

3939
#define COALESCE(x, y) ((x == 0) ? (y) : (x))
4040

41+
#ifdef _WIN32
42+
# define MONGOC_RAND_R rand_s
43+
#else
44+
# define MONGOC_RAND_R rand_r
45+
#endif
46+
4147
BSON_BEGIN_DECLS
4248

4349

0 commit comments

Comments
 (0)