diff --git a/src/junction.c b/src/junction.c index 1e0f679..70a7e3e 100644 --- a/src/junction.c +++ b/src/junction.c @@ -2087,6 +2087,8 @@ static double ep_score(const j_endpoint *ep) /* Order the rule's targets according to the configured balance mode. "first" * keeps rule order, which is what a failover chain wants; the others sort. */ +static pthread_mutex_t g_rr_mu = PTHREAD_MUTEX_INITIALIZER; + static void balance_order(const j_rule *r, int *order, int n) { static unsigned long rr_turn; @@ -2111,7 +2113,12 @@ static void balance_order(const j_rule *r, int *order, int n) slots[ns++] = i; } if (!ns) return; - int shift = (int)(rr_turn++ % (unsigned long)ns); + /* Concurrent requests must get different turns; otherwise two requests + * starting together both pick the same first target and queue there. */ + pthread_mutex_lock(&g_rr_mu); + unsigned long turn = rr_turn++; + pthread_mutex_unlock(&g_rr_mu); + int shift = (int)(turn % (unsigned long)ns); int seen = 0; for (int s2 = 0; s2 < ns && seen < n; s2++) { int t = slots[(s2 + shift) % ns];