From 5ccc753367d0cd901262236a124f0f015ed33c6e Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 24 Dec 2014 10:33:37 +0000 Subject: [PATCH] kill stale connections The weave protocol already effectively performs heartbeating - FetchAll is performed at regular intervals. Hence we can detect stale connections by the lack of receiving any data for approximately that interval (we use x2). We use the same interval to set a time limit on the handshake too, and any subsequent interactions prior to the connection being fully established. Fixes #272. --- router/connection.go | 7 ++++++- router/consts.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/router/connection.go b/router/connection.go index 9ff2e303..2388a284 100644 --- a/router/connection.go +++ b/router/connection.go @@ -285,8 +285,8 @@ func (conn *LocalConnection) handshake(acceptNewPeer bool) error { // ourself. Only when we add this connection to the conn.local // peer will it be visible from multiple go-routines. tcpConn := conn.TCPConn - tcpConn.SetKeepAlive(true) tcpConn.SetLinger(0) + conn.extendReadDeadline() enc := gob.NewEncoder(tcpConn) @@ -428,6 +428,7 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder, usingPassword bool var err error for { var msg []byte + conn.extendReadDeadline() if conn.CheckFatal(decoder.Decode(&msg)) != nil { return } @@ -501,6 +502,10 @@ func (conn *LocalConnection) receiveTCP(decoder *gob.Decoder, usingPassword bool } } +func (conn *LocalConnection) extendReadDeadline() { + conn.TCPConn.SetReadDeadline(time.Now().Add(ReadTimeout)) +} + // Heartbeats // Heartbeating serves two purposes: a) keeping NAT paths alive, and diff --git a/router/consts.go b/router/consts.go index ae9fa46b..92245167 100644 --- a/router/consts.go +++ b/router/consts.go @@ -22,6 +22,7 @@ const ( SlowHeartbeat = 10 * time.Second FetchAllInterval = 30 * time.Second FragTestInterval = 5 * time.Minute + ReadTimeout = 2 * FetchAllInterval PMTUVerifyAttempts = 8 PMTUVerifyTimeout = 10 * time.Millisecond // gets doubled with every attempt MaxDuration = time.Duration(math.MaxInt64)