-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
request_builder_retry.go
68 lines (58 loc) · 2.92 KB
/
request_builder_retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package fastshot
import "time"
// BuilderRequestRetry is the interface that wraps the basic methods for setting Request maxAttempts.
var _ BuilderRequestRetry[RequestBuilder] = (*RequestRetryBuilder)(nil)
// RequestRetryBuilder serves as the main entry point for configuring Request maxAttempts.
type RequestRetryBuilder struct {
parentBuilder *RequestBuilder
requestConfig *RequestConfigBase
}
// Retry returns a new RequestRetryBuilder for setting custom HTTP CookiesWrapper.
func (b *RequestBuilder) Retry() *RequestRetryBuilder {
return &RequestRetryBuilder{
parentBuilder: b,
requestConfig: b.request.config,
}
}
// SetConstantBackoff sets the retry interval and maximum attempts.
func (r RequestRetryBuilder) SetConstantBackoff(interval time.Duration, maxAttempts uint) *RequestBuilder {
r.requestConfig.RetryConfig().SetInterval(interval)
r.requestConfig.RetryConfig().SetMaxAttempts(maxAttempts)
r.requestConfig.RetryConfig().SetBackoffRate(1)
r.requestConfig.RetryConfig().SetJitterStrategy(JitterStrategyNone)
return r.parentBuilder
}
// SetConstantBackoffWithJitter sets the retry interval and maximum attempts.
func (r RequestRetryBuilder) SetConstantBackoffWithJitter(interval time.Duration, maxAttempts uint) *RequestBuilder {
r.requestConfig.RetryConfig().SetInterval(interval)
r.requestConfig.RetryConfig().SetMaxAttempts(maxAttempts)
r.requestConfig.RetryConfig().SetBackoffRate(1)
r.requestConfig.RetryConfig().SetJitterStrategy(JitterStrategyFull)
return r.parentBuilder
}
// SetExponentialBackoff sets the retry interval, maximum attempts, and backoff rate.
func (r RequestRetryBuilder) SetExponentialBackoff(interval time.Duration, maxAttempts uint, backoffRate float64) *RequestBuilder {
r.requestConfig.RetryConfig().SetInterval(interval)
r.requestConfig.RetryConfig().SetMaxAttempts(maxAttempts)
r.requestConfig.RetryConfig().SetBackoffRate(backoffRate)
r.requestConfig.RetryConfig().SetJitterStrategy(JitterStrategyNone)
return r.parentBuilder
}
// SetExponentialBackoffWithJitter sets the retry interval, maximum attempts, and backoff rate.
func (r RequestRetryBuilder) SetExponentialBackoffWithJitter(interval time.Duration, maxAttempts uint, backoffRate float64) *RequestBuilder {
r.requestConfig.RetryConfig().SetInterval(interval)
r.requestConfig.RetryConfig().SetMaxAttempts(maxAttempts)
r.requestConfig.RetryConfig().SetBackoffRate(backoffRate)
r.requestConfig.RetryConfig().SetJitterStrategy(JitterStrategyFull)
return r.parentBuilder
}
// WithRetryCondition sets the retry condition for the request.
func (r RequestRetryBuilder) WithRetryCondition(shouldRetry func(response *Response) bool) *RequestBuilder {
r.requestConfig.RetryConfig().SetShouldRetry(shouldRetry)
return r.parentBuilder
}
// WithMaxDelay sets the maximum delay for the request.
func (r RequestRetryBuilder) WithMaxDelay(duration time.Duration) *RequestBuilder {
r.requestConfig.RetryConfig().SetMaxDelay(duration)
return r.parentBuilder
}