mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-12 15:51:01 +00:00
rewind Body on retry
this is really not ideal, and there are libraries for this kind of thing, but I'd have to figure out how to make the libraries work with everywhere we're already creating stdlib http clients.
This commit is contained in:
parent
cdf4bc4c88
commit
3105a24542
1 changed files with 20 additions and 0 deletions
|
|
@ -1745,13 +1745,33 @@ func giveRawResponse(b bool) executeRequestOption {
|
|||
}
|
||||
}
|
||||
|
||||
type nopCloser struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (n nopCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *InternalClient) doWithRetry(req *http.Request) (*http.Response, error) {
|
||||
sleepDuration := time.Second
|
||||
newBody := nopCloser{}
|
||||
if req.Body != nil {
|
||||
bod, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "reading body")
|
||||
}
|
||||
newBody.Reader = bytes.NewReader(bod)
|
||||
req.Body = newBody
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
// start timer after first request, so if retryPeriod > 0 we
|
||||
// pretty much always do at least one retry
|
||||
start := time.Now()
|
||||
for ; err != nil || resp.StatusCode < 200 || resp.StatusCode >= 300; resp, err = c.httpClient.Do(req) {
|
||||
if newBody.Reader != nil {
|
||||
newBody.Seek(0, io.SeekStart)
|
||||
}
|
||||
if time.Since(start) > c.retryPeriod {
|
||||
break
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue