From 5cc0ea1ec3bedc51f1106cc76ffd865eaea9efb5 Mon Sep 17 00:00:00 2001 From: newacorn Date: Wed, 21 Aug 2024 05:24:01 +0800 Subject: [PATCH] Fix the MaxConns semaphore issue in HostClient. (#1831) Fix the MaxConns semaphore issue in HostClient. Currently, the conns length and connsCount count in HostClient do not correctly implement the MaxConns semaphore mechanism. This fix ensures that the waiter wake-up chain does not break. --- client.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 65154ec..b712f1b 100644 --- a/client.go +++ b/client.go @@ -1749,7 +1749,17 @@ func (c *HostClient) releaseConn(cc *clientConn) { w := q.popFront() if w.waiting() { delivered = w.tryDeliver(cc, nil) - break + // This is the last resort to hand over conCount sema. + // We must ensure that there are no valid waiters in connsWait + // when we exit this loop. + // + // We did not apply the same looping pattern in the decConnsCount + // method because it needs to create a new time-spent connection, + // and the decConnsCount call chain will inevitably reach this point. + // When MaxConnWaitTimeout>0. + if delivered { + break + } } c.connsWait.failedWaiters.Add(-1) }