Skip to content

Commit caf1c04

Browse files
committed
improvising retry mechanism and introducing max attempts for retry
1 parent 970f4e8 commit caf1c04

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

internal/tfresource/retry.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,35 @@ func init() {
6565
}
6666

6767
func GetRetryBackoffDuration(response oci_common.OCIOperationResponse, disableNotFoundRetries bool, service string, startTime time.Time, optionals ...interface{}) time.Duration {
68-
return getRetryBackoffDurationWithExpectedRetryDurationFn(response, disableNotFoundRetries, service, startTime, getExpectedRetryDuration, optionals...)
68+
backoffDuration := getRetryBackoffDurationWithExpectedRetryDurationFn(response, disableNotFoundRetries, service, startTime, getExpectedRetryDuration, optionals...)
69+
// Do nothing if service specific retry duration exists
70+
if _, ok := serviceExpectedRetryDurationMap[service]; ok {
71+
return backoffDuration
72+
}
73+
if response.Response != nil && response.Response.HTTPResponse() != nil {
74+
statusCode := response.Response.HTTPResponse().StatusCode
75+
switch statusCode {
76+
case 429, 503:
77+
utils.Logf("[DEGUG] Handling Retry Timeout for API Response Error Code %d", statusCode)
78+
rawResponse := response.Response.HTTPResponse()
79+
retryAfterVal := rawResponse.Header.Get("Retry-After")
80+
if retryAfterVal == "" {
81+
retryAfterVal = rawResponse.Header.Get("retry-after")
82+
}
83+
if retryAfterVal != "" {
84+
if waitTime, err := strconv.Atoi(retryAfterVal); err == nil {
85+
backoffDuration = time.Duration(waitTime) * time.Second
86+
utils.Logf("[DEGUG] Retry Timeout obtained from response header is %s", backoffDuration)
87+
88+
} else {
89+
utils.Logf("[DEGUG] Error while reading retry-after from response header. Obtained retry-after value is %s. Proceeding with default retry time %s", retryAfterVal, backoffDuration)
90+
}
91+
} else {
92+
utils.Logf("[DEGUG] retry-after value not found in header to handle API Response Error Code %d. Proceeding with default retry time %s", statusCode, backoffDuration)
93+
}
94+
}
95+
}
96+
return backoffDuration
6997
}
7098

7199
func getRetryBackoffDurationWithExpectedRetryDurationFn(response oci_common.OCIOperationResponse, disableNotFoundRetries bool, service string, startTime time.Time, expectedRetryDurationFn expectedRetryDurationFn, optionals ...interface{}) time.Duration {
@@ -353,7 +381,7 @@ func GetRetryPolicy(disableNotFoundRetries bool, service string, optionals ...in
353381
func getDefaultRetryPolicy(disableNotFoundRetries bool, service string, optionals ...interface{}) *oci_common.RetryPolicy {
354382
startTime := time.Now()
355383
retryPolicy := &oci_common.RetryPolicy{
356-
MaximumNumberAttempts: 0,
384+
MaximumNumberAttempts: 10,
357385
ShouldRetryOperation: func(response oci_common.OCIOperationResponse) bool {
358386
return ShouldRetry(response, disableNotFoundRetries, service, startTime, optionals...)
359387
},

0 commit comments

Comments
 (0)