core: Use KeepAliveConfig to pass keepalive_interval to listener's accepted sockets (#7151)

Fix #7144
This commit is contained in:
joshuamcbeth 2025-08-02 11:43:34 -04:00 committed by GitHub
parent 5bc2afbbb6
commit e4447c4ba7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 12 deletions

View File

@ -107,7 +107,8 @@ func listenReusable(ctx context.Context, lnKey string, network, address string,
if err != nil {
return nil, err
}
return &fakeCloseListener{sharedListener: sharedLn.(*sharedListener), keepAlivePeriod: config.KeepAlive}, nil
return &fakeCloseListener{sharedListener: sharedLn.(*sharedListener), keepAliveConfig: config.KeepAliveConfig}, nil
}
// fakeCloseListener is a private wrapper over a listener that
@ -121,12 +122,11 @@ func listenReusable(ctx context.Context, lnKey string, network, address string,
type fakeCloseListener struct {
closed int32 // accessed atomically; belongs to this struct only
*sharedListener // embedded, so we also become a net.Listener
keepAlivePeriod time.Duration
keepAliveConfig net.KeepAliveConfig
}
type canSetKeepAlive interface {
SetKeepAlivePeriod(d time.Duration) error
SetKeepAlive(bool) error
type canSetKeepAliveConfig interface {
SetKeepAliveConfig(config net.KeepAliveConfig) error
}
func (fcl *fakeCloseListener) Accept() (net.Conn, error) {
@ -140,12 +140,8 @@ func (fcl *fakeCloseListener) Accept() (net.Conn, error) {
if err == nil {
// if 0, do nothing, Go's default is already set
// and if the connection allows setting KeepAlive, set it
if tconn, ok := conn.(canSetKeepAlive); ok && fcl.keepAlivePeriod != 0 {
if fcl.keepAlivePeriod > 0 {
err = tconn.SetKeepAlivePeriod(fcl.keepAlivePeriod)
} else { // negative
err = tconn.SetKeepAlive(false)
}
if tconn, ok := conn.(canSetKeepAliveConfig); ok && fcl.keepAliveConfig.Enable {
err = tconn.SetKeepAliveConfig(fcl.keepAliveConfig)
if err != nil {
Log().With(zap.String("server", fcl.sharedListener.key)).Warn("unable to set keepalive for new connection:", zap.Error(err))
}

View File

@ -531,7 +531,12 @@ func (app *App) Start() error {
if h1ok || h2ok && useTLS || h2cok {
// create the listener for this socket
lnAny, err := listenAddr.Listen(app.ctx, portOffset, net.ListenConfig{KeepAlive: time.Duration(srv.KeepAliveInterval)})
lnAny, err := listenAddr.Listen(app.ctx, portOffset, net.ListenConfig{
KeepAliveConfig: net.KeepAliveConfig{
Enable: srv.KeepAliveInterval != 0,
Interval: time.Duration(srv.KeepAliveInterval),
},
})
if err != nil {
return fmt.Errorf("listening on %s: %v", listenAddr.At(portOffset), err)
}