Skip to content

Commit

Permalink
Set keepalive on TCP socket so idleTimeout works
Browse files Browse the repository at this point in the history
  • Loading branch information
ajardan authored and traefiker committed Aug 8, 2018
1 parent 7ff6e6b commit 60b4095
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ type serverEntryPoint struct {
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.
func NewServer(globalConfiguration configuration.GlobalConfiguration, provider provider.Provider) *Server {
server := new(Server)
Expand Down Expand Up @@ -803,6 +819,8 @@ func (s *Server) prepareServer(entryPointName string, entryPoint *configuration.
return nil, nil, err
}

listener = tcpKeepAliveListener{listener.(*net.TCPListener)}

if entryPoint.ProxyProtocol != nil {
IPs, err := whitelist.NewIP(entryPoint.ProxyProtocol.TrustedIPs, entryPoint.ProxyProtocol.Insecure, false)
if err != nil {
Expand Down

0 comments on commit 60b4095

Please sign in to comment.