-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.go
50 lines (43 loc) · 1.18 KB
/
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
package qpool
import (
"math"
"time"
)
// RetryPolicy defines retry behavior
type RetryPolicy struct {
MaxAttempts int
Strategy RetryStrategy
BackoffFunc func(attempt int) time.Duration
Filter func(error) bool
}
// RetryStrategy defines the interface for retry behavior
type RetryStrategy interface {
NextDelay(attempt int) time.Duration
}
// ExponentialBackoff implements RetryStrategy
type ExponentialBackoff struct {
Initial time.Duration
}
func (eb *ExponentialBackoff) NextDelay(attempt int) time.Duration {
return eb.Initial * time.Duration(math.Pow(2, float64(attempt-1)))
}
// WithCircuitBreaker configures circuit breaker for a job
func WithCircuitBreaker(id string, maxFailures int, resetTimeout time.Duration) JobOption {
return func(j *Job) {
j.CircuitID = id
j.CircuitConfig = &CircuitBreakerConfig{
MaxFailures: maxFailures,
ResetTimeout: resetTimeout,
HalfOpenMax: 2, // Default value, could be made configurable
}
}
}
// WithRetry configures retry behavior for a job
func WithRetry(attempts int, strategy RetryStrategy) JobOption {
return func(j *Job) {
j.RetryPolicy = &RetryPolicy{
MaxAttempts: attempts,
Strategy: strategy,
}
}
}