|
7 | 7 | "net" |
8 | 8 | "net/http" |
9 | 9 | "strings" |
| 10 | + "time" |
10 | 11 |
|
11 | 12 | log "github.com/sirupsen/logrus" |
12 | 13 | ) |
@@ -48,68 +49,89 @@ func (s *Server) acceptMuxConnections(listener net.Listener, httpListener *muxLi |
48 | 49 | continue |
49 | 50 | } |
50 | 51 |
|
51 | | - tlsConn, ok := conn.(*tls.Conn) |
52 | | - if ok { |
53 | | - if errHandshake := tlsConn.Handshake(); errHandshake != nil { |
54 | | - if errClose := conn.Close(); errClose != nil { |
55 | | - log.Errorf("failed to close connection after TLS handshake error: %v", errClose) |
56 | | - } |
57 | | - continue |
58 | | - } |
59 | | - proto := strings.TrimSpace(tlsConn.ConnectionState().NegotiatedProtocol) |
60 | | - if proto == "h2" || proto == "http/1.1" { |
61 | | - if httpListener == nil { |
62 | | - if errClose := conn.Close(); errClose != nil { |
63 | | - log.Errorf("failed to close connection: %v", errClose) |
64 | | - } |
65 | | - continue |
66 | | - } |
67 | | - if errPut := httpListener.Put(tlsConn); errPut != nil { |
68 | | - if errClose := conn.Close(); errClose != nil { |
69 | | - log.Errorf("failed to close connection after HTTP routing failure: %v", errClose) |
70 | | - } |
71 | | - } |
72 | | - continue |
73 | | - } |
74 | | - } |
| 52 | + // Dispatch each connection to a goroutine so that slow/idle clients |
| 53 | + // cannot block the accept loop. Previously, TLS handshake and |
| 54 | + // reader.Peek(1) were performed inline; an idle TCP connection that |
| 55 | + // never sent bytes would block Peek indefinitely, preventing all |
| 56 | + // subsequent connections from being accepted (issue #3267). |
| 57 | + go s.routeMuxConnection(conn, httpListener) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// routeMuxConnection performs per-connection protocol detection and routing. |
| 62 | +func (s *Server) routeMuxConnection(conn net.Conn, httpListener *muxListener) { |
| 63 | + // Set a read deadline so that idle connections that never send bytes do not |
| 64 | + // leak goroutines and file descriptors. The deadline is cleared once the |
| 65 | + // connection is successfully routed to its handler. |
| 66 | + const muxSniffDeadline = 10 * time.Second |
| 67 | + _ = conn.SetReadDeadline(time.Now().Add(muxSniffDeadline)) |
75 | 68 |
|
76 | | - reader := bufio.NewReader(conn) |
77 | | - prefix, errPeek := reader.Peek(1) |
78 | | - if errPeek != nil { |
| 69 | + tlsConn, ok := conn.(*tls.Conn) |
| 70 | + if ok { |
| 71 | + if errHandshake := tlsConn.Handshake(); errHandshake != nil { |
79 | 72 | if errClose := conn.Close(); errClose != nil { |
80 | | - log.Errorf("failed to close connection after protocol peek failure: %v", errClose) |
| 73 | + log.Errorf("failed to close connection after TLS handshake error: %v", errClose) |
81 | 74 | } |
82 | | - continue |
| 75 | + return |
83 | 76 | } |
84 | | - |
85 | | - if isRedisRESPPrefix(prefix[0]) { |
86 | | - if s.cfg != nil && s.cfg.Home.Enabled { |
| 77 | + proto := strings.TrimSpace(tlsConn.ConnectionState().NegotiatedProtocol) |
| 78 | + if proto == "h2" || proto == "http/1.1" { |
| 79 | + if httpListener == nil { |
87 | 80 | if errClose := conn.Close(); errClose != nil { |
88 | | - log.Errorf("failed to close redis connection while home mode is enabled: %v", errClose) |
| 81 | + log.Errorf("failed to close connection: %v", errClose) |
89 | 82 | } |
90 | | - continue |
| 83 | + return |
91 | 84 | } |
92 | | - if !s.managementRoutesEnabled.Load() { |
| 85 | + if errPut := httpListener.Put(tlsConn); errPut != nil { |
93 | 86 | if errClose := conn.Close(); errClose != nil { |
94 | | - log.Errorf("failed to close redis connection while management is disabled: %v", errClose) |
| 87 | + log.Errorf("failed to close connection after HTTP routing failure: %v", errClose) |
95 | 88 | } |
96 | | - continue |
| 89 | + } else { |
| 90 | + _ = conn.SetReadDeadline(time.Time{}) |
97 | 91 | } |
98 | | - go s.handleRedisConnection(conn, reader) |
99 | | - continue |
| 92 | + return |
100 | 93 | } |
| 94 | + } |
| 95 | + |
| 96 | + reader := bufio.NewReader(conn) |
| 97 | + prefix, errPeek := reader.Peek(1) |
| 98 | + if errPeek != nil { |
| 99 | + if errClose := conn.Close(); errClose != nil { |
| 100 | + log.Errorf("failed to close connection after protocol peek failure: %v", errClose) |
| 101 | + } |
| 102 | + return |
| 103 | + } |
101 | 104 |
|
102 | | - if httpListener == nil { |
| 105 | + if isRedisRESPPrefix(prefix[0]) { |
| 106 | + if s.cfg != nil && s.cfg.Home.Enabled { |
103 | 107 | if errClose := conn.Close(); errClose != nil { |
104 | | - log.Errorf("failed to close connection without HTTP listener: %v", errClose) |
| 108 | + log.Errorf("failed to close redis connection while home mode is enabled: %v", errClose) |
105 | 109 | } |
106 | | - continue |
| 110 | + return |
107 | 111 | } |
108 | | - |
109 | | - if errPut := httpListener.Put(&bufferedConn{Conn: conn, reader: reader}); errPut != nil { |
| 112 | + if !s.managementRoutesEnabled.Load() { |
110 | 113 | if errClose := conn.Close(); errClose != nil { |
111 | | - log.Errorf("failed to close connection after HTTP routing failure: %v", errClose) |
| 114 | + log.Errorf("failed to close redis connection while management is disabled: %v", errClose) |
112 | 115 | } |
| 116 | + return |
| 117 | + } |
| 118 | + _ = conn.SetReadDeadline(time.Time{}) |
| 119 | + s.handleRedisConnection(conn, reader) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + if httpListener == nil { |
| 124 | + if errClose := conn.Close(); errClose != nil { |
| 125 | + log.Errorf("failed to close connection without HTTP listener: %v", errClose) |
| 126 | + } |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + if errPut := httpListener.Put(&bufferedConn{Conn: conn, reader: reader}); errPut != nil { |
| 131 | + if errClose := conn.Close(); errClose != nil { |
| 132 | + log.Errorf("failed to close connection after HTTP routing failure: %v", errClose) |
113 | 133 | } |
| 134 | + } else { |
| 135 | + _ = conn.SetReadDeadline(time.Time{}) |
114 | 136 | } |
115 | 137 | } |
0 commit comments