-
Notifications
You must be signed in to change notification settings - Fork 84
/
signal.go
83 lines (73 loc) · 2.43 KB
/
signal.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
// Copyright (c) 2018-2020, The Decred developers
// Copyright (c) 2013-2014, The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"os"
"os/signal"
)
// shutdownRequested checks if the Done channel of the given context has been
// closed. This could indicate cancellation, expiration, or deadline expiry. But
// when called for the context provided by withShutdownCancel, it indicates if
// shutdown has been requested (i.e. via os.Interrupt or requestShutdown).
func shutdownRequested(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
var (
// shutdownRequest is used to initiate shutdown from one of the subsystems
// using the same code paths as when an interrupt signal is received.
shutdownRequest = make(chan struct{})
// shutdownSignal is closed whenever shutdown is invoked through an
// interrupt signal or via requestShutdown. Any contexts created using
// withShutdownChannel are canceled when this is closed.
shutdownSignal = make(chan struct{})
)
// withShutdownCancel creates a copy of a context that is canceled whenever
// shutdown is invoked through an interrupt signal or from an JSON-RPC stop
// request.
func withShutdownCancel(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
go func() {
<-shutdownSignal
cancel()
}()
return ctx
}
// requestShutdown signals for starting the clean shutdown of the process
// through an internal component.
func requestShutdown() {
shutdownRequest <- struct{}{}
}
// shutdownListener listens for shutdown requests and cancels all contexts
// created from withShutdownCancel. This function never returns and is intended
// to be spawned in a new goroutine.
func shutdownListener() {
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt)
// Listen for the initial shutdown signal.
select {
case sig := <-interruptChannel:
fmt.Printf("Received signal (%s). Shutting down...\n", sig)
case <-shutdownRequest:
fmt.Println("Shutdown requested. Shutting down...")
}
// Cancel all contexts created from withShutdownCancel.
close(shutdownSignal)
// Listen for any more shutdown signals and log that shutdown has already
// been signaled.
for {
select {
case <-interruptChannel:
case <-shutdownRequest:
}
log.Info("Shutdown signaled. Already shutting down...")
}
}