Skip to content

Commit

Permalink
feat: add retry settings method at request level #752
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 15, 2024
1 parent 25571df commit 860c55e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ func (c *Client) R() *Request {
responseBodyLimit: c.responseBodyLimit,
generateCurlOnDebug: c.generateCurlOnDebug,
}

r.retryConditions = append(r.retryConditions, c.retryConditions...)
return r
}

Expand Down
34 changes: 32 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,36 @@ func (r *Request) AddRetryCondition(condition RetryConditionFunc) *Request {
return r
}

// SetRetryCount method enables retry on Resty client and allows you
// to set no. of retry count. Resty uses a Backoff mechanism.
func (r *Request) SetRetryCount(count int) *Request {
r.RetryCount = count
return r
}

// SetRetryWaitTime method sets the default wait time for sleep before retrying
//
// Default is 100 milliseconds.
func (r *Request) SetRetryWaitTime(waitTime time.Duration) *Request {
r.RetryWaitTime = waitTime
return r
}

// SetRetryMaxWaitTime method sets the max wait time for sleep before retrying
//
// Default is 2 seconds.
func (r *Request) SetRetryMaxWaitTime(maxWaitTime time.Duration) *Request {
r.RetryMaxWaitTime = maxWaitTime
return r
}

// SetRetryResetReaders method enables the Resty client to seek the start of all
// file readers are given as multipart files if the object implements [io.ReadSeeker].
func (r *Request) SetRetryResetReaders(b bool) *Request {
r.RetryResetReaders = b
return r
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// HTTP request tracing
//_______________________________________________________________________
Expand Down Expand Up @@ -1031,7 +1061,7 @@ func (r *Request) Execute(method, url string) (*Response, error) {
r.Method = method
r.URL = r.selectAddr(addrs, url, 0)

if r.client.retryCount == 0 {
if r.RetryCount == 0 {
r.Attempt = 1
resp, err = r.client.execute(r)
r.client.onErrorHooks(r, resp, unwrapNoRetryErr(err))
Expand All @@ -1054,7 +1084,7 @@ func (r *Request) Execute(method, url string) (*Response, error) {
Retries(r.RetryCount),
WaitTime(r.RetryWaitTime),
MaxWaitTime(r.RetryMaxWaitTime),
RetryConditions(append(r.retryConditions, r.client.RetryConditions()...)),
RetryConditions(r.retryConditions),
RetryHooks(r.client.RetryHooks()),
ResetMultipartReaders(r.RetryResetReaders),
)
Expand Down
48 changes: 43 additions & 5 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func TestConditionalGet(t *testing.T) {
logResponse(t, resp)
}

// Check to make sure the package Function works.
func TestConditionalGetDefaultClient(t *testing.T) {
func TestConditionalGetRequestLevel(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
attemptCount := 1
Expand All @@ -173,9 +172,12 @@ func TestConditionalGetDefaultClient(t *testing.T) {

// Clear the default client.
client := dc()
// Proceed to check.
client.AddRetryCondition(check).SetRetryCount(1)
resp, err := client.R().
AddRetryCondition(check).
SetRetryCount(1).
SetRetryWaitTime(time.Duration(50)*time.Millisecond).
SetRetryMaxWaitTime(time.Duration(1)*time.Second).
SetRetryResetReaders(true).
SetQueryParam("request_no", strconv.FormatInt(time.Now().Unix(), 10)).
Get(ts.URL + "/")

Expand Down Expand Up @@ -800,7 +802,7 @@ func TestResetMultipartReaderSeekStartError(t *testing.T) {
assertEqual(t, err.Error(), errSeekFailure.Error())
}

func TestResetMultipartReaders(t *testing.T) {
func TestClientResetMultipartReaders(t *testing.T) {
ts := createFilePostServer(t)
defer ts.Close()

Expand Down Expand Up @@ -834,3 +836,39 @@ func TestResetMultipartReaders(t *testing.T) {
assertEqual(t, 500, resp.StatusCode())
assertNil(t, err)
}

func TestRequestResetMultipartReaders(t *testing.T) {
ts := createFilePostServer(t)
defer ts.Close()

str := "test"
buf := []byte(str)

bufReader := bytes.NewReader(buf)
bufCpy := make([]byte, len(buf))

c := dc().
SetTimeout(time.Second * 3).
AddRetryAfterErrorCondition().
AddRetryHook(
func(response *Response, _ error) {
read, err := bufReader.Read(bufCpy)

assertNil(t, err)
assertEqual(t, len(buf), read)
assertEqual(t, str, string(bufCpy))
},
)

assertEqual(t, false, c.RetryResetReaders())

req := c.R().
SetRetryCount(2).
SetRetryResetReaders(true).
SetFileReader("name", "filename", bufReader)
resp, err := req.Post(ts.URL + "/set-reset-multipart-readers-test")

assertEqual(t, true, req.RetryResetReaders)
assertEqual(t, 500, resp.StatusCode())
assertNil(t, err)
}

0 comments on commit 860c55e

Please sign in to comment.