Skip to content

Commit e57189e

Browse files
committed
CDRIVER-424 filter out non-matching nodes before those that are far
1 parent 63981c0 commit e57189e

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/mongoc/mongoc-cluster.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,6 @@ _mongoc_cluster_select (mongoc_cluster_t *cluster,
740740
* in the fast path of command dispatching.
741741
*/
742742

743-
#define IS_NEARER_THAN(n, msec) \
744-
((msec < 0 && (n)->ping_avg_msec >= 0) || ((n)->ping_avg_msec < msec))
745-
746743
count = 0;
747744

748745
for (i = 0; i < MONGOC_CLUSTER_MAX_NODES; i++) {
@@ -759,10 +756,33 @@ _mongoc_cluster_select (mongoc_cluster_t *cluster,
759756
}
760757

761758
}
759+
count++;
760+
}
761+
}
762+
763+
/*
764+
* Filter nodes with score less than highest score.
765+
*/
766+
if (max_score) {
767+
for (i = 0; i < MONGOC_CLUSTER_MAX_NODES; i++) {
768+
if (nodes[i] && (scores[i] < max_score)) {
769+
nodes[i] = NULL;
770+
count--;
771+
}
772+
}
773+
}
774+
775+
/*
776+
* Get the nearest node among those which have not been filtered out
777+
*/
778+
#define IS_NEARER_THAN(n, msec) \
779+
((msec < 0 && (n)->ping_avg_msec >= 0) || ((n)->ping_avg_msec < msec))
780+
781+
for (i = 0; i < MONGOC_CLUSTER_MAX_NODES; i++) {
782+
if (nodes[i]) {
762783
if (IS_NEARER_THAN(nodes[i], nearest)) {
763784
nearest = nodes[i]->ping_avg_msec;
764785
}
765-
count++;
766786
}
767787
}
768788

@@ -775,27 +795,14 @@ _mongoc_cluster_select (mongoc_cluster_t *cluster,
775795
watermark = nearest + cluster->sec_latency_ms;
776796
for (i = 0; i < MONGOC_CLUSTER_MAX_NODES; i++) {
777797
if (nodes[i]) {
778-
if (nodes[i]->ping_avg_msec >(int32_t)watermark) {
798+
if (nodes[i]->ping_avg_msec > (int32_t)watermark) {
779799
nodes[i] = NULL;
780800
count--;
781801
}
782802
}
783803
}
784804
}
785805

786-
/*
787-
* Filter nodes with score less than highest score.
788-
*/
789-
if (max_score) {
790-
for (i = 0; i < MONGOC_CLUSTER_MAX_NODES; i++) {
791-
if (nodes[i] && (scores[i] < max_score)) {
792-
nodes[i] = NULL;
793-
count--;
794-
}
795-
}
796-
}
797-
798-
799806
/*
800807
* Mark the error as unable to locate a target node.
801808
*/

0 commit comments

Comments
 (0)