Skip to content

Commit

Permalink
GH #352 Expose already calculated retry attempt value
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 9, 2020
1 parent d2ba00f commit 29cc550
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
34 changes: 19 additions & 15 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@ import (
// resty client. Request provides an options to override client level
// settings and also an options for the request composition.
type Request struct {
URL string
Method string
Token string
AuthScheme string
QueryParam url.Values
FormData url.Values
Header http.Header
Time time.Time
Body interface{}
Result interface{}
Error interface{}
RawRequest *http.Request
SRV *SRVRecord
UserInfo *User
Cookies []*http.Cookie
URL string
Method string
Token string
AuthScheme string
QueryParam url.Values
FormData url.Values
Header http.Header
Time time.Time
Body interface{}
Result interface{}
Error interface{}
RawRequest *http.Request
SRV *SRVRecord
UserInfo *User
Cookies []*http.Cookie
RetryAttempt int

isMultiPart bool
isFormData bool
Expand Down Expand Up @@ -582,6 +583,7 @@ func (r *Request) TraceInfo() TraceInfo {
IsConnReused: ct.gotConnInfo.Reused,
IsConnWasIdle: ct.gotConnInfo.WasIdle,
ConnIdleTime: ct.gotConnInfo.IdleTime,
RetryAttempt: r.RetryAttempt,
}

if ct.gotConnInfo.Reused {
Expand Down Expand Up @@ -704,6 +706,8 @@ func (r *Request) Execute(method, url string) (*Response, error) {
RetryConditions(r.client.RetryConditions),
)

r.RetryAttempt = attempt

return resp, unwrapNoRetryErr(err)
}

Expand Down
5 changes: 4 additions & 1 deletion retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ func TestClientRetryWaitCallbackSwitchToDefault(t *testing.T) {
}

c := dc().
EnableTrace().
SetRetryCount(retryCount).
SetRetryWaitTime(retryWaitTime).
SetRetryMaxWaitTime(retryMaxWaitTime).
Expand All @@ -456,10 +457,12 @@ func TestClientRetryWaitCallbackSwitchToDefault(t *testing.T) {
return true
},
)
_, _ = c.R().Get(ts.URL + "/set-retrywaittime-test")
resp, _ := c.R().Get(ts.URL + "/set-retrywaittime-test")

// 6 attempts were made
assertEqual(t, attempt, 6)
assertEqual(t, resp.Request.RetryAttempt, 6)
assertEqual(t, resp.Request.TraceInfo().RetryAttempt, 6)

// Initial attempt has 0 time slept since last request
assertEqual(t, retryIntervals[0], uint64(0))
Expand Down
4 changes: 4 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type TraceInfo struct {
// ConnIdleTime is a duration how long the connection was previously
// idle, if IsConnWasIdle is true.
ConnIdleTime time.Duration

// RetryAttempt is to represent the retry attempt made during a Resty
// request execution flow when using the Resty retry feature.
RetryAttempt int
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand Down

0 comments on commit 29cc550

Please sign in to comment.