From 2cae8cd4a15dd632385b5781ff08e8039d1cec4a Mon Sep 17 00:00:00 2001 From: Kaushal Rohit Date: Thu, 16 Sep 2021 10:25:39 +0530 Subject: [PATCH] Added RetryConditions option to Request level (#436) --- client.go | 3 +++ request.go | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 55709b47..34e423d7 100644 --- a/client.go +++ b/client.go @@ -589,6 +589,9 @@ func (c *Client) SetRetryAfter(callback RetryAfterFunc) *Client { // AddRetryCondition method adds a retry condition function to array of functions // that are checked to determine if the request is retried. The request will // retry if any of the functions return true and error is nil. +// +// Note: These retry conditions are applied on all Request made using this Client. +// For Request specific retry conditions check *Request.AddRetryCondition func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client { c.RetryConditions = append(c.RetryConditions, condition) return c diff --git a/request.go b/request.go index bf857e57..52166e69 100644 --- a/request.go +++ b/request.go @@ -67,6 +67,7 @@ type Request struct { clientTrace *clientTrace multipartFiles []*File multipartFields []*MultipartField + retryConditions []RetryConditionFunc } // Context method returns the Context if its already set in request @@ -593,6 +594,16 @@ func (r *Request) SetCookies(rs []*http.Cookie) *Request { return r } +// AddRetryCondition method adds a retry condition function to the request's +// array of functions that are checked to determine if the request is retried. +// The request will retry if any of the functions return true and error is nil. +// +// Note: These retry conditions are checked before all retry conditions of the client. +func (r *Request) AddRetryCondition(condition RetryConditionFunc) *Request { + r.retryConditions = append(r.retryConditions, condition) + return r +} + //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ // HTTP request tracing //_______________________________________________________________________ @@ -763,7 +774,7 @@ func (r *Request) Execute(method, url string) (*Response, error) { Retries(r.client.RetryCount), WaitTime(r.client.RetryWaitTime), MaxWaitTime(r.client.RetryMaxWaitTime), - RetryConditions(r.client.RetryConditions), + RetryConditions(append(r.retryConditions, r.client.RetryConditions...)), RetryHooks(r.client.RetryHooks), )