Grace - Graceful HTTP Server Shutdown
Installation
go get github.com/davidsugianto/go-pkgs/grace
Usage
Replace your existing server start call:
// BEFORE
http.ListenAndServe(":8080", handler)
// AFTER
grace.ServeHTTP(":8080", handler)
That's it! Works with any framework.
Examples
Standard Library
import "github.com/davidsugianto/go-pkgs/grace"
http.HandleFunc("/", handler)
grace.ServeHTTP(":8080", nil)
Gin
r := gin.Default()
grace.ServeHTTP(":8080", r)
Echo
e := echo.New()
grace.ServeHTTP(":8080", e)
Any Framework
grace.ServeHTTP(":8080", yourHandler)
HTTPS
grace.ServeHTTPS(":8443", "cert.pem", "key.pem", handler)
Custom Server
server := &http.Server{...}
grace.ServeServer(server)
What It Does
- Starts your HTTP server normally
- Listens for SIGINT/SIGTERM signals
- Stops accepting new connections
- Waits up to 30 seconds for active requests to complete
- Gracefully shuts down