Graceful shutdown or restart

There are a few approaches you can use to perform a graceful shutdown or restart. You can make use of third-party packages specifically built for that, or you can use the app.Shutdown(context.Context) method. Examples can be found here.

Register an event on CTRL/CMD+C using iris.RegisterOnInterrupt:

  1. idleConnsClosed := make(chan struct{})
  2. iris.RegisterOnInterrupt(func() {
  3. timeout := 10 * time.Second
  4. ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
  5. defer cancel()
  6. // close all hosts.
  7. app.Shutdown(ctx)
  8. close(idleConnsClosed)
  9. })
  10. // [...]
  11. app.Listen(":8080", iris.WithoutInterruptHandler, iris.WithoutServerError(iris.ErrServerClosed))
  12. <-idleConnsClosed