Skip to content

Commit 5185257

Browse files
committed
main: Convert signal handling to use context.
This converts the signal handling and dcrdMain function to use a context with cancellation instead of a separate interrupt channel. It does not really make much of a difference yet, but the intent is to simplify the various subsystems in future commits by refactoring them to make use of the context versus manually handling start and stop in each subsystem.
1 parent 074d50f commit 5185257

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

dcrd.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func dcrdMain() error {
4747
}
4848
}()
4949

50-
// Get a channel that will be closed when a shutdown signal has been
50+
// Get a context that will be canceled when a shutdown signal has been
5151
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
5252
// another subsystem such as the RPC server.
53-
interrupt := interruptListener()
53+
ctx := shutdownListener()
5454
defer dcrdLog.Info("Shutdown complete")
5555

5656
// Show version and home dir at startup.
@@ -114,8 +114,8 @@ func dcrdMain() error {
114114
go drainOutgoingPipeMessages()
115115
}
116116

117-
// Return now if an interrupt signal was triggered.
118-
if interruptRequested(interrupt) {
117+
// Return now if a shutdown signal was triggered.
118+
if shutdownRequested(ctx) {
119119
return nil
120120
}
121121

@@ -133,8 +133,8 @@ func dcrdMain() error {
133133
db.Close()
134134
}()
135135

136-
// Return now if an interrupt signal was triggered.
137-
if interruptRequested(interrupt) {
136+
// Return now if a shutdown signal was triggered.
137+
if shutdownRequested(ctx) {
138138
return nil
139139
}
140140

@@ -143,31 +143,31 @@ func dcrdMain() error {
143143
// NOTE: The order is important here because dropping the tx index also
144144
// drops the address index since it relies on it.
145145
if cfg.DropAddrIndex {
146-
if err := indexers.DropAddrIndex(db, interrupt); err != nil {
146+
if err := indexers.DropAddrIndex(db, ctx.Done()); err != nil {
147147
dcrdLog.Errorf("%v", err)
148148
return err
149149
}
150150

151151
return nil
152152
}
153153
if cfg.DropTxIndex {
154-
if err := indexers.DropTxIndex(db, interrupt); err != nil {
154+
if err := indexers.DropTxIndex(db, ctx.Done()); err != nil {
155155
dcrdLog.Errorf("%v", err)
156156
return err
157157
}
158158

159159
return nil
160160
}
161161
if cfg.DropExistsAddrIndex {
162-
if err := indexers.DropExistsAddrIndex(db, interrupt); err != nil {
162+
if err := indexers.DropExistsAddrIndex(db, ctx.Done()); err != nil {
163163
dcrdLog.Errorf("%v", err)
164164
return err
165165
}
166166

167167
return nil
168168
}
169169
if cfg.DropCFIndex {
170-
if err := indexers.DropCfIndex(db, interrupt); err != nil {
170+
if err := indexers.DropCfIndex(db, ctx.Done()); err != nil {
171171
dcrdLog.Errorf("%v", err)
172172
return err
173173
}
@@ -178,7 +178,7 @@ func dcrdMain() error {
178178
// Create server and start it.
179179
lifetimeNotifier.notifyStartupEvent(lifetimeEventP2PServer)
180180
server, err := newServer(cfg.Listeners, db, activeNetParams.Params,
181-
cfg.DataDir, interrupt)
181+
cfg.DataDir, ctx.Done())
182182
if err != nil {
183183
// TODO(oga) this logging could do with some beautifying.
184184
dcrdLog.Errorf("Unable to start server on %v: %v",
@@ -195,7 +195,7 @@ func dcrdMain() error {
195195

196196
server.Start()
197197

198-
if interruptRequested(interrupt) {
198+
if shutdownRequested(ctx) {
199199
return nil
200200
}
201201

@@ -207,7 +207,7 @@ func dcrdMain() error {
207207
// Wait until the interrupt signal is received from an OS signal or
208208
// shutdown is requested through one of the subsystems such as the RPC
209209
// server.
210-
<-interrupt
210+
<-ctx.Done()
211211
return nil
212212
}
213213

signal.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package main
77

88
import (
9+
"context"
910
"os"
1011
"os/signal"
1112
)
@@ -18,26 +19,24 @@ var shutdownRequestChannel = make(chan struct{})
1819
// shutdown. This may be modified during init depending on the platform.
1920
var interruptSignals = []os.Signal{os.Interrupt}
2021

21-
// interruptListener listens for OS Signals such as SIGINT (Ctrl+C) and shutdown
22-
// requests from shutdownRequestChannel. It returns a channel that is closed
22+
// shutdownListener listens for OS Signals such as SIGINT (Ctrl+C) and shutdown
23+
// requests from shutdownRequestChannel. It returns a context that is canceled
2324
// when either signal is received.
24-
func interruptListener() <-chan struct{} {
25-
c := make(chan struct{})
25+
func shutdownListener() context.Context {
26+
ctx, cancel := context.WithCancel(context.Background())
2627
go func() {
2728
interruptChannel := make(chan os.Signal, 1)
2829
signal.Notify(interruptChannel, interruptSignals...)
2930

30-
// Listen for initial shutdown signal and close the returned
31-
// channel to notify the caller.
31+
// Listen for initial shutdown signal and cancel the returned context.
3232
select {
3333
case sig := <-interruptChannel:
34-
dcrdLog.Infof("Received signal (%s). Shutting down...",
35-
sig)
34+
dcrdLog.Infof("Received signal (%s). Shutting down...", sig)
3635

3736
case <-shutdownRequestChannel:
3837
dcrdLog.Infof("Shutdown requested. Shutting down...")
3938
}
40-
close(c)
39+
cancel()
4140

4241
// Listen for repeated signals and display a message so the user
4342
// knows the shutdown is in progress and the process is not
@@ -55,15 +54,15 @@ func interruptListener() <-chan struct{} {
5554
}
5655
}()
5756

58-
return c
57+
return ctx
5958
}
6059

61-
// interruptRequested returns true when the channel returned by
62-
// interruptListener was closed. This simplifies early shutdown slightly since
63-
// the caller can just use an if statement instead of a select.
64-
func interruptRequested(interrupted <-chan struct{}) bool {
60+
// shutdownRequested returns true when the context returned by shutdownListener
61+
// was canceled. This simplifies early shutdown slightly since the caller can
62+
// just use an if statement instead of a select.
63+
func shutdownRequested(ctx context.Context) bool {
6564
select {
66-
case <-interrupted:
65+
case <-ctx.Done():
6766
return true
6867
default:
6968
}

0 commit comments

Comments
 (0)