This project will easy to hook shutdown events to avoid losing application states and keep data correct.
- Use goroutine to running application (ex: web server, worker...).
- Import this project and use
WaitGracefullyShutdown
on themain.go
function of the last line.
See details in the file: sample/main.go
func main() {
waitTimeForShutdown := time.Second * 5
waitTimeForRequest := time.Second * 10
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(waitTimeForRequest)
_, _ = fmt.Fprint(w, "ok")
})
server := http.Server{
Addr: ":8080",
Handler: handler,
}
go func() { _ = server.ListenAndServe() }()
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
ez.WaitGracefullyShutdown(logger, waitTimeForShutdown, func(ctx context.Context) {
_ = server.Shutdown(ctx)
})
}