From c6d8623b6fd5beac71481fe385b43b8855073320 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 10 Sep 2014 16:17:56 +0100 Subject: [PATCH] Only signal connection failures; remove successful connections when we see them --- connection.go | 4 ++++ connection_maker.go | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/connection.go b/connection.go index f0aed6f9..a88d7d42 100644 --- a/connection.go +++ b/connection.go @@ -277,6 +277,10 @@ func (conn *LocalConnection) handleShutdown() { // blank out the forwardChan so that the router processes don't // try to send any more conn.stopForwarders() + + if conn.remote != nil { + conn.Router.ConnectionMaker.ShutdownConnection(conn.remoteTCPAddr) + } } func (conn *LocalConnection) handshake(acceptNewPeer bool) error { diff --git a/connection_maker.go b/connection_maker.go index 8501216b..babcee3b 100644 --- a/connection_maker.go +++ b/connection_maker.go @@ -14,7 +14,7 @@ const ( MaxAttemptCount = 100 CMInitiate = iota CMStatus = iota - CMAttemptDone = iota + CMFailed = iota ) const ( @@ -39,6 +39,12 @@ func (cm *ConnectionMaker) InitiateConnection(address string) { address: address} } +func (cm *ConnectionMaker) ShutdownConnection(address string) { + cm.queryChan <- &ConnectionMakerInteraction{ + Interaction: Interaction{code: CMFailed}, + address: address} +} + func (cm *ConnectionMaker) String() string { resultChan := make(chan interface{}, 0) cm.queryChan <- &ConnectionMakerInteraction{ @@ -68,13 +74,11 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio maybeTick() case query.code == CMStatus: query.resultChan <- cm.status() - case query.code == CMAttemptDone: + case query.code == CMFailed: if target, found := cm.targets[query.address]; found { target.state = CSUnconnected target.tryAfter, target.tryInterval = tryAfter(target.tryInterval) maybeTick() - } else { - log.Fatal("CMAttemptDone unknown address", query.address) } default: log.Fatal("Unexpected connection maker query:", query) @@ -142,6 +146,9 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections(now time.Time) { target.state = CSAttempting go cm.attemptConnection(address, cm.cmdLineAddress[address]) } + } else if our_connected_targets[address] { + //log.Println("Deleting target now connected:", address) + delete(cm.targets, address) } } } @@ -174,10 +181,8 @@ func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) log.Println("Attempting connection to", address) if err := cm.router.Ourself.CreateConnection(address, acceptNewPeer); err != nil { log.Println(err) + cm.ShutdownConnection(address) } - cm.queryChan <- &ConnectionMakerInteraction{ - Interaction: Interaction{code: CMAttemptDone}, - address: address} } func tryImmediately() (time.Time, time.Duration) {