Skip to content

Commit

Permalink
Always set Keepalive options
Browse files Browse the repository at this point in the history
Different versions of Go have different defaults, so we just always set
what the user configured.
  • Loading branch information
erikdubbelboer committed Dec 26, 2020
1 parent 4e63057 commit bd35133
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ func (s *Server) getNextProto(c net.Conn) (proto string, err error) {
// eventually go away.
type tcpKeepaliveListener struct {
*net.TCPListener
keepalive bool
keepalivePeriod time.Duration
}

Expand All @@ -1453,7 +1454,7 @@ func (ln tcpKeepaliveListener) Accept() (net.Conn, error) {
if err != nil {
return nil, err
}
if err := tc.SetKeepAlive(true); err != nil {
if err := tc.SetKeepAlive(ln.keepalive); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
Expand All @@ -1477,13 +1478,12 @@ func (s *Server) ListenAndServe(addr string) error {
if err != nil {
return err
}
if s.TCPKeepalive {
if tcpln, ok := ln.(*net.TCPListener); ok {
return s.Serve(tcpKeepaliveListener{
TCPListener: tcpln,
keepalivePeriod: s.TCPKeepalivePeriod,
})
}
if tcpln, ok := ln.(*net.TCPListener); ok {
return s.Serve(tcpKeepaliveListener{
TCPListener: tcpln,
keepalive: s.TCPKeepalive,
keepalivePeriod: s.TCPKeepalivePeriod,
})
}
return s.Serve(ln)
}
Expand Down Expand Up @@ -1523,13 +1523,12 @@ func (s *Server) ListenAndServeTLS(addr, certFile, keyFile string) error {
if err != nil {
return err
}
if s.TCPKeepalive {
if tcpln, ok := ln.(*net.TCPListener); ok {
return s.ServeTLS(tcpKeepaliveListener{
TCPListener: tcpln,
keepalivePeriod: s.TCPKeepalivePeriod,
}, certFile, keyFile)
}
if tcpln, ok := ln.(*net.TCPListener); ok {
return s.ServeTLS(tcpKeepaliveListener{
TCPListener: tcpln,
keepalive: s.TCPKeepalive,
keepalivePeriod: s.TCPKeepalivePeriod,
}, certFile, keyFile)
}
return s.ServeTLS(ln, certFile, keyFile)
}
Expand All @@ -1550,13 +1549,12 @@ func (s *Server) ListenAndServeTLSEmbed(addr string, certData, keyData []byte) e
if err != nil {
return err
}
if s.TCPKeepalive {
if tcpln, ok := ln.(*net.TCPListener); ok {
return s.ServeTLSEmbed(tcpKeepaliveListener{
TCPListener: tcpln,
keepalivePeriod: s.TCPKeepalivePeriod,
}, certData, keyData)
}
if tcpln, ok := ln.(*net.TCPListener); ok {
return s.ServeTLSEmbed(tcpKeepaliveListener{
TCPListener: tcpln,
keepalive: s.TCPKeepalive,
keepalivePeriod: s.TCPKeepalivePeriod,
}, certData, keyData)
}
return s.ServeTLSEmbed(ln, certData, keyData)
}
Expand Down

0 comments on commit bd35133

Please sign in to comment.