Set keepalive on TCP socket so idleTimeout works

This commit is contained in:
macros 2018-08-08 19:12:03 +02:00 committed by Traefiker Bot
parent 7ff6e6b66f
commit 60b4095c75

View file

@ -90,6 +90,22 @@ type serverEntryPoint struct {
onDemandListener func(string) (*tls.Certificate, error) onDemandListener func(string) (*tls.Certificate, error)
} }
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
// NewServer returns an initialized Server. // NewServer returns an initialized Server.
func NewServer(globalConfiguration configuration.GlobalConfiguration, provider provider.Provider) *Server { func NewServer(globalConfiguration configuration.GlobalConfiguration, provider provider.Provider) *Server {
server := new(Server) server := new(Server)
@ -803,6 +819,8 @@ func (s *Server) prepareServer(entryPointName string, entryPoint *configuration.
return nil, nil, err return nil, nil, err
} }
listener = tcpKeepAliveListener{listener.(*net.TCPListener)}
if entryPoint.ProxyProtocol != nil { if entryPoint.ProxyProtocol != nil {
IPs, err := whitelist.NewIP(entryPoint.ProxyProtocol.TrustedIPs, entryPoint.ProxyProtocol.Insecure, false) IPs, err := whitelist.NewIP(entryPoint.ProxyProtocol.TrustedIPs, entryPoint.ProxyProtocol.Insecure, false)
if err != nil { if err != nil {