Attempt to connect immediately when given a new connection

This commit is contained in:
Bryan Boreham committed 2014-09-08 16:05:54 +01:00
1 parent 5cf2cb0c50
commit a3312c822c
1 file changed
+25 -9
+25 -9
View File
@@ -10,7 +10,7 @@ import (
) )
const ( const (
InitialInterval = 1 * time.Second InitialInterval = 5 * time.Second
MaxInterval = 10 * time.Minute MaxInterval = 10 * time.Minute
MaxAttemptCount = 100 MaxAttemptCount = 100
CMEnsure = iota CMEnsure = iota
@@ -74,6 +74,11 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
tick = time.After(5 * time.Second) tick = time.After(5 * time.Second)
} }
} }
tickNow := func() {
if tick == nil {
tick = time.After(0 * time.Second)
}
}
for { for {
select { select {
case query, ok := <-queryChan: case query, ok := <-queryChan:
@@ -83,14 +88,16 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
switch { switch {
case query.code == CMEnsure: case query.code == CMEnsure:
cm.addToTargets(query.acceptAnyPeer, query.address) cm.addToTargets(query.acceptAnyPeer, query.address)
maybeTick() cm.resetTarget(query.address)
tickNow()
case query.code == CMStatus: case query.code == CMStatus:
query.resultChan <- cm.status() query.resultChan <- cm.status()
case query.code == CMEstablished: case query.code == CMEstablished:
target := cm.targets[query.address] target := cm.targets[query.address]
log.Println("Connection established to", query.address, target)
if target != nil { if target != nil {
target.established = true target.established = true
} else {
log.Fatal("Connection established to unknown address", query.address)
} }
case query.code == CMConnSucceeded: case query.code == CMConnSucceeded:
cm.targets[query.address].attempting = false cm.targets[query.address].attempting = false
@@ -121,15 +128,19 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) { func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
target := cm.targets[address] target := cm.targets[address]
if target == nil { if target == nil {
after, interval := tryAfter(InitialInterval)
target = &Target{ target = &Target{
acceptAnyPeer: acceptAnyPeer, acceptAnyPeer: acceptAnyPeer,
tryInterval: interval, }
tryAfter: after} cm.targets[address] = target
} }
target.established = false // this seems a crap place to do this }
// FIXME: what does it mean if an address is added twice?
cm.targets[address] = target func (cm *ConnectionMaker) resetTarget(address string) {
target := cm.targets[address]
target.established = false
after, interval := tryImmediately()
target.tryInterval = interval
target.tryAfter = after
} }
func (cm *ConnectionMaker) status() string { func (cm *ConnectionMaker) status() string {
@@ -159,6 +170,11 @@ func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool)
address: address} address: address}
} }
func tryImmediately() (time.Time, time.Duration) {
interval := time.Duration(rand.Int63n(int64(InitialInterval)))
return time.Now(), interval
}
func tryAfter(interval time.Duration) (time.Time, time.Duration) { func tryAfter(interval time.Duration) (time.Time, time.Duration) {
interval += time.Duration(rand.Int63n(int64(interval))) interval += time.Duration(rand.Int63n(int64(interval)))
if interval > MaxInterval { if interval > MaxInterval {