From dec794eb72cb72f985013ac429adaf253a4f8d57 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 8 Sep 2014 15:05:50 +0100 Subject: [PATCH] Remove all references to PeerName in ConnectionMaker --- connection.go | 2 +- connection_maker.go | 52 ++++++++++++++++++++++----------------------- peer.go | 5 +++-- types.go | 6 +++--- weaver/main.go | 2 +- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/connection.go b/connection.go index 138df0c6..37363b19 100644 --- a/connection.go +++ b/connection.go @@ -279,7 +279,7 @@ func (conn *LocalConnection) handleShutdown() { conn.stopForwarders() if conn.remote != nil { - conn.Router.ConnectionMaker.EnsureConnection(conn.remote.Name, conn.remoteTCPAddr) + conn.Router.ConnectionMaker.EnsureConnection(conn.remoteTCPAddr) } } diff --git a/connection_maker.go b/connection_maker.go index df7346df..73530b46 100644 --- a/connection_maker.go +++ b/connection_maker.go @@ -1,13 +1,5 @@ package weave -/* ConnectionMaker is responsible for making connections between peers, -and retrying them when they break. It sits in a loop waiting for requests -to be sent over queryChan, or for timer ticks. -Every connection has a PeerName, which is a unique identifier for every weave -router (e.g. a MAC address), and an IP address which it was foundAt. - -*/ - import ( "bytes" "fmt" @@ -38,11 +30,18 @@ func StartConnectionMaker(router *Router) *ConnectionMaker { return state } -func (cm *ConnectionMaker) EnsureConnection(name PeerName, foundAt string) { +func (cm *ConnectionMaker) InitiateConnection(address string) { cm.queryChan <- &ConnectionMakerInteraction{ Interaction: Interaction{code: CMEnsure}, - name: name, - foundAt: foundAt} + acceptAnyPeer: true, + address: address} +} + +func (cm *ConnectionMaker) EnsureConnection(address string) { + cm.queryChan <- &ConnectionMakerInteraction{ + Interaction: Interaction{code: CMEnsure}, + acceptAnyPeer: false, + address: address} } func (cm *ConnectionMaker) String() string { @@ -69,16 +68,14 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio } switch { case query.code == CMEnsure: - if query.name != cm.router.Ourself.Name { - cm.addToTargets(query.name, query.foundAt) - maybeTick() - } + cm.addToTargets(query.acceptAnyPeer, query.address) + maybeTick() case query.code == CMStatus: query.resultChan <- cm.status() case query.code == CMConnSucceeded: - cm.targets[query.foundAt].attempting = false + cm.targets[query.address].attempting = false case query.code == CMConnFailed: - target := cm.targets[query.foundAt] + target := cm.targets[query.address] target.attempting = false after, interval := tryAfter(target.tryInterval) target.tryInterval = interval @@ -92,7 +89,7 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio if target.conn == nil && !target.attempting && now.After(target.tryAfter) { target.attemptCount += 1 target.attempting = true; - go cm.attemptConnection(address, target.commandLine) + go cm.attemptConnection(address, target.acceptAnyPeer) } } tick = nil @@ -101,24 +98,26 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio } } -func (cm *ConnectionMaker) addToTargets(name PeerName, foundAt string) { - target := cm.targets[foundAt] +func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) { + target := cm.targets[address] if target == nil { after, interval := tryAfter(InitialInterval) target = &Target{ + acceptAnyPeer: acceptAnyPeer, tryInterval: interval, tryAfter: after} } - foundAtHost, foundAtPortStr, err := net.SplitHostPort(foundAt) + // FIXME: what does it mean if an address is added twice? + addrHost, addrPortStr, err := net.SplitHostPort(address) if err == nil { // ensure port-less version is there - cm.targets[foundAtHost] = target - if foundAtPort, err := strconv.Atoi(foundAtPortStr); err == nil && foundAtPort != Port { - cm.targets[foundAt] = target + cm.targets[addrHost] = target + if addrPort, err := strconv.Atoi(addrPortStr); err == nil && addrPort != Port { + cm.targets[address] = target } } else { // can't split it, assume it must not have port on it - cm.targets[foundAt] = target + cm.targets[address] = target } } @@ -146,8 +145,7 @@ func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) // Tell the query loop we've finished this attempt cm.queryChan <- &ConnectionMakerInteraction{ Interaction: Interaction{code: conncode}, - name: UnknownPeerName, - foundAt: address} + address: address} } func tryAfter(interval time.Duration) (time.Time, time.Duration) { diff --git a/peer.go b/peer.go index b818a7b5..9eef00dd 100644 --- a/peer.go +++ b/peer.go @@ -110,10 +110,11 @@ func (peer *Peer) SetVersionAndConnections(version uint64, connections map[PeerN peer.version = version peer.connections = connections for remoteName, conn := range connections { - peer.Router.ConnectionMaker.EnsureConnection(remoteName, conn.RemoteTCPAddr()) + if remoteName != peer.Router.Ourself.Name { + peer.Router.ConnectionMaker.EnsureConnection(conn.RemoteTCPAddr()) + } } } - func (peer *Peer) Forward(dstPeer *Peer, df bool, frame []byte, dec *EthernetDecoder) error { return peer.Relay(peer, dstPeer, df, frame, dec) } diff --git a/types.go b/types.go index d7ce9020..c13ab96c 100644 --- a/types.go +++ b/types.go @@ -155,8 +155,8 @@ type Topology struct { type ConnectionMakerInteraction struct { Interaction - foundAt string - name PeerName + acceptAnyPeer bool + address string } type ConnectionMaker struct { @@ -167,7 +167,7 @@ type ConnectionMaker struct { // Information about an address where we may find a peer type Target struct { - commandLine 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? conn *Connection tryAfter time.Time // next time to try this address diff --git a/weaver/main.go b/weaver/main.go index 96dbaf21..1b2c2361 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -124,7 +124,7 @@ func main() { router := weave.NewRouter(iface, ourName, []byte(password), connLimit, bufSz*1024*1024, logFrame) router.Start() for _, peer := range peers { - router.ConnectionMaker.EnsureConnection(weave.UnknownPeerName, peer) + router.ConnectionMaker.InitiateConnection(peer) } go handleHttp(router) handleSignals(router)