From 4f5df39bdd9ce05146da14bb60f5a17a163d5262 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 30 Oct 2018 11:55:28 -0600 Subject: [PATCH] caddy: Re-invoke listener middlewares on reloads and upgrades --- caddy.go | 7 +++++++ caddyhttp/httpserver/server.go | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/caddy.go b/caddy.go index 3a2a0cce..b7af5f40 100644 --- a/caddy.go +++ b/caddy.go @@ -388,6 +388,11 @@ type GracefulServer interface { // address; you must store the address the // server is to serve on some other way. Address() string + + // WrapListener wraps a listener with the + // listener middlewares configured for this + // server, if any. + WrapListener(net.Listener) net.Listener } // Listener is a net.Listener with an underlying file descriptor. @@ -744,6 +749,7 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res return err } } + ln = gs.WrapListener(ln) } } @@ -782,6 +788,7 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res return err } } + ln = gs.WrapListener(ln) } } diff --git a/caddyhttp/httpserver/server.go b/caddyhttp/httpserver/server.go index 800f921d..513206dc 100644 --- a/caddyhttp/httpserver/server.go +++ b/caddyhttp/httpserver/server.go @@ -274,16 +274,26 @@ func (s *Server) Listen() (net.Listener, error) { ln = tcpKeepAliveListener{TCPListener: tcpLn} } + cln := s.WrapListener(ln) + + // Very important to return a concrete caddy.Listener + // implementation for graceful restarts. + return cln.(caddy.Listener), nil +} + +// WrapListener wraps ln in the listener middlewares configured +// for this server. +func (s *Server) WrapListener(ln net.Listener) net.Listener { + if ln == nil { + return nil + } cln := ln.(caddy.Listener) for _, site := range s.sites { for _, m := range site.listenerMiddleware { cln = m(cln) } } - - // Very important to return a concrete caddy.Listener - // implementation for graceful restarts. - return cln.(caddy.Listener), nil + return cln } // ListenPacket creates udp connection for QUIC if it is enabled,