Skip to content

Commit 4dd76b0

Browse files
authored
Add Backoff interface (#12)
* feat: Add Backoff interface to support customizable backoff policies (#8) * refactor: Update worker and manager to use Backoff interface (#8)
1 parent e28424b commit 4dd76b0

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

backoff.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ import (
55
"time"
66
)
77

8+
var DefaultBackoffPolicy = BackoffPolicy{
9+
BaseDelay: 1 * time.Second,
10+
MaxDelay: 30 * time.Second,
11+
UseJitter: true,
12+
JitterRangeMs: 300,
13+
}
14+
15+
// Backoff defines the interface for calculating backoff delays between retries.
16+
// Implementations of this interface can provide custom logic for determining
17+
// how long to wait before retrying a failed task based on the number of retries.
18+
type Backoff interface {
19+
// Calculate returns the duration to wait before the next retry attempt.
20+
// The input parameter retries indicates how many times the task has already been retried.
21+
Calculate(retries int) time.Duration
22+
}
23+
824
// BackoffPolicy defines the configuration for handling retries with backoff logic.
925
// It provides settings for base delay, maximum delay, jitter, and the range for jitter.
1026
type BackoffPolicy struct {

manager.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ManagerOption func(*ManagerConfig)
1717

1818
// ManagerConfig holds configuration options for the Manager.
1919
type ManagerConfig struct {
20-
BackoffPolicy *BackoffPolicy
20+
BackoffPolicy Backoff
2121
}
2222

2323
// Manager is responsible for managing the lifecycle of workers,
@@ -37,7 +37,7 @@ type Manager struct {
3737
// Example usage:
3838
//
3939
// manager := NewManager(bp, numOfWorkers, WithBackoffPolicy(customBackoffPolicy))
40-
func WithBackoffPolicy(bp *BackoffPolicy) ManagerOption {
40+
func WithBackoffPolicy(bp Backoff) ManagerOption {
4141
return func(cfg *ManagerConfig) {
4242
cfg.BackoffPolicy = bp
4343
}
@@ -79,14 +79,8 @@ func NewManager(broker Broker, wf WorkerFactory, numWorkers int, opts ...Manager
7979
opt(managerConfig)
8080
}
8181

82-
// Set default backoff if cfg.BackoffPolicy == nil
8382
if managerConfig.BackoffPolicy == nil {
84-
managerConfig.BackoffPolicy = &BackoffPolicy{
85-
BaseDelay: 1 * time.Second,
86-
MaxDelay: 30 * time.Second,
87-
UseJitter: true,
88-
JitterRangeMs: 300,
89-
}
83+
managerConfig.BackoffPolicy = &DefaultBackoffPolicy
9084
}
9185

9286
manager := &Manager{broker: broker, ctx: ctx, cancel: cancel}

worker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type WorkerFactory func(cfg WorkerConfig) Worker
6565
type WorkerConfig struct {
6666
ID int
6767
Broker Broker
68-
Backoff *BackoffPolicy
68+
Backoff Backoff
6969
WG *sync.WaitGroup
7070
}
7171

@@ -86,7 +86,7 @@ type WorkerConfig struct {
8686
type DefaultWorker struct {
8787
id int
8888
broker Broker
89-
backoff *BackoffPolicy
89+
backoff Backoff
9090
handlers map[string]TaskHandlerFunc
9191
wg *sync.WaitGroup
9292
}

0 commit comments

Comments
 (0)