From 9a9930f32906ed5995e1b3c7c607b7c310c55b09 Mon Sep 17 00:00:00 2001
From: Jeevanandam M
Date: Thu, 21 Nov 2024 19:36:16 -0800
Subject: [PATCH 1/2] fix: buffer reuse and release #917
---
client.go | 1 -
request.go | 2 ++
request_test.go | 6 ++----
util.go | 30 ++++--------------------------
4 files changed, 8 insertions(+), 31 deletions(-)
diff --git a/client.go b/client.go
index 48c69f1..7ab33ae 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 9075ead..6c6f925 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 2d1e5bf..6bdf198 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/util.go b/util.go
index 9f58a3f..0ac470e 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())
From 43d5ecacff602fd9ed5d548b8e946ba413b373d2 Mon Sep 17 00:00:00 2001
From: Jeevanandam M
Date: Thu, 21 Nov 2024 19:40:23 -0800
Subject: [PATCH 2/2] release: version bump and readme update for v2.16.2
---
README.md | 6 +++---
resty.go | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index e513b24..7592ca9 100644
--- a/README.md
+++ b/README.md
@@ -4,12 +4,12 @@
Features section describes in detail about Resty capabilities
-
+
## 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/resty.go b/resty.go
index 450aa66..92ed4eb 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 {