Graceful shutdown
In this next section of the book we’re going to talk about an important but often overlooked topic: how to safely stop your running application.
At the moment, when we stop our API application (usually by pressing Ctrl+C) it is terminated immediately with no opportunity for in-flight HTTP requests to complete. This isn’t ideal for two reasons:
- It means that clients won’t receive responses to their in-flight requests — all they will experience is a hard closure of the HTTP connection.
- Any work being carried out by our handlers may be left in an incomplete state.
We’re going to mitigate these problems by adding graceful shutdown functionality to our application, so that in-flight HTTP requests have the opportunity to complete processing before the application is terminated.
You’ll learn about:
Shutdown signals — what they are, how to send them, and how to listen for them in your API application.
How to use these signals to trigger a graceful shutdown of the HTTP server using Go’s
Shutdown()method.