Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone the body during backoff/retry #127

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package fauna

import (
"bytes"
"context"
_ "embed"
"fmt"
Expand Down Expand Up @@ -175,26 +176,61 @@ func NewClient(secret string, timeouts Timeouts, configFns ...ClientConfigFn) *C
return client
}

func (c *Client) doWithRetry(req *http.Request, attemptNumber int) (attempts int, r *http.Response, err error) {
attempts = attemptNumber
r, err = c.http.Do(req)
if err != nil {
return
func (c *Client) doWithRetry(req *http.Request) (attempts int, r *http.Response, err error) {
req2 := req.Clone(req.Context())
body, rerr := io.ReadAll(req.Body)
if rerr != nil {
return attempts, r, rerr
}

if attemptNumber <= c.maxAttempts {
switch r.StatusCode {
case http.StatusTooManyRequests:
defer r.Body.Close()
if _, err = io.Copy(io.Discard, io.LimitReader(r.Body, 4096)); err != nil {
cerr := req.Body.Close()
if cerr != nil {
return attempts, r, cerr
}

for {
shouldRetry := false

// Ensure we have a fresh body for the request
req2.Body = io.NopCloser(bytes.NewReader(body))
r, err = c.http.Do(req2)
attempts++
if err != nil {
return
}

if r != nil {
switch r.StatusCode {
case http.StatusTooManyRequests:
shouldRetry = true
}
}

if attempts >= c.maxAttempts || !shouldRetry {
return attempts, r, err
}

// We're going to retry, so drain the response
if r != nil {
err = c.drainResponse(r.Body)
if err != nil {
return
}
}

time.Sleep(c.backoff(attemptNumber))
_, r, err = c.doWithRetry(req, attemptNumber+1)
timer := time.NewTimer(c.backoff(attempts))
select {
case <-req.Context().Done():
timer.Stop()
return attempts, r, req.Context().Err()
case <-timer.C:
}
}
}

func (c *Client) drainResponse(body io.ReadCloser) (err error) {
defer body.Close()
_, err = io.Copy(io.Discard, io.LimitReader(body, 4096))
return
}

Expand Down
7 changes: 6 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ type ErrThrottling struct {
*ErrFauna
}

func getErrFauna(httpStatus int, res *queryResponse) error {
func getErrFauna(httpStatus int, res *queryResponse, attempts int) error {
if res.Error != nil {
res.Error.QueryInfo = newQueryInfo(res)

if res.Error.QueryInfo.Stats != nil {
res.Error.QueryInfo.Stats.Attempts = attempts
}

res.Error.StatusCode = httpStatus
}

Expand Down
2 changes: 1 addition & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestGetErrFauna(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := &queryResponse{Error: tt.args.serviceError, Summary: ""}
err := getErrFauna(tt.args.httpStatus, res)
err := getErrFauna(tt.args.httpStatus, res, 1)
if tt.wantErr {
assert.ErrorAs(t, err, &tt.args.errType)
assert.NotZero(t, res.Error.StatusCode)
Expand Down
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *Client) do(request *fqlRequest) (*QuerySuccess, error) {
req.Header.Set(k, v)
}

attempts, r, doErr := c.doWithRetry(req, 0)
attempts, r, doErr := c.doWithRetry(req)
if doErr != nil {
return nil, ErrNetwork(fmt.Errorf("network error: %w", doErr))
}
Expand All @@ -94,7 +94,7 @@ func (c *Client) do(request *fqlRequest) (*QuerySuccess, error) {
c.lastTxnTime.sync(res.TxnTime)
res.Header = r.Header

if serviceErr := getErrFauna(r.StatusCode, &res); serviceErr != nil {
if serviceErr := getErrFauna(r.StatusCode, &res, attempts); serviceErr != nil {
return nil, serviceErr
}

Expand Down