/
middleware.go
76 lines (67 loc) · 1.53 KB
/
middleware.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
package workhorse
import (
"context"
"time"
)
// Every applies a task periodically every interval until the first
// failure. Example:
//
// w.Go("task", workhorse.Every(func(ctx context.Context) error {
// fmt.Println("still alive!")
// return nil
// }, time.Minute))
func Every(task Task, interval time.Duration) Task {
return func(ctx context.Context) error {
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-t.C:
}
if err := task(ctx); err != nil {
return err
}
}
}
}
// Retry retries a task on failures with a linear backoff.
// Set the numRetries to -1 to retry forever. Example:
//
// w.Go("task", workhorse.Retry(func(ctx context.Context) error {
// return fmt.Errorf("instant failure!")
// }, 4, time.Second))
func Retry(task Task, numRetries int, backoff time.Duration) Task {
return func(ctx context.Context) error {
t := time.NewTimer(backoff)
defer t.Stop()
remaining := numRetries
for {
err := task(ctx)
if err == nil {
return nil
} else if remaining == 0 {
return err
} else if remaining > 0 {
remaining--
}
t.Reset(backoff)
select {
case <-ctx.Done():
return nil
case <-t.C:
}
}
}
}
// Instrument allows to instrument tasks.
func Instrument(task Task, tfn func(name string, runTime time.Duration, err error)) Task {
return func(ctx context.Context) error {
start := time.Now()
err := task(ctx)
elapsed := time.Since(start)
tfn(TaskName(ctx), elapsed, err)
return err
}
}