diff --git a/README.md b/README.md index e513b24e..7592ca95 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@

Features section describes in detail about Resty capabilities

-

Build Status Code Coverage Go Report Card Release Version GoDoc License Mentioned in Awesome Go

+

Build Status Code Coverage Go Report Card Release Version GoDoc License Mentioned in Awesome Go

## News - * v2.16.1 [released](https://github.com/go-resty/resty/releases/tag/v2.16.1) and tagged on Nov 19, 2024. + * v2.16.2 [released](https://github.com/go-resty/resty/releases/tag/v2.16.2) and tagged on Nov 21, 2024. * v2.0.0 [released](https://github.com/go-resty/resty/releases/tag/v2.0.0) and tagged on Jul 16, 2019. * v1.12.0 [released](https://github.com/go-resty/resty/releases/tag/v1.12.0) and tagged on Feb 27, 2019. * v1.0 released and tagged on Sep 25, 2017. - Resty's first version was released on Sep 15, 2015 then it grew gradually as a very handy and helpful library. Its been a two years since first release. I'm very thankful to Resty users and its [contributors](https://github.com/go-resty/resty/graphs/contributors). @@ -105,7 +105,7 @@ Resty author also published following projects for Go Community. ```bash # Go Modules -require github.com/go-resty/resty/v2 v2.16.1 +require github.com/go-resty/resty/v2 v2.16.2 ``` ## Usage diff --git a/client.go b/client.go index 48c69f19..7ab33ae8 100644 --- a/client.go +++ b/client.go @@ -1236,7 +1236,6 @@ func (c *Client) executeBefore(req *Request) error { return wrapNoRetryErr(err) } - req.RawRequest.Body = newRequestBodyReleaser(req.RawRequest.Body, req.bodyBuf) return nil } diff --git a/request.go b/request.go index 9075ead5..6c6f9259 100644 --- a/request.go +++ b/request.go @@ -1030,6 +1030,7 @@ func (r *Request) Execute(method, url string) (*Response, error) { r.Attempt = 1 resp, err = r.client.execute(r) r.client.onErrorHooks(r, resp, unwrapNoRetryErr(err)) + backToBufPool(r.bodyBuf) return resp, unwrapNoRetryErr(err) } @@ -1059,6 +1060,7 @@ func (r *Request) Execute(method, url string) (*Response, error) { } r.client.onErrorHooks(r, resp, unwrapNoRetryErr(err)) + backToBufPool(r.bodyBuf) return resp, unwrapNoRetryErr(err) } diff --git a/request_test.go b/request_test.go index 2d1e5bf4..6bdf198d 100644 --- a/request_test.go +++ b/request_test.go @@ -2203,10 +2203,8 @@ func TestRequestGH917(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { b, err := io.ReadAll(r.Body) assertError(t, err) - if len(b) > 0 { - // sometimes, the body is "testtest" instead of "test" - assertEqual(t, "test", string(b)) - } + assertEqual(t, "test", string(b)) + w.WriteHeader(http.StatusInternalServerError) })) diff --git a/resty.go b/resty.go index 450aa66a..92ed4eb8 100644 --- a/resty.go +++ b/resty.go @@ -14,7 +14,7 @@ import ( ) // Version # of resty -const Version = "2.16.1" +const Version = "2.16.2" // New method creates a new Resty client. func New() *Client { diff --git a/util.go b/util.go index 9f58a3f5..0ac470e4 100644 --- a/util.go +++ b/util.go @@ -19,7 +19,6 @@ import ( "runtime" "sort" "strings" - "sync" ) //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ @@ -297,38 +296,17 @@ func acquireBuffer() *bytes.Buffer { func releaseBuffer(buf *bytes.Buffer) { if buf != nil { + buf.Reset() bufPool.Put(buf) } } -// requestBodyReleaser wraps requests's body and implements custom Close for it. -// The Close method closes original body and releases request body back to sync.Pool. -type requestBodyReleaser struct { - releaseOnce sync.Once - reqBuf *bytes.Buffer - io.ReadCloser -} - -func newRequestBodyReleaser(respBody io.ReadCloser, reqBuf *bytes.Buffer) io.ReadCloser { - if reqBuf == nil { - return respBody - } - - return &requestBodyReleaser{ - reqBuf: reqBuf, - ReadCloser: respBody, +func backToBufPool(buf *bytes.Buffer) { + if buf != nil { + bufPool.Put(buf) } } -func (rr *requestBodyReleaser) Close() error { - err := rr.ReadCloser.Close() - rr.releaseOnce.Do(func() { - releaseBuffer(rr.reqBuf) - }) - - return err -} - func closeq(v interface{}) { if c, ok := v.(io.Closer); ok { silently(c.Close())