Skip to content

Commit 16d3180

Browse files
authored
Fix retry logic sending empty request bodies on subsequent retries (grafana#109)
* Copy body reader into memory if we are retrying requests * Add nilcheck and adjust comment
1 parent 3881f5c commit 16d3180

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

client.go

+11
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,19 @@ func (c *Client) request(method, requestPath string, query url.Values, body io.R
7171
bodyContents []byte
7272
)
7373

74+
// If we want to retry a request that sends data, we'll need to stash the request data in memory. Otherwise, we lose it since readers cannot be replayed.
75+
var bodyBuffer bytes.Buffer
76+
if c.config.NumRetries > 0 && body != nil {
77+
body = io.TeeReader(body, &bodyBuffer)
78+
}
79+
7480
// retry logic
7581
for n := 0; n <= c.config.NumRetries; n++ {
82+
// If it's not the first request, re-use the request body we stashed earlier.
83+
if n > 0 {
84+
body = bytes.NewReader(bodyBuffer.Bytes())
85+
}
86+
7687
req, err = c.newRequest(method, requestPath, query, body)
7788
if err != nil {
7889
return err

0 commit comments

Comments
 (0)