The Iris framework supports graceful shutdown. In actual business scenarios, when we want to restart or close the web application, directly forcing the Go process to shut down can easily lead to some business processes being terminated halfway through without completion, causing business exceptions. The better approach is to stop accepting new requests, let the ongoing requests finish processing, and then exit the Go process.
You can use several methods to perform graceful shutdown or restart. You can also use third-party packages specifically built for this purpose, or you can use the app.Shutdown(context.Context) method.
Register the CTRL/CMD+C event using iris.RegisterOnInterrupt:
idleConnsClosed := make(chan struct{})
iris.RegisterOnInterrupt(func() {
timeout := 10 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
defer cancel()
// close all hosts.
app.Shutdown(ctx)
close(idleConnsClosed)
})
// [...]
app.Listen(":8080", iris.WithoutInterruptHandler, iris.WithoutServerError(iris.ErrServerClosed))
<-idleConnsClosed