-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
228 lines (191 loc) · 5.81 KB
/
group.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package coda
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
// GroupCrashError is an error that is returned when a group crashed.
// This means that a goroutine for this group returned with an error.
type GroupCrashError struct {
GroupName string
}
var _ error = &GroupCrashError{}
func (e *GroupCrashError) Error() string {
return fmt.Sprintf("group \"%s\" crashed", e.GroupName)
}
// GroupNotReadyError is an error that is returned when a goroutine did not become ready in time.
// Timeout depends on the timeout settings of the group.
type GroupNotReadyError struct {
GroupName string
}
var _ error = &GroupNotReadyError{}
func (e *GroupNotReadyError) Error() string {
return fmt.Sprintf("group \"%s\" did not become ready in time", e.GroupName)
}
// GroupStoppedEarlyError is an error that is returned when a goroutine returned before the context of the group was
// canceled.
type GroupStoppedEarlyError struct {
GroupName string
}
var _ error = &GroupStoppedEarlyError{}
func (e *GroupStoppedEarlyError) Error() string {
return fmt.Sprintf("group \"%s\" stopped early, no shutdown signal has been emitted yet but group function still returned", e.GroupName)
}
// Group is a group of goroutines that can be shut down using Shutdown.
// Do not create a Group directly, instead create one using Shutdown.NewGroup.
type Group struct {
options *groupOptions
name string
shutdown *Shutdown
isStoppingLock sync.Mutex
isStopping bool
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
func newGroup(shutdown *Shutdown, name string, opts ...GroupOption) *Group {
ctx, cancel := context.WithCancel(shutdown.Ctx())
return &Group{
options: buildGroupOptions(opts...),
name: name,
shutdown: shutdown,
ctx: ctx,
cancel: cancel,
}
}
// Name returns the name of the group.
func (g *Group) Name() string {
return g.name
}
type GroupFunc func(ctx context.Context, ready func()) error
// Go starts a new goroutine in this group.
// The goroutine is managed by the group and will be shut down when the group
// is shut down.
//
// Options can be provided to customize behavior:
// - WithReadyTimeout: Maximum time to wait for ready signal
// - WithCrashOnReadyTimeoutHit: Stop everything if ready timeout is hit
// - WithCrashOnError: Stop everything if goroutine returns error
// - WithCrashOnEarlyStop: Stop everything if goroutine exits before shutdown
// - WithBlock: Block until goroutine signals ready
//
// If the group is already stopping/stopped, the goroutine will not be started.
//
// Example:
//
// group.Go(func(ctx context.Context, ready func()) error {
// // Do initialization
// ready()
//
// // Wait for shutdown
// <-ctx.Done()
//
// // Do cleanup
// return nil
// }, coda.WithBlock(true))
func (g *Group) Go(fn GroupFunc, opts ...GroupGoroutineOption) {
g.isStoppingLock.Lock()
if g.isStopping {
// Not starting new goroutine since we are already stopped / in the process of stopping
g.isStoppingLock.Unlock()
return
}
options := buildGroupGoroutineOptions(opts...)
readyCh := make(chan struct{})
var once sync.Once
ready := func() {
once.Do(func() {
close(readyCh)
})
}
g.wg.Add(1)
go func() {
defer g.wg.Done()
if err := fn(g.ctx, ready); err != nil {
if options.crashOnError {
g.shutdown.options.logger.Error(fmt.Sprintf("Goroutine in group \"%s\" returned error: %s", g.name, err.Error()))
g.shutdown.StopWithError(errors.Join(&GroupCrashError{
GroupName: g.name,
}, err))
}
return
}
if g.ctx.Err() == nil && options.crashOnEarlyStop {
// fn returned, but its context is not canceled yet.
g.shutdown.options.logger.Error(fmt.Sprintf("Goroutine in group \"%s\" stopped early, crashing...", g.name))
g.shutdown.StopWithError(&GroupStoppedEarlyError{
GroupName: g.name,
})
return
}
g.shutdown.options.logger.Info(fmt.Sprintf("Goroutine in group \"%s\" stopped", g.name))
}()
// We can now safely unlock the isStoppingLock since the goroutine is now "added" to the wait group
g.isStoppingLock.Unlock()
// Create a channel to signal if we should stop blocking (ready function called or ready timeout hit).
blockCh := make(chan struct{})
// Handle ready timeout, do this in a goroutine so we can optionally block on this based on the options provided.
go func() {
// Wait for ready function to be called
if options.readyTimeout < 0 {
// No ready timeout set, just wait on the readyCh without timeout
<-readyCh
close(blockCh)
return
}
// Timeout is set, wait with timeout
timer := time.NewTimer(options.readyTimeout)
select {
case <-readyCh:
// Make sure we don't leak any resources
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
g.shutdown.options.logger.Error(fmt.Sprintf("Ready timeout hit for goroutine in group \"%s\"", g.name))
// Timed out waiting for readyCh to be closed
if options.crashOnReadyTimeoutHit {
g.shutdown.StopWithError(&GroupNotReadyError{
GroupName: g.name,
})
}
}
close(blockCh)
}()
if options.block {
<-blockCh
}
}
// stopAndWait will send the signal to all goroutines running in this group to shut down.
// It will wait until all routines have stopped (until a timeout is hit if a timeout is set).
func (g *Group) stopAndWait() (timedOut bool) {
g.isStoppingLock.Lock()
g.isStopping = true
g.isStoppingLock.Unlock()
g.cancel()
if g.options.shutdownTimeout < 0 {
// No need to handle timeouts, etc...
// Just wait for all routines to be done
g.wg.Wait()
return false
}
// Timeout is set, wait using a timeout
done := make(chan struct{})
go func() {
defer close(done)
g.wg.Wait()
}()
timer := time.NewTimer(g.options.shutdownTimeout)
select {
case <-done:
// Make sure we don't leak any resources
if !timer.Stop() {
<-timer.C
}
return false
case <-timer.C:
return true
}
}