gitmyhub

go-gracefully

Go ★ 66 updated 11y ago

Graceful shutdown utility for Golang

A small Go utility for graceful server shutdown that finishes in-flight requests before exiting, with a second-signal force-quit override and configurable timeout.

Gosetup: easycomplexity 2/5

Go-gracefully is a small utility for Go programs that handles the moment when you need to shut a server down. When you stop a running program, you often want it to finish handling the requests it's already working on before it exits. This tool gives you a way to do that cleanly, with a built-in safety valve: if you send the shutdown signal a second time, it immediately forces the program to stop.

In everyday terms, imagine a restaurant that's closing for the night. A graceful shutdown means the kitchen finishes cooking the meals that are already ordered, but stops taking new orders. If the manager flips the lights off and locks the door (the second signal), everything stops right away — no matter what's still on the stove. This tool gives a Go program that same behavior, with a configurable timeout so the "graceful" period doesn't drag on forever.

This is useful for anyone building a backend service or API in Go. If you're deploying a new version of your app, you need to stop the old version first. Without graceful shutdown, in-flight requests get cut off mid-processing — which could mean a half-finished database write or a user seeing an error page. With it, those requests complete normally, and the program exits once it's idle (or once the timeout hits). The second-signal hard exit matters for situations where a process is stuck and you need it gone immediately.

The project itself is quite small and focused. The README doesn't go into much detail beyond a brief example, but the core idea is simple: set a timeout, call shutdown, then stop your work. It's a utility that does one specific job and doesn't try to do more.

Where it fits