-
Notifications
You must be signed in to change notification settings - Fork 12
/
executors.go
154 lines (139 loc) · 3.03 KB
/
executors.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gohalt
import (
"context"
"sync"
"time"
)
// DefaultRetriedDuration defines default initial duration for `square` throttler used by default.
// By default DefaultRetriedDuration is set to use `100 * time.Millisecond`.
var DefaultRetriedDuration = 100 * time.Millisecond
// Runnable defined by typical abstract async func signature.
// Runnable is used by `Runner` as a subject for execution.
type Runnable func(context.Context) error
func use(err error) Runnable {
return func(ctx context.Context) error {
return err
}
}
func nope(context.Context) error {
return nil
}
func loop(period time.Duration, run Runnable) Runnable {
return func(ctx context.Context) error {
tick := time.NewTicker(period)
defer tick.Stop()
for {
<-tick.C
if err := run(ctx); err != nil {
return err
}
}
}
}
func delayed(after time.Duration, run Runnable) Runnable {
return func(ctx context.Context) error {
if err := sleep(ctx, after); err != nil {
return err
}
return run(ctx)
}
}
func locked(run Runnable) Runnable {
var lock uint64
return func(ctx context.Context) error {
defer atomicBDecr(&lock)
if atomicBIncr(&lock) > 1 {
return nil
}
return run(ctx)
}
}
func cached(cache time.Duration, run Runnable) (cached Runnable, reset Runnable) {
var lock uint64
cached = func(ctx context.Context) error {
ts := atomicGet(&lock)
now := uint64(time.Now().UTC().UnixNano())
// on first call run no matters what
if ts == 0 {
if err := run(ctx); err != nil {
return err
}
atomicSet(&lock, now)
return nil
}
// then use cached timestamp
if cache > 0 && time.Duration(now-ts) > cache {
if err := run(ctx); err != nil {
return err
}
atomicSet(&lock, now)
return nil
}
return nil
}
reset = func(context.Context) error {
atomicSet(&lock, 0)
return nil
}
return
}
func retried(retries uint64, run Runnable) Runnable {
thr := NewThrottlerSquare(DefaultRetriedDuration, 0, false)
return func(ctx context.Context) (err error) {
// no need neither to check error
// nor to call release counterpart
for i := uint64(0); i < retries+1; i++ {
_ = thr.Acquire(ctx)
err = run(ctx)
if err == nil {
return
}
log("error happened in retry: %v", err)
}
return
}
}
func once(run Runnable) Runnable {
var once sync.Once
return func(ctx context.Context) (err error) {
once.Do(func() {
err = run(ctx)
})
return err
}
}
func async(run Runnable) Runnable {
return func(ctx context.Context) (err error) {
gorun(ctx, run)
return nil
}
}
func all(runs ...Runnable) Runnable {
return func(ctx context.Context) error {
var once sync.Once
var wg sync.WaitGroup
var result error
for _, run := range runs {
wg.Add(1)
go func(run Runnable) {
if err := run(ctx); err != nil {
once.Do(func() {
result = err
})
}
wg.Done()
}(run)
}
wg.Wait()
return result
}
}
func gorun(ctx context.Context, r Runnable) {
go func() {
_ = r(ctx)
}()
}
func sleep(_ context.Context, dur time.Duration) error {
time.Sleep(dur)
return nil
}