-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_goroutine_options.go
85 lines (75 loc) · 2.69 KB
/
group_goroutine_options.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
package coda
import "time"
type groupGoroutineOptions struct {
readyTimeout time.Duration
crashOnReadyTimeoutHit bool
crashOnError bool
crashOnEarlyStop bool
block bool
}
func defaultGroupGoroutineOptions() *groupGoroutineOptions {
return &groupGoroutineOptions{
readyTimeout: -1, // No timeout
crashOnReadyTimeoutHit: true,
crashOnError: true,
crashOnEarlyStop: true,
block: false,
}
}
type GroupGoroutineOption func(*groupGoroutineOptions)
func buildGroupGoroutineOptions(opts ...GroupGoroutineOption) *groupGoroutineOptions {
options := defaultGroupGoroutineOptions()
for _, fn := range opts {
fn(options)
}
return options
}
// WithReadyTimeout sets the timeout Group.Go will max block for while waiting for the ready() signal.
// Any value less than 0 will disable the timeout and will cause Go to wait without a limit.
// Default is no timeout.
// It can be passed to Group.Go.
func WithReadyTimeout(d time.Duration) GroupGoroutineOption {
return func(options *groupGoroutineOptions) {
options.readyTimeout = d
}
}
// WithCrashOnReadyTimeoutHit sets if Shutdown should fully stop / abort startup if the ready timeout is hit.
// Set to "false" to ignore timeout errors and continue normal execution, set to "true" to auto shutdown.
// Default is "true".
// It can be passed to Group.Go.
func WithCrashOnReadyTimeoutHit(crash bool) GroupGoroutineOption {
return func(options *groupGoroutineOptions) {
options.crashOnReadyTimeoutHit = crash
}
}
// WithCrashOnError sets if Shutdown should fully stop with an error if the GroupFunc returns
// an error.
// Default is "true".
// It can be passed to Group.Go.
func WithCrashOnError(crash bool) GroupGoroutineOption {
return func(options *groupGoroutineOptions) {
options.crashOnError = crash
}
}
// WithCrashOnEarlyStop sets if Shutdown should fully stop with an error if the GroupFunc returns
// early. An early return is when the function returns without an error and the context of the group is not canceled.
// Default is "true".
// It can be passed to Group.Go.
func WithCrashOnEarlyStop(crash bool) GroupGoroutineOption {
return func(options *groupGoroutineOptions) {
options.crashOnEarlyStop = crash
}
}
// WithBlock sets whether Group.Go should block until the goroutine signals it's ready.
// When true, Go will not return until either:
// - The ready() function is called
// - The ready timeout is hit (if configured)
// - The goroutine returns an error
//
// Default is "false" (non-blocking).
// It can be passed to Group.Go.
func WithBlock(block bool) GroupGoroutineOption {
return func(options *groupGoroutineOptions) {
options.block = block
}
}