This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
79 lines (68 loc) · 1.61 KB
/
option.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
package backoff
import (
"time"
)
type options struct {
timeout time.Duration
timeoutErrMessage string
maxWaitTime time.Duration
debugMode bool
debugPrint func(error)
abortLoop func(error) bool
timePrint func(time.Duration)
}
// Option enables customize the backoff
type Option func(*options)
// Timeout is to exit the backoff
// default: 65 seconds
func Timeout(t time.Duration) Option {
return func(o *options) {
o.timeout = t
}
}
// TimeoutErrMessage is to be output on timeerror
// default: "A timeout ends the exponential backoff"
func TimeoutErrMessage(msg string) Option {
return func(o *options) {
o.timeoutErrMessage = msg
}
}
// MaxWaitTime sets the maximum waiting time for the backoff interval
// default: 32 seconds
func MaxWaitTime(t time.Duration) Option {
return func(o *options) {
o.maxWaitTime = t
}
}
// DebugModeOn enables to print logs
// default: debug mode is off
func DebugModeOn() Option {
return func(o *options) {
o.debugMode = true
}
}
// DebugPrint customizes how to print error
func DebugPrint(f func(error)) Option {
return func(o *options) {
o.debugPrint = f
}
}
// AbortLoop aborts backoff loop when a specific error occurs
func AbortLoop(f func(error) bool) Option {
return func(o *options) {
o.abortLoop = f
}
}
// TimePrint prints sleepting time
// default: "waiting ?s..."
func TimePrint(f func(time.Duration)) Option {
return func(o *options) {
o.timePrint = f
}
}
// DisabelTimePrint disables printing sleep time
func DisabelTimePrint() Option {
return func(o *options) {
o.timePrint = func(t time.Duration) {}
}
}