-
Notifications
You must be signed in to change notification settings - Fork 12
/
stop.go
50 lines (41 loc) · 1.73 KB
/
stop.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
package services
import "context"
// A StopChan signals when some work should stop.
// Use StopChanR if you already have a read only <-chan.
type StopChan chan struct{}
// NewCtx returns a background [context.Context] that is cancelled when StopChan is closed.
func (s StopChan) NewCtx() (context.Context, context.CancelFunc) {
return StopRChan((<-chan struct{})(s)).NewCtx()
}
// Ctx cancels a [context.Context] when StopChan is closed.
func (s StopChan) Ctx(ctx context.Context) (context.Context, context.CancelFunc) {
return StopRChan((<-chan struct{})(s)).Ctx(ctx)
}
// CtxCancel cancels a [context.Context] when StopChan is closed.
// Returns ctx and cancel unmodified, for convenience.
func (s StopChan) CtxCancel(ctx context.Context, cancel context.CancelFunc) (context.Context, context.CancelFunc) {
return StopRChan((<-chan struct{})(s)).CtxCancel(ctx, cancel)
}
// A StopRChan signals when some work should stop.
// This is a receive-only version of StopChan, for casting an existing <-chan.
type StopRChan <-chan struct{}
// NewCtx returns a background [context.Context] that is cancelled when StopChan is closed.
func (s StopRChan) NewCtx() (context.Context, context.CancelFunc) {
return s.Ctx(context.Background())
}
// Ctx cancels a [context.Context] when StopChan is closed.
func (s StopRChan) Ctx(ctx context.Context) (context.Context, context.CancelFunc) {
return s.CtxCancel(context.WithCancel(ctx))
}
// CtxCancel cancels a [context.Context] when StopChan is closed.
// Returns ctx and cancel unmodified, for convenience.
func (s StopRChan) CtxCancel(ctx context.Context, cancel context.CancelFunc) (context.Context, context.CancelFunc) {
go func() {
select {
case <-s:
cancel()
case <-ctx.Done():
}
}()
return ctx, cancel
}