From 47358e31049eed047f861cd1bb54d38b8413e92e Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Sun, 22 Jan 2017 23:48:27 +0100 Subject: [PATCH] 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. --- cmd/server-mux.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/server-mux.go b/cmd/server-mux.go index 1d7458d2c..e98741bb6 100644 --- a/cmd/server-mux.go +++ b/cmd/server-mux.go @@ -111,6 +111,8 @@ func (c *ConnMux) PeekProtocol() (string, error) { // Read - streams the ConnMux buffer when reset flag is activated, otherwise // streams from the incoming network connection func (c *ConnMux) Read(b []byte) (int, error) { + // Push read deadline + c.Conn.SetReadDeadline(time.Now().Add(defaultTCPReadTimeout)) 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. 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 func newListenerMux(listener net.Listener, config *tls.Config) *ListenerMux { l := ListenerMux{ @@ -202,6 +207,9 @@ func newListenerMux(listener net.Listener, config *tls.Config) *ListenerMux { return } + // Enable Read timeout + conn.SetReadDeadline(time.Now().Add(defaultTCPReadTimeout)) + // Enable keep alive for each connection. conn.SetKeepAlive(true) conn.SetKeepAlivePeriod(defaultKeepAliveTimeout)