Remove all references to PeerName in ConnectionMaker
This commit is contained in:
1 parent
8b3e97b96a
commit
dec794eb72
5 files changed
+33
-34
No files matched your search
+1
-1
@@ -279,7 +279,7 @@ func (conn *LocalConnection) handleShutdown() {
|
|||||||
conn.stopForwarders()
|
conn.stopForwarders()
|
||||||
|
|
||||||
if conn.remote != nil {
|
if conn.remote != nil {
|
||||||
conn.Router.ConnectionMaker.EnsureConnection(conn.remote.Name, conn.remoteTCPAddr)
|
conn.Router.ConnectionMaker.EnsureConnection(conn.remoteTCPAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
-27
@@ -1,13 +1,5 @@
|
|||||||
package weave
|
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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -38,11 +30,18 @@ func StartConnectionMaker(router *Router) *ConnectionMaker {
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) EnsureConnection(name PeerName, foundAt string) {
|
func (cm *ConnectionMaker) InitiateConnection(address string) {
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
cm.queryChan <- &ConnectionMakerInteraction{
|
||||||
Interaction: Interaction{code: CMEnsure},
|
Interaction: Interaction{code: CMEnsure},
|
||||||
name: name,
|
acceptAnyPeer: true,
|
||||||
foundAt: foundAt}
|
address: address}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cm *ConnectionMaker) EnsureConnection(address string) {
|
||||||
|
cm.queryChan <- &ConnectionMakerInteraction{
|
||||||
|
Interaction: Interaction{code: CMEnsure},
|
||||||
|
acceptAnyPeer: false,
|
||||||
|
address: address}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) String() string {
|
func (cm *ConnectionMaker) String() string {
|
||||||
@@ -69,16 +68,14 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case query.code == CMEnsure:
|
case query.code == CMEnsure:
|
||||||
if query.name != cm.router.Ourself.Name {
|
cm.addToTargets(query.acceptAnyPeer, query.address)
|
||||||
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:
|
||||||
cm.targets[query.foundAt].attempting = false
|
cm.targets[query.address].attempting = false
|
||||||
case query.code == CMConnFailed:
|
case query.code == CMConnFailed:
|
||||||
target := cm.targets[query.foundAt]
|
target := cm.targets[query.address]
|
||||||
target.attempting = false
|
target.attempting = false
|
||||||
after, interval := tryAfter(target.tryInterval)
|
after, interval := tryAfter(target.tryInterval)
|
||||||
target.tryInterval = interval
|
target.tryInterval = interval
|
||||||
@@ -92,7 +89,7 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
if target.conn == nil && !target.attempting && now.After(target.tryAfter) {
|
if target.conn == nil && !target.attempting && now.After(target.tryAfter) {
|
||||||
target.attemptCount += 1
|
target.attemptCount += 1
|
||||||
target.attempting = true;
|
target.attempting = true;
|
||||||
go cm.attemptConnection(address, target.commandLine)
|
go cm.attemptConnection(address, target.acceptAnyPeer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tick = nil
|
tick = nil
|
||||||
@@ -101,24 +98,26 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) addToTargets(name PeerName, foundAt string) {
|
func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
|
||||||
target := cm.targets[foundAt]
|
target := cm.targets[address]
|
||||||
if target == nil {
|
if target == nil {
|
||||||
after, interval := tryAfter(InitialInterval)
|
after, interval := tryAfter(InitialInterval)
|
||||||
target = &Target{
|
target = &Target{
|
||||||
|
acceptAnyPeer: acceptAnyPeer,
|
||||||
tryInterval: interval,
|
tryInterval: interval,
|
||||||
tryAfter: after}
|
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 {
|
if err == nil {
|
||||||
// ensure port-less version is there
|
// ensure port-less version is there
|
||||||
cm.targets[foundAtHost] = target
|
cm.targets[addrHost] = target
|
||||||
if foundAtPort, err := strconv.Atoi(foundAtPortStr); err == nil && foundAtPort != Port {
|
if addrPort, err := strconv.Atoi(addrPortStr); err == nil && addrPort != Port {
|
||||||
cm.targets[foundAt] = target
|
cm.targets[address] = 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
|
||||||
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
|
// Tell the query loop we've finished this attempt
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
cm.queryChan <- &ConnectionMakerInteraction{
|
||||||
Interaction: Interaction{code: conncode},
|
Interaction: Interaction{code: conncode},
|
||||||
name: UnknownPeerName,
|
address: address}
|
||||||
foundAt: address}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func tryAfter(interval time.Duration) (time.Time, time.Duration) {
|
func tryAfter(interval time.Duration) (time.Time, time.Duration) {
|
||||||
|
|||||||
@@ -110,10 +110,11 @@ func (peer *Peer) SetVersionAndConnections(version uint64, connections map[PeerN
|
|||||||
peer.version = version
|
peer.version = version
|
||||||
peer.connections = connections
|
peer.connections = connections
|
||||||
for remoteName, conn := range 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 {
|
func (peer *Peer) Forward(dstPeer *Peer, df bool, frame []byte, dec *EthernetDecoder) error {
|
||||||
return peer.Relay(peer, dstPeer, df, frame, dec)
|
return peer.Relay(peer, dstPeer, df, frame, dec)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,8 +155,8 @@ type Topology struct {
|
|||||||
|
|
||||||
type ConnectionMakerInteraction struct {
|
type ConnectionMakerInteraction struct {
|
||||||
Interaction
|
Interaction
|
||||||
foundAt string
|
acceptAnyPeer bool
|
||||||
name PeerName
|
address string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionMaker struct {
|
type ConnectionMaker struct {
|
||||||
@@ -167,7 +167,7 @@ type ConnectionMaker struct {
|
|||||||
|
|
||||||
// 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 {
|
||||||
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?
|
attempting bool // are we currently attempting to connect to this address?
|
||||||
conn *Connection
|
conn *Connection
|
||||||
tryAfter time.Time // next time to try this address
|
tryAfter time.Time // next time to try this address
|
||||||
|
|||||||
+1
-1
@@ -124,7 +124,7 @@ func main() {
|
|||||||
router := weave.NewRouter(iface, ourName, []byte(password), connLimit, bufSz*1024*1024, logFrame)
|
router := weave.NewRouter(iface, ourName, []byte(password), connLimit, bufSz*1024*1024, logFrame)
|
||||||
router.Start()
|
router.Start()
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
router.ConnectionMaker.EnsureConnection(weave.UnknownPeerName, peer)
|
router.ConnectionMaker.InitiateConnection(peer)
|
||||||
}
|
}
|
||||||
go handleHttp(router)
|
go handleHttp(router)
|
||||||
handleSignals(router)
|
handleSignals(router)
|
||||||
|
|||||||
Reference in new issue
Block a user