Skip to content

Latest commit

Β 

History

History

graceful-shutdown

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Graceful shutdown in Fiber

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.

Explanation

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.