Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Go 1.16: Cleanup shutdownListener related code #171

Open
atc0005 opened this issue Sep 17, 2020 · 0 comments
Open

Go 1.16: Cleanup shutdownListener related code #171

atc0005 opened this issue Sep 17, 2020 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@atc0005
Copy link
Owner

atc0005 commented Sep 17, 2020

Background

Just learned of this new functionality landing in Go 1.16:

os/signal: add NotifyContext to cancel context using system signals

This is going to clean up much startup boilerplate.

ctx, stop := signal.NotifyContext(ctx, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
defer stop()

Changes

It's been a little bit since I touched the code, but this looks to be the relevant portions to get an update once Go 1.16 is available:

brick/cmd/brick/main.go

Lines 114 to 120 in 88889b6

// Setup "listener" to cancel the parent context when Signal.Notify()
// indicates that SIGINT has been received
go shutdownListener(ctx, quit, cancel)
// Setup "listener" to shutdown the running http server when
// the parent context has been cancelled
go gracefulShutdown(ctx, httpServer, config.HTTPServerShutdownTimeout, httpDone)

also:

// shutdownListener listens for an os.Signal on the provided quit channel.
// When this signal is received, the provided parent context cancel() function
// is used to cancel all child contexts. This is intended to be run as a
// goroutine.
func shutdownListener(ctx context.Context, quit <-chan os.Signal, parentContextCancel context.CancelFunc) {
// FIXME: If we're passing in the parent context's CancelFunc, do we need
// the `ctx` that we're passing in?
// monitor for shutdown signal
osSignal := <-quit
log.Debugf("shutdownListener: Received shutdown signal: %v", osSignal)
// Attempt to trigger a cancellation of the parent context
log.Debug("shutdownListener: Cancelling context ...")
parentContextCancel()
log.Debug("shutdownListener: context canceled")
}

See also func gracefulShutdown in that same file.

References

@atc0005 atc0005 added the enhancement New feature or request label Sep 17, 2020
@atc0005 atc0005 added this to the Future milestone Sep 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant