From d2f97fc426ed451e64dc8e35e7f87a1d4a2d7bde Mon Sep 17 00:00:00 2001 From: Zhengkai Wang Date: Fri, 12 May 2023 15:49:37 +0800 Subject: [PATCH] fix the problem: HostClient maybe delete when pendingClientRequests > 0 (#1562) * add the functions to get host clients * add concurrency for client's HostClient map * delete test code * add lock in once block * fix the problem: HostClient maybe delete when pendingClientRequests > 0 * fix the problem: HostClient maybe delete when pendingClientRequests > 0 --------- Co-authored-by: wangzhengkai.wzk --- client.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 88005714b1..d37ed334af 100644 --- a/client.go +++ b/client.go @@ -501,6 +501,10 @@ func (c *Client) Do(req *Request, resp *Response) error { m = c.ms } hc := m[string(host)] + if hc != nil { + atomic.AddInt32(&hc.pendingClientRequests, 1) + defer atomic.AddInt32(&hc.pendingClientRequests, -1) + } c.mLock.RUnlock() if hc == nil { c.mLock.Lock() @@ -545,12 +549,11 @@ func (c *Client) Do(req *Request, resp *Response) error { startCleaner = true } } + atomic.AddInt32(&hc.pendingClientRequests, 1) + defer atomic.AddInt32(&hc.pendingClientRequests, -1) c.mLock.Unlock() } - atomic.AddInt32(&hc.pendingClientRequests, 1) - defer atomic.AddInt32(&hc.pendingClientRequests, -1) - if startCleaner { go c.mCleaner(m) }