From 70b1f77fc7c24b9eb0e279acd7653fc2e391efaf Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Tue, 30 Jan 2018 20:11:54 +0200 Subject: [PATCH] Set keep-alives for TCP connections --- pupy/proxy/daemon.go | 6 ++++++ pupy/proxy/streams.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/pupy/proxy/daemon.go b/pupy/proxy/daemon.go index 5bfd9526..5bc884e9 100644 --- a/pupy/proxy/daemon.go +++ b/pupy/proxy/daemon.go @@ -5,6 +5,8 @@ import ( "net" "net/http" + "time" + "sync/atomic" log "github.com/sirupsen/logrus" @@ -65,6 +67,10 @@ func (d *Daemon) onListenerDisabled() { func (d *Daemon) handle(conn net.Conn) { defer conn.Close() + conn.(*net.TCPConn).SetKeepAlive(true) + conn.(*net.TCPConn).SetKeepAlivePeriod(1 * time.Minute) + conn.(*net.TCPConn).SetNoDelay(true) + brh := &BindRequestHeader{} err := RecvMessage(conn, brh) diff --git a/pupy/proxy/streams.go b/pupy/proxy/streams.go index 164e5c71..0ba294e8 100644 --- a/pupy/proxy/streams.go +++ b/pupy/proxy/streams.go @@ -6,6 +6,8 @@ import ( "strconv" "strings" + "time" + "errors" "crypto/tls" @@ -201,6 +203,10 @@ func (d *Daemon) listenAcceptTCP(in net.Conn, port int, cherr chan error, chconn log.Debug("Acceptor exited") return } else { + conn.(*net.TCPConn).SetKeepAlive(true) + conn.(*net.TCPConn).SetKeepAlivePeriod(1 * time.Minute) + conn.(*net.TCPConn).SetNoDelay(true) + chconn <- conn } @@ -253,6 +259,10 @@ func (d *Daemon) listenAcceptTLS(in net.Conn, port int, cherr chan error, chconn log.Debug("Acceptor exited") return } else { + conn.(*net.TCPConn).SetKeepAlive(true) + conn.(*net.TCPConn).SetKeepAlivePeriod(1 * time.Minute) + conn.(*net.TCPConn).SetNoDelay(true) + chconn <- conn }