Skip to content

Commit 1277888

Browse files
authored
caddyhttp: Register post-shutdown callbacks (#5948)
1 parent 2c48dda commit 1277888

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

modules/caddyhttp/app.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,15 @@ func (app *App) Stop() error {
642642
finishedShutdown.Wait()
643643
}
644644

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+
645654
return nil
646655
}
647656

modules/caddyhttp/server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ type Server struct {
253253
connStateFuncs []func(net.Conn, http.ConnState)
254254
connContextFuncs []func(ctx context.Context, c net.Conn) context.Context
255255
onShutdownFuncs []func()
256+
onStopFuncs []func(context.Context) error // TODO: Experimental (Nov. 2023)
256257
}
257258

258259
// 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
630631
s.connContextFuncs = append(s.connContextFuncs, f)
631632
}
632633

633-
// RegisterOnShutdown registers f to be invoked on server shutdown.
634+
// RegisterOnShutdown registers f to be invoked when the server begins to shut down.
634635
func (s *Server) RegisterOnShutdown(f func()) {
635636
s.onShutdownFuncs = append(s.onShutdownFuncs, f)
636637
}
637638

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+
638646
// HTTPErrorConfig determines how to handle errors
639647
// from the HTTP handlers.
640648
type HTTPErrorConfig struct {

0 commit comments

Comments
 (0)