title | keywords | description | ||||
---|---|---|---|---|---|---|
Graceful shutdown |
|
Graceful shutdown of applications. |
fiberRecipes/graceful-shutdown on graceful-shutdown (f0834df) [?] via πΉ v1.15.2 took 4s
β― go run graceful-shutdown
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Fiber v2.1.0 β
β http://127.0.0.1:3000 β
β β
β Handlers ............. 2 Threads ............. 8 β
β Prefork ....... Disabled PID .............. 2540 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
^CGracefully shutting down...
Running cleanup tasks...
This shows how to implement a graceful shutdown with Fiber and the os/signal
package.
This example relies on the use of channels, a data type in Go that allows you to send and receive data to/from specific places in an application (read more about them here).
A channel is created, and registered with signal.Notify
so that when the program receives an interrupt (for example, when CTRL+C
is pressed), a notification is sent to the channel. Once this is received, app.Shutdown
is called to close all active connections and return from app.Listen
. After this point, cleanup functions can be run and the program eventually quits.