Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 22:15 UTC
core: Use KeepAliveConfig to pass keepalive_interval to listener's accepted sockets (#7151)
Fix #7144
joshuamcbeth committed
Aug 2, 2025 at 11:43 UTC
e4447c4ba783482c35b096cfbe0e0ffa0403b450
2 files changed
+13
-12
listen.go
+7
-11
@@ -107,7 +107,8 @@ func listenReusable(ctx context.Context, lnKey string, network, address string,
107
if err != nil {
108
return nil, err
109
}
110
- return &fakeCloseListener{sharedListener: sharedLn.(*sharedListener), keepAlivePeriod: config.KeepAlive}, nil
110
+
111
+ return &fakeCloseListener{sharedListener: sharedLn.(*sharedListener), keepAliveConfig: config.KeepAliveConfig}, nil
112
}
113
114
// fakeCloseListener is a private wrapper over a listener that
@@ -121,12 +122,11 @@ func listenReusable(ctx context.Context, lnKey string, network, address string,
122
type fakeCloseListener struct {
123
closed int32 // accessed atomically; belongs to this struct only
124
*sharedListener // embedded, so we also become a net.Listener
124
- keepAlivePeriod time.Duration
125
+ keepAliveConfig net.KeepAliveConfig
126
}
127
127
-type canSetKeepAlive interface {
128
- SetKeepAlivePeriod(d time.Duration) error
129
- SetKeepAlive(bool) error
128
+type canSetKeepAliveConfig interface {
129
+ SetKeepAliveConfig(config net.KeepAliveConfig) error
130
}
131
132
func (fcl *fakeCloseListener) Accept() (net.Conn, error) {
@@ -140,12 +140,8 @@ func (fcl *fakeCloseListener) Accept() (net.Conn, error) {
140
if err == nil {
141
// if 0, do nothing, Go's default is already set
142
// and if the connection allows setting KeepAlive, set it
143
- if tconn, ok := conn.(canSetKeepAlive); ok && fcl.keepAlivePeriod != 0 {
144
- if fcl.keepAlivePeriod > 0 {
145
- err = tconn.SetKeepAlivePeriod(fcl.keepAlivePeriod)
146
- } else { // negative
147
- err = tconn.SetKeepAlive(false)
148
- }
143
+ if tconn, ok := conn.(canSetKeepAliveConfig); ok && fcl.keepAliveConfig.Enable {
144
+ err = tconn.SetKeepAliveConfig(fcl.keepAliveConfig)
145
if err != nil {
146
Log().With(zap.String("server", fcl.sharedListener.key)).Warn("unable to set keepalive for new connection:", zap.Error(err))
147
}
modules/caddyhttp/app.go
+6
-1
@@ -531,7 +531,12 @@ func (app *App) Start() error {
531
532
if h1ok || h2ok && useTLS || h2cok {
533
// create the listener for this socket
534
- lnAny, err := listenAddr.Listen(app.ctx, portOffset, net.ListenConfig{KeepAlive: time.Duration(srv.KeepAliveInterval)})
534
+ lnAny, err := listenAddr.Listen(app.ctx, portOffset, net.ListenConfig{
535
+ KeepAliveConfig: net.KeepAliveConfig{
536
+ Enable: srv.KeepAliveInterval != 0,
537
+ Interval: time.Duration(srv.KeepAliveInterval),
538
+ },
539
+ })
540
if err != nil {
541
return fmt.Errorf("listening on %s: %v", listenAddr.At(portOffset), err)
542
}