diff options
author | Andy Pan <panjf2000@gmail.com> | 2022-11-15 23:34:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-15 16:34:34 +0100 |
commit | 8a60232af1680ce47809d060c439c33343eb0284 (patch) | |
tree | 3e73d91c5a65971bfb9d9a91fb70a51b238f6c06 /server.go | |
parent | Make sure nothing is nil in tmp slice (#1423) (diff) | |
download | fasthttp-8a60232af1680ce47809d060c439c33343eb0284.tar.gz fasthttp-8a60232af1680ce47809d060c439c33343eb0284.tar.bz2 fasthttp-8a60232af1680ce47809d060c439c33343eb0284.zip |
Assert with *net.TCPConn instead of *net.TCPListener in acceptConn() for TCP sockets (#1432)
Make the code more succinct.
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 36 |
1 files changed, 15 insertions, 21 deletions
@@ -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") @@ -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 { |