Skip to content

Commit

Permalink
Merge pull request #919 from go-resty/fix-buffer-issue
Browse files Browse the repository at this point in the history
fix: buffer reuse and back to pool and release v2.16.2
  • Loading branch information
jeevatkm authored Nov 22, 2024
2 parents d598157 + 43d5eca commit c34e460
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<p align="center"><a href="#features">Features</a> section describes in detail about Resty capabilities</p>
</p>
<p align="center">
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Av2"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg?branch=v2" alt="Build Status"></a> <a href="https://app.codecov.io/gh/go-resty/resty/tree/v2"><img src="https://codecov.io/gh/go-resty/resty/branch/v2/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.16.1-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
<p align="center"><a href="https://github.com/go-resty/resty/actions/workflows/ci.yml?query=branch%3Av2"><img src="https://github.com/go-resty/resty/actions/workflows/ci.yml/badge.svg?branch=v2" alt="Build Status"></a> <a href="https://app.codecov.io/gh/go-resty/resty/tree/v2"><img src="https://codecov.io/gh/go-resty/resty/branch/v2/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a> <a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.16.2-blue.svg" alt="Release Version"></a> <a href="https://pkg.go.dev/github.com/go-resty/resty/v2"><img src="https://pkg.go.dev/badge/github.com/go-resty/resty" alt="GoDoc"></a> <a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a> <a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" alt="Mentioned in Awesome Go"></a></p>
</p>

## 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).
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 2 additions & 4 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))

Expand Down
2 changes: 1 addition & 1 deletion resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 4 additions & 26 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"runtime"
"sort"
"strings"
"sync"
)

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit c34e460

Please sign in to comment.