Skip to content

Commit

Permalink
Assert with *net.TCPConn instead of *net.TCPListener in acceptConn() …
Browse files Browse the repository at this point in the history
…for TCP sockets (#1432)

Make the code more succinct.
  • Loading branch information
panjf2000 authored Nov 15, 2022
1 parent c57a2ce commit 8a60232
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1871,27 +1871,7 @@ func (s *Server) Shutdown() error {

func acceptConn(s *Server, ln net.Listener, lastPerIPErrorTime *time.Time) (net.Conn, error) {
for {
var c net.Conn
var err error
if tl, ok := ln.(*net.TCPListener); ok && s.TCPKeepalive {
var tc *net.TCPConn
tc, err = tl.AcceptTCP()
if err == nil {
if err := tc.SetKeepAlive(s.TCPKeepalive); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
if s.TCPKeepalivePeriod > 0 {
if err := tc.SetKeepAlivePeriod(s.TCPKeepalivePeriod); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
}
c = tc
}
} else {
c, err = ln.Accept()
}
c, err := ln.Accept()
if err != nil {
if c != nil {
panic("BUG: net.Listener returned non-nil conn and non-nil error")
Expand All @@ -1910,6 +1890,20 @@ func acceptConn(s *Server, ln net.Listener, lastPerIPErrorTime *time.Time) (net.
if c == nil {
panic("BUG: net.Listener returned (nil, nil)")
}

if tc, ok := c.(*net.TCPConn); ok && s.TCPKeepalive {
if err := tc.SetKeepAlive(s.TCPKeepalive); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
if s.TCPKeepalivePeriod > 0 {
if err := tc.SetKeepAlivePeriod(s.TCPKeepalivePeriod); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
}
}

if s.MaxConnsPerIP > 0 {
pic := wrapPerIPConn(s, c)
if pic == nil {
Expand Down

0 comments on commit 8a60232

Please sign in to comment.