@marcel91200 / caddy Mirror · read-only / commits / 12778880
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 17:00 UTC

caddyhttp: Register post-shutdown callbacks (#5948)

Matt Holt committed Feb 14, 2024 at 21:21 UTC 127788807fdc32feb48a7f24a7e89f954ad3a049
2 files changed +18 -1
modules/caddyhttp/app.go
+9
@@ -642,6 +642,15 @@ func (app *App) Stop() error {
642 finishedShutdown.Wait()
643 }
644
645 + // run stop callbacks now that the server shutdowns are complete
646 + for name, s := range app.Servers {
647 + for _, stopHook := range s.onStopFuncs {
648 + if err := stopHook(ctx); err != nil {
649 + app.logger.Error("server stop hook", zap.String("server", name), zap.Error(err))
650 + }
651 + }
652 + }
653 +
654 return nil
655 }
656
modules/caddyhttp/server.go
+9 -1
@@ -253,6 +253,7 @@ type Server struct {
253 connStateFuncs []func(net.Conn, http.ConnState)
254 connContextFuncs []func(ctx context.Context, c net.Conn) context.Context
255 onShutdownFuncs []func()
256 + onStopFuncs []func(context.Context) error // TODO: Experimental (Nov. 2023)
257 }
258
259 // ServeHTTP is the entry point for all HTTP requests.
@@ -630,11 +631,18 @@ func (s *Server) RegisterConnContext(f func(ctx context.Context, c net.Conn) con
631 s.connContextFuncs = append(s.connContextFuncs, f)
632 }
633
633 -// RegisterOnShutdown registers f to be invoked on server shutdown.
634 +// RegisterOnShutdown registers f to be invoked when the server begins to shut down.
635 func (s *Server) RegisterOnShutdown(f func()) {
636 s.onShutdownFuncs = append(s.onShutdownFuncs, f)
637 }
638
639 +// RegisterOnStop registers f to be invoked after the server has shut down completely.
640 +//
641 +// EXPERIMENTAL: Subject to change or removal.
642 +func (s *Server) RegisterOnStop(f func(context.Context) error) {
643 + s.onStopFuncs = append(s.onStopFuncs, f)
644 +}
645 +
646 // HTTPErrorConfig determines how to handle errors
647 // from the HTTP handlers.
648 type HTTPErrorConfig struct {