@@ -10,7 +10,7 @@ import (
1010 "github.com/stackitcloud/stackit-sdk-go/core/utils"
1111)
1212
13- var RetryHttpErrorStatusCodes = []int {http .StatusBadGateway , http .StatusGatewayTimeout }
13+ var RetryHttpErrorStatusCodes = []int {http .StatusBadGateway , http .StatusGatewayTimeout , http . StatusServiceUnavailable }
1414
1515// AsyncActionCheck reports whether a specific async action has finished.
1616// - waitFinished == true if the async action is finished, false otherwise.
@@ -20,25 +20,33 @@ type AsyncActionCheck[T any] func() (waitFinished bool, response *T, err error)
2020
2121// AsyncActionHandler handles waiting for a specific async action to be finished.
2222type AsyncActionHandler [T any ] struct {
23- checkFn AsyncActionCheck [T ]
24- sleepBeforeWait time.Duration
25- throttle time.Duration
26- timeout time.Duration
27- tempErrRetryLimit int
28- IntermediateStateReached bool
23+ checkFn AsyncActionCheck [T ]
24+ sleepBeforeWait time.Duration
25+ throttle time.Duration
26+ timeout time.Duration
27+ tempErrRetryLimit int
28+ IntermediateStateReached bool
29+ retryHttpErrorStatusCodes []int
2930}
3031
3132// New initializes an AsyncActionHandler
3233func New [T any ](f AsyncActionCheck [T ]) * AsyncActionHandler [T ] {
3334 return & AsyncActionHandler [T ]{
34- checkFn : f ,
35- sleepBeforeWait : 0 * time .Second ,
36- throttle : 5 * time .Second ,
37- timeout : 30 * time .Minute ,
38- tempErrRetryLimit : 5 ,
35+ checkFn : f ,
36+ sleepBeforeWait : 0 * time .Second ,
37+ throttle : 5 * time .Second ,
38+ timeout : 30 * time .Minute ,
39+ tempErrRetryLimit : 5 ,
40+ retryHttpErrorStatusCodes : RetryHttpErrorStatusCodes ,
3941 }
4042}
4143
44+ // SetRetryHttpErrorStatusCodes sets the retry codes to use for retry.
45+ func (h * AsyncActionHandler [T ]) SetRetryHttpErrorStatusCodes (c []int ) * AsyncActionHandler [T ] {
46+ h .retryHttpErrorStatusCodes = c
47+ return h
48+ }
49+
4250// SetThrottle sets the time interval between each check of the async action.
4351func (h * AsyncActionHandler [T ]) SetThrottle (d time.Duration ) * AsyncActionHandler [T ] {
4452 h .throttle = d
@@ -114,7 +122,7 @@ func (h *AsyncActionHandler[T]) handleError(retryTempErrorCounter int, err error
114122 return retryTempErrorCounter , fmt .Errorf ("found non-GenericOpenApiError: %w" , err )
115123 }
116124 // Some APIs may return temporary errors and the request should be retried
117- if ! utils .Contains (RetryHttpErrorStatusCodes , oapiErr .StatusCode ) {
125+ if ! utils .Contains (h . retryHttpErrorStatusCodes , oapiErr .StatusCode ) {
118126 return retryTempErrorCounter , err
119127 }
120128 retryTempErrorCounter ++
0 commit comments