@@ -94,6 +94,7 @@ type Request struct {
94
94
multipartBoundary string
95
95
multipartFields []* MultipartField
96
96
retryConditions []RetryConditionFunc
97
+ retryHooks []RetryHookFunc
97
98
resultCurlCmd string
98
99
generateCurlCmd bool
99
100
debugLogCurlCmd bool
@@ -945,14 +946,43 @@ func (r *Request) SetDebug(d bool) *Request {
945
946
return r
946
947
}
947
948
948
- // AddRetryCondition method adds a retry condition function to the request's
949
- // array of functions is checked to determine if the request can be retried.
950
- // The request will retry if any functions return true and the error is nil .
949
+ // AddRetryConditions method adds one or more retry condition functions into the request.
950
+ // These retry conditions are executed to determine if the request can be retried.
951
+ // The request will retry if any functions return ` true`, otherwise return `false` .
951
952
//
952
- // NOTE: The request level retry conditions are checked before all retry
953
- // conditions from the client instance.
954
- func (r * Request ) AddRetryCondition (condition RetryConditionFunc ) * Request {
955
- r .retryConditions = append (r .retryConditions , condition )
953
+ // NOTE:
954
+ // - The client-level retry conditions are applied to all requests.
955
+ // - The request-level retry conditions are executed first before client-level
956
+ // retry conditions. See [Request.SetRetryConditions]
957
+ func (r * Request ) AddRetryConditions (conditions ... RetryConditionFunc ) * Request {
958
+ r .retryConditions = append (r .retryConditions , conditions ... )
959
+ return r
960
+ }
961
+
962
+ // SetRetryConditions method overwrites the retry conditions in the request.
963
+ // These retry conditions are executed to determine if the request can be retried.
964
+ // The request will retry if any function returns `true`, otherwise return `false`.
965
+ func (r * Request ) SetRetryConditions (conditions ... RetryConditionFunc ) * Request {
966
+ r .retryConditions = conditions
967
+ return r
968
+ }
969
+
970
+ // AddRetryHooks method adds one or more side-effecting retry hooks in the request.
971
+ //
972
+ // NOTE:
973
+ // - All the retry hooks are executed on each request retry.
974
+ // - The request-level retry hooks are executed first before client-level hooks.
975
+ func (r * Request ) AddRetryHooks (hooks ... RetryHookFunc ) * Request {
976
+ r .retryHooks = append (r .retryHooks , hooks ... )
977
+ return r
978
+ }
979
+
980
+ // SetRetryHooks method overwrites side-effecting retry hooks in the request.
981
+ //
982
+ // NOTE:
983
+ // - All the retry hooks are executed on each request retry.
984
+ func (r * Request ) SetRetryHooks (hooks ... RetryHookFunc ) * Request {
985
+ r .retryHooks = hooks
956
986
return r
957
987
}
958
988
@@ -1355,8 +1385,7 @@ func (r *Request) Execute(method, url string) (res *Response, err error) {
1355
1385
// is still false
1356
1386
if ! needsRetry && res != nil {
1357
1387
// user defined retry conditions
1358
- retryConditions := append (r .retryConditions , r .client .RetryConditions ()... )
1359
- for _ , retryCondition := range retryConditions {
1388
+ for _ , retryCondition := range r .retryConditions {
1360
1389
if needsRetry = retryCondition (res , err ); needsRetry {
1361
1390
break
1362
1391
}
@@ -1375,7 +1404,7 @@ func (r *Request) Execute(method, url string) (res *Response, err error) {
1375
1404
}
1376
1405
1377
1406
// run user-defined retry hooks
1378
- for _ , retryHookFunc := range r .client . RetryHooks () {
1407
+ for _ , retryHookFunc := range r .retryHooks {
1379
1408
retryHookFunc (res , err )
1380
1409
}
1381
1410
@@ -1393,11 +1422,11 @@ func (r *Request) Execute(method, url string) (res *Response, err error) {
1393
1422
select {
1394
1423
case <- r .Context ().Done ():
1395
1424
isCtxDone = true
1396
- timer .Stop ()
1397
1425
err = wrapErrors (r .Context ().Err (), err )
1398
1426
break
1399
1427
case <- timer .C :
1400
1428
}
1429
+ timer .Stop ()
1401
1430
if isCtxDone {
1402
1431
break
1403
1432
}
0 commit comments