fix: Avoid Income globalStats twice upon error (#17263)

This commit is contained in:
jiuker 2023-05-22 22:42:27 +08:00 committed by GitHub
parent 2920b0fc6d
commit b1b00a5055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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()