-
Notifications
You must be signed in to change notification settings - Fork 110
/
stoppable_workers.go
77 lines (65 loc) · 2.73 KB
/
stoppable_workers.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
package utils
import (
"context"
"sync"
goutils "go.viam.com/utils"
)
// TODO: When this struct is widely used and feature complete, move this to goutils instead of
// here. Until then, we cannot use this in any package imported by utils (e.g., the logging
// package) without introducing a circular import dependency.
// StoppableWorkers is a collection of goroutines that can be stopped at a later time.
type StoppableWorkers interface {
AddWorkers(...func(context.Context))
Stop()
Context() context.Context
}
// stoppableWorkersImpl is the implementation of StoppableWorkers. The linter will complain if you
// try to make a copy of something that contains a sync.WaitGroup (and returning a value at the end
// of NewStoppableWorkers() would make a copy of it), so we do everything through the
// StoppableWorkers interface to avoid making copies (since interfaces do everything by pointer).
type stoppableWorkersImpl struct {
mu sync.Mutex
cancelCtx context.Context
cancelFunc func()
activeBackgroundWorkers sync.WaitGroup
}
// NewStoppableWorkers runs the functions in separate goroutines. They can be stopped later.
func NewStoppableWorkers(funcs ...func(context.Context)) StoppableWorkers {
cancelCtx, cancelFunc := context.WithCancel(context.Background())
workers := &stoppableWorkersImpl{cancelCtx: cancelCtx, cancelFunc: cancelFunc}
workers.AddWorkers(funcs...)
return workers
}
// AddWorkers starts up additional goroutines for each function passed in. If you call this after
// calling Stop(), it will return immediately without starting any new goroutines.
func (sw *stoppableWorkersImpl) AddWorkers(funcs ...func(context.Context)) {
sw.mu.Lock()
defer sw.mu.Unlock()
if sw.cancelCtx.Err() != nil { // We've already stopped everything.
return
}
sw.activeBackgroundWorkers.Add(len(funcs))
for _, f := range funcs {
// In Go 1.21 and earlier, variables created in a loop were reused from one iteration to
// the next. Make a "fresh" copy of it here so that, if we're on to the next iteration of
// the loop before the goroutine starts up, it starts this function instead of the next
// one. For details, see https://go.dev/blog/loopvar-preview
f := f
goutils.PanicCapturingGo(func() {
defer sw.activeBackgroundWorkers.Done()
f(sw.cancelCtx)
})
}
}
// Stop shuts down all the goroutines we started up.
func (sw *stoppableWorkersImpl) Stop() {
sw.mu.Lock()
defer sw.mu.Unlock()
sw.cancelFunc()
sw.activeBackgroundWorkers.Wait()
}
// Context gets the context the workers are checking on. Using this function is expected to be
// rare: usually you shouldn't need to interact with the context directly.
func (sw *stoppableWorkersImpl) Context() context.Context {
return sw.cancelCtx
}