Skip to content

Commit 8a60232

Browse files
authored
Assert with *net.TCPConn instead of *net.TCPListener in acceptConn() for TCP sockets (#1432)
Make the code more succinct.
1 parent c57a2ce commit 8a60232

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

Diff for: server.go

+15-21
Original file line numberDiff line numberDiff line change
@@ -1871,27 +1871,7 @@ func (s *Server) Shutdown() error {
18711871

18721872
func acceptConn(s *Server, ln net.Listener, lastPerIPErrorTime *time.Time) (net.Conn, error) {
18731873
for {
1874-
var c net.Conn
1875-
var err error
1876-
if tl, ok := ln.(*net.TCPListener); ok && s.TCPKeepalive {
1877-
var tc *net.TCPConn
1878-
tc, err = tl.AcceptTCP()
1879-
if err == nil {
1880-
if err := tc.SetKeepAlive(s.TCPKeepalive); err != nil {
1881-
tc.Close() //nolint:errcheck
1882-
return nil, err
1883-
}
1884-
if s.TCPKeepalivePeriod > 0 {
1885-
if err := tc.SetKeepAlivePeriod(s.TCPKeepalivePeriod); err != nil {
1886-
tc.Close() //nolint:errcheck
1887-
return nil, err
1888-
}
1889-
}
1890-
c = tc
1891-
}
1892-
} else {
1893-
c, err = ln.Accept()
1894-
}
1874+
c, err := ln.Accept()
18951875
if err != nil {
18961876
if c != nil {
18971877
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.
19101890
if c == nil {
19111891
panic("BUG: net.Listener returned (nil, nil)")
19121892
}
1893+
1894+
if tc, ok := c.(*net.TCPConn); ok && s.TCPKeepalive {
1895+
if err := tc.SetKeepAlive(s.TCPKeepalive); err != nil {
1896+
tc.Close() //nolint:errcheck
1897+
return nil, err
1898+
}
1899+
if s.TCPKeepalivePeriod > 0 {
1900+
if err := tc.SetKeepAlivePeriod(s.TCPKeepalivePeriod); err != nil {
1901+
tc.Close() //nolint:errcheck
1902+
return nil, err
1903+
}
1904+
}
1905+
}
1906+
19131907
if s.MaxConnsPerIP > 0 {
19141908
pic := wrapPerIPConn(s, c)
19151909
if pic == nil {

0 commit comments

Comments
 (0)