Use atomic for shutdownAt

This commit is contained in:
Francis Lavoie 2026-04-13 00:55:27 -04:00
parent cef419186f
commit 344bbbcd06
No known key found for this signature in database
3 changed files with 7 additions and 15 deletions

View File

@ -214,8 +214,6 @@ func (app *App) Provision(ctx caddy.Context) error {
srv.ctx = ctx
srv.logger = app.logger.Named("log")
srv.errorLogger = app.logger.Named("log.error")
srv.shutdownAtMu = new(sync.RWMutex)
if srv.Metrics != nil {
srv.logger.Warn("per-server 'metrics' is deprecated; use 'metrics' in the root 'http' app instead")
app.Metrics = cmp.Or(app.Metrics, &Metrics{
@ -689,9 +687,7 @@ func (app *App) Stop() error {
for _, addr := range na.Expand() {
if caddy.ListenerUsage(addr.Network, addr.JoinHostPort(0)) < 2 {
app.logger.Debug("listener closing and shutdown delay is configured", zap.String("address", addr.String()))
server.shutdownAtMu.Lock()
server.shutdownAt = scheduledTime
server.shutdownAtMu.Unlock()
server.shutdownAt.Store(&scheduledTime)
delay = true
} else {
app.logger.Debug("shutdown delay configured but listener will remain open", zap.String("address", addr.String()))

View File

@ -387,17 +387,14 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
switch key {
case "http.shutting_down":
server := req.Context().Value(ServerCtxKey).(*Server)
server.shutdownAtMu.RLock()
defer server.shutdownAtMu.RUnlock()
return !server.shutdownAt.IsZero(), true
return server.shutdownAt.Load() != nil, true
case "http.time_until_shutdown":
server := req.Context().Value(ServerCtxKey).(*Server)
server.shutdownAtMu.RLock()
defer server.shutdownAtMu.RUnlock()
if server.shutdownAt.IsZero() {
t := server.shutdownAt.Load()
if t == nil {
return nil, true
}
return time.Until(server.shutdownAt), true
return time.Until(*t), true
}
return nil, false

View File

@ -28,7 +28,7 @@ import (
"runtime"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/caddyserver/certmagic"
@ -291,8 +291,7 @@ type Server struct {
trustedProxies IPRangeSource
shutdownAt time.Time
shutdownAtMu *sync.RWMutex
shutdownAt atomic.Pointer[time.Time]
// registered callback functions
connStateFuncs []func(net.Conn, http.ConnState)