From the course: Go for Developers: Practical Techniques for Effective Coding
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
System signals - Go Tutorial
From the course: Go for Developers: Practical Techniques for Effective Coding
System signals
- All programs should attempt a graceful shutdown. This means that instead of crashing or exiting an application before resources are released, the application should instead wait for the resources to be released. There are a few rules for graceful shutdown that we must follow. First, we needed to detect that the program was requested to shut down. Then we have to shut down all internal processes, including long running go routines. We have to have a reasonable timeout in the event that internal processes are taking too long to shut down or are deadlocked. We need to respond to an actual user request for immediate hard shutdown. And finally, we need to record the result of the shutdown, success, timeout, user intervention, et cetera. Using channels in the OS Signals package, you can capture system signals and respond accordingly. The signal.Notify function allows you to register a channel to receive notifications of an os.Signal type. Here we're using the make function to create a…