From 4e442393b2dda7001b5dd629079025089f4520b4 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 10 Sep 2014 16:39:05 +0100 Subject: [PATCH] refactor: 'enum' -> bool --- connection_maker.go | 17 +++++------------ types.go | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/connection_maker.go b/connection_maker.go index dedf2851..aa6f0440 100644 --- a/connection_maker.go +++ b/connection_maker.go @@ -20,11 +20,6 @@ const ( CMFailed = iota ) -const ( - CSUnconnected ConnectionState = iota - CSAttempting = iota -) - func StartConnectionMaker(router *Router) *ConnectionMaker { queryChan := make(chan *ConnectionMakerInteraction, ChannelSize) state := &ConnectionMaker{ @@ -79,7 +74,7 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio query.resultChan <- cm.status() case query.code == CMFailed: if target, found := cm.targets[query.address]; found { - target.state = CSUnconnected + target.attempting = false target.tryAfter, target.tryInterval = tryAfter(target.tryInterval) maybeTick() } @@ -140,13 +135,13 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections(now time.Time) { now = time.Now() // make sure we catch items just added for address, target := range cm.targets { - if target.state == CSUnconnected { + if !target.attempting { if our_connected_targets[address] || !validTarget[address] { //log.Println("Deleting target no longer valid:", address) delete(cm.targets, address) } else if now.After(target.tryAfter) { + target.attempting = true target.attemptCount += 1 - target.state = CSAttempting go cm.attemptConnection(address, cm.cmdLineAddress[address]) } } else if our_connected_targets[address] { @@ -160,9 +155,7 @@ func (cm *ConnectionMaker) addToTargets(address string) { address = NormalisePeerAddr(address) target := cm.targets[address] if target == nil { - target = &Target{ - state: CSUnconnected, - } + target = &Target{} target.tryAfter, target.tryInterval = tryImmediately() cm.targets[address] = target } @@ -171,7 +164,7 @@ func (cm *ConnectionMaker) addToTargets(address string) { func (cm *ConnectionMaker) status() string { var buf bytes.Buffer for address, target := range cm.targets { - if target.state == CSAttempting { + if target.attempting { buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter)) } else { buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter)) diff --git a/types.go b/types.go index afff09d7..da13b0df 100644 --- a/types.go +++ b/types.go @@ -169,7 +169,7 @@ type ConnectionState int // Information about an address where we may find a peer type Target struct { - state ConnectionState + attempting bool // are we currently attempting to connect there? tryAfter time.Time // next time to try this address tryInterval time.Duration // backoff time on next failure attemptCount int