Improve retry policy

This commit is contained in:
Robert Neumayer 2021-02-09 16:00:19 +01:00
parent 6b947edd04
commit bcd60c3895
1 changed files with 15 additions and 4 deletions

View File

@ -4,7 +4,10 @@ import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"regexp"
"sync/atomic"
"time"
"github.com/oracle/oci-go-sdk/common"
@ -23,15 +26,23 @@ type driverOCI struct {
var retryPolicy = &common.RetryPolicy{
MaximumNumberAttempts: 10,
ShouldRetryOperation: func(res common.OCIOperationResponse) bool {
if res.Error != nil {
return true
var e common.ServiceError
if errors.As(res.Error, &e) {
if e.GetHTTPStatusCode() == 429 || e.GetHTTPStatusCode() == 500 || e.GetHTTPStatusCode() == 503 {
return true
}
}
return false
},
NextDuration: func(common.OCIOperationResponse) time.Duration {
return 2 * time.Second
NextDuration: func(res common.OCIOperationResponse) time.Duration {
x := uint64(res.AttemptNumber)
d := time.Duration(math.Pow(2, float64(atomic.LoadUint64(&x)))) * time.Second
j := time.Duration(rand.Float64()*(2000)) * time.Millisecond
w := d + j
return w
},
}
var requestMetadata = common.RequestMetadata{
RetryPolicy: retryPolicy,
}