Skip to content

Commit d598157

Browse files
authored
Merge pull request #918 from go-resty/buffer-reset-issue
2 parents feedf18 + 253540b commit d598157

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<p align="center"><a href="#features">Features</a> section describes in detail about Resty capabilities</p>
55
</p>
66
<p align="center">
7-
<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.0-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>
7+
<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>
88
</p>
99

1010
## News
1111

12-
* v2.16.0 [released](https://github.com/go-resty/resty/releases/tag/v2.16.0) and tagged on Nov 10, 2024.
12+
* v2.16.1 [released](https://github.com/go-resty/resty/releases/tag/v2.16.1) and tagged on Nov 19, 2024.
1313
* v2.0.0 [released](https://github.com/go-resty/resty/releases/tag/v2.0.0) and tagged on Jul 16, 2019.
1414
* v1.12.0 [released](https://github.com/go-resty/resty/releases/tag/v1.12.0) and tagged on Feb 27, 2019.
1515
* 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.
105105

106106
```bash
107107
# Go Modules
108-
require github.com/go-resty/resty/v2 v2.16.0
108+
require github.com/go-resty/resty/v2 v2.16.1
109109
```
110110

111111
## Usage

request_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"io"
1313
"net"
1414
"net/http"
15+
"net/http/httptest"
1516
"net/url"
1617
"os"
1718
"path/filepath"
1819
"strconv"
1920
"strings"
21+
"sync"
2022
"testing"
2123
"time"
2224

@@ -2195,3 +2197,40 @@ func TestSetResultMustNotPanicOnNil(t *testing.T) {
21952197
}()
21962198
dc().R().SetResult(nil)
21972199
}
2200+
2201+
func TestRequestGH917(t *testing.T) {
2202+
// Mock server returns 500 status code to cause client retries.
2203+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2204+
b, err := io.ReadAll(r.Body)
2205+
assertError(t, err)
2206+
if len(b) > 0 {
2207+
// sometimes, the body is "testtest" instead of "test"
2208+
assertEqual(t, "test", string(b))
2209+
}
2210+
w.WriteHeader(http.StatusInternalServerError)
2211+
}))
2212+
2213+
client := New().AddRetryCondition(
2214+
func(r *Response, err error) bool {
2215+
return err != nil || r.StatusCode() > 499
2216+
},
2217+
).SetRetryCount(3)
2218+
2219+
wg := sync.WaitGroup{}
2220+
// Run tests concurrently to make the issue easily to observe.
2221+
for i := 0; i < 100; i++ {
2222+
wg.Add(1)
2223+
go func() {
2224+
defer wg.Done()
2225+
for j := 0; j < 10; j++ {
2226+
buf := bytes.NewBufferString("test")
2227+
// Trigger some retries
2228+
resp, err := client.R().SetBody(buf).SetContentLength(true).Execute(http.MethodPost, srv.URL)
2229+
assertNil(t, err)
2230+
assertEqual(t, http.StatusInternalServerError, resp.StatusCode())
2231+
assertEqual(t, "", string(resp.Body()))
2232+
}
2233+
}()
2234+
}
2235+
wg.Wait()
2236+
}

resty.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// Version # of resty
17-
const Version = "2.16.0"
17+
const Version = "2.16.1"
1818

1919
// New method creates a new Resty client.
2020
func New() *Client {

util.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,17 @@ func functionName(i interface{}) string {
286286
}
287287

288288
func acquireBuffer() *bytes.Buffer {
289-
return bufPool.Get().(*bytes.Buffer)
289+
buf := bufPool.Get().(*bytes.Buffer)
290+
if buf.Len() == 0 {
291+
buf.Reset()
292+
return buf
293+
}
294+
bufPool.Put(buf)
295+
return new(bytes.Buffer)
290296
}
291297

292298
func releaseBuffer(buf *bytes.Buffer) {
293299
if buf != nil {
294-
buf.Reset()
295300
bufPool.Put(buf)
296301
}
297302
}

0 commit comments

Comments
 (0)