From f8ec56ea79042bc65770209c4cac4a636c5507af Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 13 Apr 2015 14:21:11 +0100 Subject: [PATCH] Move connection shutdown logging into shutdown() --- router/connection.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/router/connection.go b/router/connection.go index a17a6fea..d7cebcd0 100644 --- a/router/connection.go +++ b/router/connection.go @@ -284,7 +284,8 @@ func (conn *LocalConnection) sendAction(action ConnectionAction) { // ACTOR server func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished chan<- struct{}, acceptNewPeer bool) { - defer conn.shutdown() + var err error // important to use this var and not create another one with 'err :=' + defer func() { conn.shutdown(err) }() defer close(finished) tcpConn := conn.TCPConn @@ -292,14 +293,12 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch enc := gob.NewEncoder(tcpConn) dec := gob.NewDecoder(tcpConn) - if err := conn.handshake(enc, dec, acceptNewPeer); err != nil { - log.Printf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err) + if err = conn.handshake(enc, dec, acceptNewPeer); err != nil { return } log.Printf("->[%s] completed handshake with %s\n", conn.remoteTCPAddr, conn.remote.FullName()) - if err := conn.initHeartbeats(); err != nil { // [1] - conn.Log("connection shutting down due to error:", err) + if err = conn.initHeartbeats(); err != nil { // [1] return } @@ -307,11 +306,7 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch go conn.receiveTCP(dec) // [1] - if err := conn.actorLoop(actionChan); err != nil { // [1] - conn.Log("connection shutting down due to error:", err) - return - } - conn.Log("connection shutting down") + err = conn.actorLoop(actionChan) // [1] } // [1] Ordering constraints: @@ -376,7 +371,16 @@ func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err return } -func (conn *LocalConnection) shutdown() { +func (conn *LocalConnection) shutdown(err error) { + switch { + case err != nil && conn.remote == nil: + log.Printf("->[%s] connection shutting down due to error during handshake: %v\n", conn.remoteTCPAddr, err) + case err != nil: + conn.Log("connection shutting down due to error:", err) + default: + conn.Log("connection shutting down") + } + if conn.TCPConn != nil { checkWarn(conn.TCPConn.Close()) }