server-mux: Add tcp idle read timeout (#3607)

Avoid many idle client connections i.e client didn't send any data until a given stipulated amount of time. 

Default chosen here is 30 seconds.
This commit is contained in:
Anis Elleuch 2017-01-22 23:48:27 +01:00 committed by Harshavardhana
parent 3640c63289
commit 47358e3104
1 changed files with 8 additions and 0 deletions

View File

@ -111,6 +111,8 @@ func (c *ConnMux) PeekProtocol() (string, error) {
// Read - streams the ConnMux buffer when reset flag is activated, otherwise // Read - streams the ConnMux buffer when reset flag is activated, otherwise
// streams from the incoming network connection // streams from the incoming network connection
func (c *ConnMux) Read(b []byte) (int, error) { func (c *ConnMux) Read(b []byte) (int, error) {
// Push read deadline
c.Conn.SetReadDeadline(time.Now().Add(defaultTCPReadTimeout))
return c.bufrw.Read(b) return c.bufrw.Read(b)
} }
@ -176,6 +178,9 @@ type ListenerMuxAcceptRes struct {
// Effective value of total keep alive comes upto 9 x 10 * time.Second = 1.5 Minutes. // Effective value of total keep alive comes upto 9 x 10 * time.Second = 1.5 Minutes.
var defaultKeepAliveTimeout = 10 * time.Second // 10 seconds. var defaultKeepAliveTimeout = 10 * time.Second // 10 seconds.
// Timeout to close connection when a client is not sending any data
var defaultTCPReadTimeout = 30 * time.Second
// newListenerMux listens and wraps accepted connections with tls after protocol peeking // newListenerMux listens and wraps accepted connections with tls after protocol peeking
func newListenerMux(listener net.Listener, config *tls.Config) *ListenerMux { func newListenerMux(listener net.Listener, config *tls.Config) *ListenerMux {
l := ListenerMux{ l := ListenerMux{
@ -202,6 +207,9 @@ func newListenerMux(listener net.Listener, config *tls.Config) *ListenerMux {
return return
} }
// Enable Read timeout
conn.SetReadDeadline(time.Now().Add(defaultTCPReadTimeout))
// Enable keep alive for each connection. // Enable keep alive for each connection.
conn.SetKeepAlive(true) conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(defaultKeepAliveTimeout) conn.SetKeepAlivePeriod(defaultKeepAliveTimeout)