From b1b00a5055fc5658dde8b9759cdd94d67afcd3e3 Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Mon, 22 May 2023 22:42:27 +0800 Subject: [PATCH] fix: Avoid Income globalStats twice upon error (#17263) --- internal/rest/client.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/rest/client.go b/internal/rest/client.go index 047fbedf9..e7e6485a1 100644 --- a/internal/rest/client.go +++ b/internal/rest/client.go @@ -201,25 +201,30 @@ func (c *Client) newRequest(ctx context.Context, u *url.URL, body io.Reader) (*h type respBodyMonitor struct { io.ReadCloser - expectTimeouts bool + expectTimeouts bool + errorStatusOnce sync.Once } -func (r respBodyMonitor) Read(p []byte) (n int, err error) { +func (r *respBodyMonitor) Read(p []byte) (n int, err error) { n, err = r.ReadCloser.Read(p) - if xnet.IsNetworkOrHostDown(err, r.expectTimeouts) { - atomic.AddUint64(&globalStats.errs, 1) - } + r.errorStatus(err) return } -func (r respBodyMonitor) Close() (err error) { +func (r *respBodyMonitor) Close() (err error) { err = r.ReadCloser.Close() - if xnet.IsNetworkOrHostDown(err, r.expectTimeouts) { - atomic.AddUint64(&globalStats.errs, 1) - } + r.errorStatus(err) return } +func (r *respBodyMonitor) errorStatus(err error) { + if xnet.IsNetworkOrHostDown(err, r.expectTimeouts) { + r.errorStatusOnce.Do(func() { + atomic.AddUint64(&globalStats.errs, 1) + }) + } +} + // Call - make a REST call with context. func (c *Client) Call(ctx context.Context, method string, values url.Values, body io.Reader, length int64) (reply io.ReadCloser, err error) { urlStr := c.url.String()