Change ConnectionMaker from being focused on PeerName to being focused on address

This commit is contained in:
Bryan Boreham committed 2014-09-08 12:42:06 +01:00
1 parent b69682a626
commit 0a94388f0a
2 files changed
+42 -66

No files matched your search

+34 -55
View File
@@ -33,8 +33,7 @@ func StartConnectionMaker(router *Router) *ConnectionMaker {
state := &ConnectionMaker{ state := &ConnectionMaker{
router: router, router: router,
queryChan: queryChan, queryChan: queryChan,
failedConnections: make(map[PeerName]*FailedConnection), targets: make(map[string]*Target)}
attempting: make(map[ConnectionMakerPair]bool)}
go state.queryLoop(queryChan) go state.queryLoop(queryChan)
return state return state
} }
@@ -57,7 +56,8 @@ func (cm *ConnectionMaker) String() string {
func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteraction) { func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteraction) {
var tick <-chan time.Time var tick <-chan time.Time
maybeTick := func() { maybeTick := func() {
if tick == nil && len(cm.failedConnections) > 0 { // would be nice to optimise this to stop ticking when there is nothing worth trying
if tick == nil {
tick = time.After(5 * time.Second) tick = time.After(5 * time.Second)
} }
} }
@@ -70,47 +70,31 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
switch { switch {
case query.code == CMEnsure: case query.code == CMEnsure:
if query.name != cm.router.Ourself.Name { if query.name != cm.router.Ourself.Name {
cm.addToFailedConnection(query.name, query.foundAt) cm.addToTargets(query.name, query.foundAt)
maybeTick() maybeTick()
} }
case query.code == CMStatus: case query.code == CMStatus:
query.resultChan <- cm.status() query.resultChan <- cm.status()
case query.code == CMConnSucceeded: case query.code == CMConnSucceeded:
delete(cm.attempting, ConnectionMakerPair{query.foundAt, query.name}) cm.targets[query.foundAt].attempting = false
delete(cm.failedConnections[query.name].foundAt, query.foundAt)
case query.code == CMConnFailed: case query.code == CMConnFailed:
delete(cm.attempting, ConnectionMakerPair{query.foundAt, query.name}) cm.targets[query.foundAt].attempting = false
default: default:
log.Fatal("Unexpected connection maker query:", query) log.Fatal("Unexpected connection maker query:", query)
} }
case now := <-tick: case now := <-tick:
ConnectionLoop: for address, target := range cm.targets {
for name, failedConn := range cm.failedConnections { if target.conn == nil && !target.attempting && now.After(target.tryAfter) {
if now.After(failedConn.tryAfter) { if target.attemptCount == MaxAttemptCount {
if _, found := cm.router.Ourself.ConnectionTo(name); found { // FIXME
delete(cm.failedConnections, name)
continue
} else if len(failedConn.foundAt) == 0 {
delete(cm.failedConnections, name)
continue
} else if failedConn.attemptCount == MaxAttemptCount {
delete(cm.failedConnections, name)
continue continue
} }
// wait until all current attempts to contact this peer have failed before starting a new batch after, interval := tryAfter(target.tryInterval)
for target := range failedConn.foundAt { target.tryInterval = interval
if cm.attempting[ConnectionMakerPair{target, name}] { target.tryAfter = after
continue ConnectionLoop target.attemptCount += 1
} target.attempting = true;
} go cm.attemptConnection(address, target.commandLine)
after, interval := tryAfter(failedConn.tryInterval)
failedConn.tryInterval = interval
failedConn.tryAfter = after
failedConn.attemptCount += 1
for target := range failedConn.foundAt {
cm.attempting[ConnectionMakerPair{target, name}] = true
go cm.attemptConnection(target, name)
}
} }
} }
tick = nil tick = nil
@@ -119,58 +103,53 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
} }
} }
func (cm *ConnectionMaker) addToFailedConnection(name PeerName, foundAt string) { func (cm *ConnectionMaker) addToTargets(name PeerName, foundAt string) {
failed := cm.failedConnections[name] target := cm.targets[foundAt]
if failed == nil { if target == nil {
after, interval := tryAfter(InitialInterval) after, interval := tryAfter(InitialInterval)
failed = &FailedConnection{ target = &Target{
foundAt: make(map[string]bool),
tryInterval: interval, tryInterval: interval,
tryAfter: after} tryAfter: after}
} }
foundAtHost, foundAtPortStr, err := net.SplitHostPort(foundAt) foundAtHost, foundAtPortStr, err := net.SplitHostPort(foundAt)
if err == nil { if err == nil {
// ensure port-less version is there // ensure port-less version is there
failed.foundAt[foundAtHost] = true cm.targets[foundAtHost] = target
if foundAtPort, err := strconv.Atoi(foundAtPortStr); err == nil && foundAtPort != Port { if foundAtPort, err := strconv.Atoi(foundAtPortStr); err == nil && foundAtPort != Port {
failed.foundAt[foundAt] = true cm.targets[foundAt] = target
} }
} else { } else {
// can't split it, assume it must not have port on it // can't split it, assume it must not have port on it
failed.foundAt[foundAt] = true cm.targets[foundAt] = target
} }
cm.failedConnections[name] = failed
} }
func (cm *ConnectionMaker) status() string { func (cm *ConnectionMaker) status() string {
var buf bytes.Buffer var buf bytes.Buffer
for name, failedConn := range cm.failedConnections { for address, target := range cm.targets {
tryingNow := false if (target.conn != nil) {
foundAt := make([]string, 0, len(failedConn.foundAt)) buf.WriteString(fmt.Sprintf("%s connected to: %v\n", address, target.conn))
for target := range failedConn.foundAt { } else if target.attempting {
foundAt = append(foundAt, target) buf.WriteString(fmt.Sprintf("%s (%v attempts, trying again now)\n", address, target.attemptCount))
tryingNow = tryingNow || cm.attempting[ConnectionMakerPair{target, name}]
}
if tryingNow {
buf.WriteString(fmt.Sprintf("%s (%v attempts, trying again now): %v\n", name, failedConn.attemptCount, foundAt))
} else { } else {
buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v): %v\n", name, failedConn.attemptCount, failedConn.tryAfter, foundAt)) buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter))
} }
} }
return buf.String() return buf.String()
} }
func (cm *ConnectionMaker) attemptConnection(foundAt string, targetName PeerName) { func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) {
log.Println("Attempting connection to ", address)
var conncode = CMConnSucceeded var conncode = CMConnSucceeded
if err := cm.router.Ourself.CreateConnection(foundAt, targetName); err != nil { if err := cm.router.Ourself.CreateConnection(address, UnknownPeerName); err != nil {
log.Println(err) log.Println(err)
conncode = CMConnFailed conncode = CMConnFailed
} }
// Tell the query loop we've finished this attempt // Tell the query loop we've finished this attempt
cm.queryChan <- &ConnectionMakerInteraction{ cm.queryChan <- &ConnectionMakerInteraction{
Interaction: Interaction{code: conncode}, Interaction: Interaction{code: conncode},
name: targetName, name: UnknownPeerName,
foundAt: foundAt} foundAt: address}
} }
func tryAfter(interval time.Duration) (time.Time, time.Duration) { func tryAfter(interval time.Duration) (time.Time, time.Duration) {
+8 -11
View File
@@ -159,22 +159,19 @@ type ConnectionMakerInteraction struct {
name PeerName name PeerName
} }
type ConnectionMakerPair struct {
foundAt string
name PeerName
}
type ConnectionMaker struct { type ConnectionMaker struct {
router *Router router *Router
queryChan chan<- *ConnectionMakerInteraction queryChan chan<- *ConnectionMakerInteraction
failedConnections map[PeerName]*FailedConnection targets map[string]*Target
attempting map[ConnectionMakerPair]bool
} }
type FailedConnection struct { // Information about an address where we may find a peer
foundAt map[string]bool type Target struct {
tryAfter time.Time commandLine bool // was this address given to us on the command line?
tryInterval time.Duration attempting bool // are we currently attempting to connect to this address?
conn *Connection
tryAfter time.Time // next time to try this address
tryInterval time.Duration // backoff time on next failure
attemptCount int attemptCount int
} }