-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite.go
More file actions
48 lines (44 loc) · 1.38 KB
/
composite.go
File metadata and controls
48 lines (44 loc) · 1.38 KB
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
package signals
// AnyShutdown returns a channel closed on SIGINT, SIGTERM, or user 'q'.
// The returned channel will close when any of these signals is received.
// Note: This function spawns goroutines for signal monitoring. Once the returned
// channel closes, those goroutines will be cleaned up automatically.
func AnyShutdown() <-chan struct{} {
ch := make(chan struct{})
interruptCh := Interrupt()
terminateCh := Terminate()
quitCh := Quit()
go func() {
select {
case <-interruptCh:
close(ch)
case <-terminateCh:
close(ch)
case <-quitCh:
close(ch)
}
// Note: The other signal goroutines will clean up when their channels
// are garbage collected since no one is reading from them anymore.
}()
return ch
}
// GracefulShutdown returns a channel closed on SIGTERM or SIGHUP.
// The returned channel will close when any of these signals is received.
// Note: This function spawns goroutines for signal monitoring. Once the returned
// channel closes, those goroutines will be cleaned up automatically.
func GracefulShutdown() <-chan struct{} {
ch := make(chan struct{})
terminateCh := Terminate()
hangupCh := Hangup()
go func() {
select {
case <-terminateCh:
close(ch)
case <-hangupCh:
close(ch)
}
// Note: The other signal goroutines will clean up when their channels
// are garbage collected since no one is reading from them anymore.
}()
return ch
}