From 8486b58bdc78e4f0ee1748a55619454429b160a5 Mon Sep 17 00:00:00 2001 From: Kaushal Rohit Date: Wed, 14 Jul 2021 13:15:52 +0530 Subject: [PATCH 1/2] Added RetryConditions to Request --- client.go | 3 +++ request.go | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 36b9f9ce..bd94bbcd 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 41709cf9..b6dd0f5d 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 @@ -577,6 +578,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 after all retry conditions of the client. +func (r *Request) AddRetryCondition(condition RetryConditionFunc) *Request { + r.retryConditions = append(r.retryConditions, condition) + return r +} + //‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ // HTTP request tracing //_______________________________________________________________________ @@ -747,7 +758,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.client.RetryConditions, r.retryConditions...)), RetryHooks(r.client.RetryHooks), ) From 9a6409ddb3d2bef97468fa9a4527298978b5741e Mon Sep 17 00:00:00 2001 From: Kaushal Rohit Date: Tue, 14 Sep 2021 13:59:34 +0530 Subject: [PATCH 2/2] Request RetryConditions should be checked before the clients --- request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/request.go b/request.go index b6dd0f5d..d5310a64 100644 --- a/request.go +++ b/request.go @@ -582,7 +582,7 @@ func (r *Request) SetCookies(rs []*http.Cookie) *Request { // 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 after all retry conditions of the client. +// 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 @@ -758,7 +758,7 @@ func (r *Request) Execute(method, url string) (*Response, error) { Retries(r.client.RetryCount), WaitTime(r.client.RetryWaitTime), MaxWaitTime(r.client.RetryMaxWaitTime), - RetryConditions(append(r.client.RetryConditions, r.retryConditions...)), + RetryConditions(append(r.retryConditions, r.client.RetryConditions...)), RetryHooks(r.client.RetryHooks), )