-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepeater.go
69 lines (60 loc) · 1.92 KB
/
repeater.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
// Package repeater call fun till it returns no error, up to repeat some number of iterations and delays defined by strategy.
// Repeats number and delays defined by strategy.Interface. Terminates immediately on err from
// provided, optional list of critical errors
package repeater
import (
"context"
"errors"
"time"
"github.com/go-pkgz/repeater/strategy"
)
// Repeater is the main object, should be made by New or NewDefault, embeds strategy
type Repeater struct {
Strategy
}
// Strategy interface for repeater strategy
type Strategy interface {
Start(ctx context.Context) <-chan struct{} // returns channel with repeater ticks
}
// New repeater with a given strategy. If strategy=nil initializes with FixedDelay 5sec, 10 times.
func New(strtg strategy.Interface) *Repeater {
if strtg == nil {
strtg = &strategy.FixedDelay{Repeats: 10, Delay: time.Second * 5}
}
result := Repeater{Strategy: strtg}
return &result
}
// NewDefault makes repeater with FixedDelay strategy
func NewDefault(repeats int, delay time.Duration) *Repeater {
return New(&strategy.FixedDelay{Repeats: repeats, Delay: delay})
}
// Do repeats fun till no error. Predefined (optional) errors terminate immediately
func (r Repeater) Do(ctx context.Context, fun func() error, errs ...error) (err error) {
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc() // ensure strategy's channel termination
inErrors := func(err error) bool {
for _, e := range errs {
if errors.Is(err, e) {
return true
}
}
return false
}
ch := r.Start(ctx) // channel of ticks-like events provided by strategy
for {
select {
case <-ctx.Done():
return ctx.Err()
case _, ok := <-ch:
if !ok { // closed channel indicates completion or early termination, set by strategy
return err
}
if err = fun(); err == nil {
return nil
}
if err != nil && inErrors(err) { // terminate on critical error from provided list
return err
}
}
}
}