From 245c6d24a1e8e47e38eb130da14961bef758cbce Mon Sep 17 00:00:00 2001 From: john Date: Sat, 25 Jul 2020 11:24:35 +0800 Subject: [PATCH] improve ghttp.Client --- net/ghttp/ghttp_client_request.go | 4 ++++ net/ghttp/ghttp_client_response.go | 3 +++ 2 files changed, 7 insertions(+) diff --git a/net/ghttp/ghttp_client_request.go b/net/ghttp/ghttp_client_request.go index 45c84c2b7..0c1a6251a 100644 --- a/net/ghttp/ghttp_client_request.go +++ b/net/ghttp/ghttp_client_request.go @@ -227,6 +227,10 @@ func (c *Client) DoRequest(method, url string, data ...interface{}) (resp *Clien req.Body = utils.NewReadCloser(reqBodyContent, false) for { if resp.Response, err = c.Do(req); err != nil { + // The response might not be nil when err != nil. + if resp.Response != nil { + resp.Response.Body.Close() + } if c.retryCount > 0 { c.retryCount-- time.Sleep(c.retryInterval) diff --git a/net/ghttp/ghttp_client_response.go b/net/ghttp/ghttp_client_response.go index a1c391b4a..30995be5b 100644 --- a/net/ghttp/ghttp_client_response.go +++ b/net/ghttp/ghttp_client_response.go @@ -63,6 +63,9 @@ func (r *ClientResponse) ReadAllString() string { // Close closes the response when it will never be used. func (r *ClientResponse) Close() error { + if r == nil || r.Response == nil || r.Response.Close { + return nil + } r.Response.Close = true return r.Response.Body.Close() }