-
Notifications
You must be signed in to change notification settings - Fork 11
/
exp_starter.go
88 lines (72 loc) · 1.93 KB
/
exp_starter.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package courier
import (
"context"
"time"
)
// StartOption can be used to customise behaviour of ExponentialStartStrategy
type StartOption func(*startOptions)
// WithMaxInterval sets the maximum interval the retry logic will wait
// before attempting another Client.Start, Default is 30 seconds
func WithMaxInterval(interval time.Duration) StartOption {
return func(o *startOptions) {
if interval > 0 {
o.maxInterval = interval
}
}
}
// WithOnRetry sets the func which is called when there is an error
// in the previous Client.Start attempt
func WithOnRetry(retryFunc func(error)) StartOption {
return func(o *startOptions) {
o.onRetry = retryFunc
}
}
// ExponentialStartStrategy will keep attempting to call Client.Start in the background and retry on error,
// it will never exit unless the context used to invoke is cancelled.
// This will NOT stop the client, that is the responsibility of caller.
func ExponentialStartStrategy(ctx context.Context, c interface{ Start() error }, opts ...StartOption) {
so := defaultStartOptions()
for _, opt := range opts {
opt(so)
}
exponentialStartStrategy(ctx, c, so)
}
type startOptions struct {
onRetry func(error)
maxInterval time.Duration
}
func defaultStartOptions() *startOptions {
return &startOptions{maxInterval: 30 * time.Second}
}
func exponentialStartStrategy(ctx context.Context, c interface{ Start() error }, so *startOptions) {
nextRetryInterval := 100 * time.Millisecond
errCh := make(chan error, 1)
startFn := func() {
errCh <- c.Start()
}
go startFn()
go func() {
for {
select {
case <-ctx.Done():
return
case err := <-errCh:
if err == nil {
return
}
if so.onRetry != nil {
so.onRetry(err)
}
go startFn()
time.Sleep(nextRetryInterval)
nextRetryInterval = min(nextRetryInterval*2, so.maxInterval)
}
}
}()
}
func min(x time.Duration, y time.Duration) time.Duration {
if x < y {
return x
}
return y
}