-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepeat_opts.go
64 lines (50 loc) · 1.08 KB
/
repeat_opts.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
package gowait
import "time"
type RepeatOptGen struct{}
type repeatOpt func(*repeatConfig)
type repeatConfig struct {
zeroDuration time.Duration
minDuration time.Duration
waitConfig
}
func (r *repeatConfig) init() *repeatConfig {
*r = repeatConfig{
zeroDuration: time.Second,
minDuration: time.Second,
}
r.waitConfig.init()
return r
}
func (r *repeatConfig) applyOpts(opts ...repeatOpt) *repeatConfig {
r.init()
for _, v := range opts {
v(r)
}
return r
}
func (r repeatConfig) applyDuration(t time.Duration) (n time.Duration) {
n = t
if r.zeroDuration > 0 && t <= 0 {
n = r.zeroDuration
}
if n < r.minDuration {
n = r.minDuration
}
return
}
func (o RepeatOptGen) ZeroDuration(zero time.Duration) repeatOpt {
return func(r *repeatConfig) {
r.zeroDuration = zero
}
}
func (o RepeatOptGen) MinDuration(min time.Duration) repeatOpt {
return func(r *repeatConfig) {
r.minDuration = min
}
}
func (o RepeatOptGen) PanicRetry(retry bool, duration time.Duration) repeatOpt {
return func(r *repeatConfig) {
r.panicRetry = retry
r.panicRetryDuration = duration
}
}