State of connection is now three-way choice rather than two booleans

This commit is contained in:
Bryan Boreham committed 2014-09-08 17:30:54 +01:00
1 parent f3cf2b598d
commit 64999d830d
2 files changed
+28 -25

No files matched your search

+24 -22
View File
@@ -16,10 +16,15 @@ const (
CMEnsure = iota CMEnsure = iota
CMStatus = iota CMStatus = iota
CMEstablished = iota CMEstablished = iota
CMConnSucceeded = iota
CMConnFailed = iota CMConnFailed = iota
) )
const (
CMUnconnected ConnectionState = iota
CMAttempting = iota
CMEstablishedState = iota
)
func StartConnectionMaker(router *Router) *ConnectionMaker { func StartConnectionMaker(router *Router) *ConnectionMaker {
queryChan := make(chan *ConnectionMakerInteraction, ChannelSize) queryChan := make(chan *ConnectionMakerInteraction, ChannelSize)
state := &ConnectionMaker{ state := &ConnectionMaker{
@@ -95,27 +100,23 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
case query.code == CMEstablished: case query.code == CMEstablished:
target := cm.targets[query.address] target := cm.targets[query.address]
if target != nil { if target != nil {
target.established = true target.state = CMEstablishedState
} else { } else {
log.Fatal("Connection established to unknown address", query.address) log.Fatal("Connection established to unknown address", query.address)
} }
case query.code == CMConnSucceeded:
cm.targets[query.address].attempting = false
case query.code == CMConnFailed: case query.code == CMConnFailed:
target := cm.targets[query.address] target := cm.targets[query.address]
target.attempting = false target.state = CMUnconnected
after, interval := tryAfter(target.tryInterval) target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
target.tryInterval = interval
target.tryAfter = after
maybeTick() maybeTick()
default: default:
log.Fatal("Unexpected connection maker query:", query) log.Fatal("Unexpected connection maker query:", query)
} }
case now := <-tick: case now := <-tick:
for address, target := range cm.targets { for address, target := range cm.targets {
if !target.established && !target.attempting && now.After(target.tryAfter) { if target.state == CMUnconnected && now.After(target.tryAfter) {
target.attemptCount += 1 target.attemptCount += 1
target.attempting = true target.state = CMAttempting
go cm.attemptConnection(address, target.acceptAnyPeer) go cm.attemptConnection(address, target.acceptAnyPeer)
} }
} }
@@ -129,6 +130,7 @@ func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
target := cm.targets[address] target := cm.targets[address]
if target == nil { if target == nil {
target = &Target{ target = &Target{
state: CMUnconnected,
acceptAnyPeer: acceptAnyPeer, acceptAnyPeer: acceptAnyPeer,
} }
cm.targets[address] = target cm.targets[address] = target
@@ -137,18 +139,21 @@ func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
func (cm *ConnectionMaker) resetTarget(address string) { func (cm *ConnectionMaker) resetTarget(address string) {
target := cm.targets[address] target := cm.targets[address]
target.established = false // If we were in the middle of a connection attempt then back-off again
after, interval := tryImmediately() if target.state == CMAttempting {
target.tryInterval = interval target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
target.tryAfter = after } else {
target.tryAfter, target.tryInterval = tryImmediately()
}
target.state = CMUnconnected
} }
func (cm *ConnectionMaker) status() string { func (cm *ConnectionMaker) status() string {
var buf bytes.Buffer var buf bytes.Buffer
for address, target := range cm.targets { for address, target := range cm.targets {
if target.established { if target.state == CMEstablishedState {
buf.WriteString(fmt.Sprintf("%s connected\n", address)) buf.WriteString(fmt.Sprintf("%s connected\n", address))
} else if target.attempting { } else if target.state == CMAttempting {
buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter)) buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter))
} else { } else {
buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter)) buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter))
@@ -159,15 +164,12 @@ func (cm *ConnectionMaker) status() string {
func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) { func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) {
log.Println("Attempting connection to", address) log.Println("Attempting connection to", address)
var conncode = CMConnSucceeded
if err := cm.router.Ourself.CreateConnection(address, acceptNewPeer); err != nil { if err := cm.router.Ourself.CreateConnection(address, acceptNewPeer); err != nil {
log.Println(err) log.Println(err)
conncode = CMConnFailed cm.queryChan <- &ConnectionMakerInteraction{
Interaction: Interaction{code: CMConnFailed},
address: address}
} }
// Tell the query loop we've finished this attempt
cm.queryChan <- &ConnectionMakerInteraction{
Interaction: Interaction{code: conncode},
address: address}
} }
func tryImmediately() (time.Time, time.Duration) { func tryImmediately() (time.Time, time.Duration) {
+4 -3
View File
@@ -165,11 +165,12 @@ type ConnectionMaker struct {
targets map[string]*Target targets map[string]*Target
} }
type ConnectionState int
// Information about an address where we may find a peer // Information about an address where we may find a peer
type Target struct { type Target struct {
acceptAnyPeer bool // was this address given to us on the command line? acceptAnyPeer bool // was this address given to us on the command line?
attempting bool // are we currently attempting to connect to this address? state ConnectionState
established bool // has the connection been established
tryAfter time.Time // next time to try this address tryAfter time.Time // next time to try this address
tryInterval time.Duration // backoff time on next failure tryInterval time.Duration // backoff time on next failure
attemptCount int attemptCount int