New implementation of ConnectionMaker just periodically scanning who we need to connect to
This commit is contained in:
1 parent
42ce45dd55
commit
84aeaf340a
4 files changed
+98
-61
No files matched your search
@@ -221,7 +221,6 @@ func (conn *LocalConnection) handleSetEstablished() error {
|
|||||||
conn.established = true
|
conn.established = true
|
||||||
conn.Unlock()
|
conn.Unlock()
|
||||||
if !old {
|
if !old {
|
||||||
conn.Router.ConnectionMaker.ConnectionEstablished(conn.remoteTCPAddr)
|
|
||||||
conn.local.ConnectionEstablished(conn)
|
conn.local.ConnectionEstablished(conn)
|
||||||
if err := conn.ensureHeartbeat(false); err != nil {
|
if err := conn.ensureHeartbeat(false); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
+76
-53
@@ -12,7 +12,7 @@ const (
|
|||||||
InitialInterval = 5 * time.Second
|
InitialInterval = 5 * time.Second
|
||||||
MaxInterval = 10 * time.Minute
|
MaxInterval = 10 * time.Minute
|
||||||
MaxAttemptCount = 100
|
MaxAttemptCount = 100
|
||||||
CMEnsure = iota
|
CMInitiate = iota
|
||||||
CMStatus = iota
|
CMStatus = iota
|
||||||
CMEstablished = iota
|
CMEstablished = iota
|
||||||
CMConnFailed = iota
|
CMConnFailed = iota
|
||||||
@@ -36,24 +36,13 @@ func StartConnectionMaker(router *Router) *ConnectionMaker {
|
|||||||
|
|
||||||
func (cm *ConnectionMaker) InitiateConnection(address string) {
|
func (cm *ConnectionMaker) InitiateConnection(address string) {
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
cm.queryChan <- &ConnectionMakerInteraction{
|
||||||
Interaction: Interaction{code: CMEnsure},
|
Interaction: Interaction{code: CMInitiate},
|
||||||
acceptAnyPeer: true,
|
isCmdLine: true,
|
||||||
address: address}
|
address: address}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) EnsureConnection(address string) {
|
func (cm *ConnectionMaker) EnsureConnection(address string) {
|
||||||
address = StripPortFromAddr(address)
|
// Do nothing - we will figure out what to do later
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
|
||||||
Interaction: Interaction{code: CMEnsure},
|
|
||||||
acceptAnyPeer: false,
|
|
||||||
address: address}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cm *ConnectionMaker) ConnectionEstablished(address string) {
|
|
||||||
address = StripPortFromAddr(address)
|
|
||||||
cm.queryChan <- &ConnectionMakerInteraction{
|
|
||||||
Interaction: Interaction{code: CMEstablished},
|
|
||||||
address: address}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) String() string {
|
func (cm *ConnectionMaker) String() string {
|
||||||
@@ -84,65 +73,99 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case query.code == CMEnsure:
|
case query.code == CMInitiate:
|
||||||
cm.addToTargets(query.acceptAnyPeer, query.address)
|
cm.addToTargets(query.isCmdLine, query.address)
|
||||||
cm.resetTarget(query.address)
|
|
||||||
tickNow()
|
tickNow()
|
||||||
case query.code == CMStatus:
|
case query.code == CMStatus:
|
||||||
query.resultChan <- cm.status()
|
query.resultChan <- cm.status()
|
||||||
case query.code == CMEstablished:
|
|
||||||
target := cm.targets[query.address]
|
|
||||||
if target != nil {
|
|
||||||
target.state = CSEstablished
|
|
||||||
} else {
|
|
||||||
log.Fatal("Connection established to unknown address", query.address)
|
|
||||||
}
|
|
||||||
case query.code == CMConnFailed:
|
case query.code == CMConnFailed:
|
||||||
target := cm.targets[query.address]
|
target := cm.targets[query.address]
|
||||||
target.state = CSUnconnected
|
if target != nil {
|
||||||
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
target.state = CSUnconnected
|
||||||
maybeTick()
|
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
||||||
|
maybeTick()
|
||||||
|
} else {
|
||||||
|
log.Fatal("CMConnFailed unknown address", query.address)
|
||||||
|
}
|
||||||
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 {
|
cm.checkStateAndAttemptConnections(now)
|
||||||
if target.state == CSUnconnected && now.After(target.tryAfter) {
|
|
||||||
if _, found := cm.router.Ourself.ConnectionOn(address); found {
|
|
||||||
target.state = CSEstablished
|
|
||||||
} else {
|
|
||||||
target.attemptCount += 1
|
|
||||||
target.state = CSAttempting
|
|
||||||
go cm.attemptConnection(address, target.acceptAnyPeer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tick = nil
|
tick = nil
|
||||||
maybeTick()
|
maybeTick()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
|
func (cm *ConnectionMaker) checkStateAndAttemptConnections(now time.Time) {
|
||||||
target := cm.targets[address]
|
ourself := cm.router.Ourself
|
||||||
if target == nil {
|
count_unconnected_commandline := 0
|
||||||
target = &Target{
|
// Any targets that are now connected, we don't need to attempt any more
|
||||||
state: CSUnconnected,
|
for address, target := range cm.targets {
|
||||||
acceptAnyPeer: acceptAnyPeer,
|
if _, found := ourself.ConnectionOn(address); found {
|
||||||
|
if target.isCmdLine {
|
||||||
|
// We need to keep hold of targets supplied on the
|
||||||
|
// command-line, because that info isn't
|
||||||
|
// tracked anywhere else
|
||||||
|
target.state = CSEstablished
|
||||||
|
target.tryInterval = InitialInterval
|
||||||
|
} else {
|
||||||
|
delete(cm.targets, address)
|
||||||
|
}
|
||||||
|
} else if target.isCmdLine {
|
||||||
|
if target.state != CSAttempting {
|
||||||
|
target.state = CSUnconnected
|
||||||
|
}
|
||||||
|
count_unconnected_commandline += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for peers that we don't have a connection to.
|
||||||
|
count_unconnected_peers := 0
|
||||||
|
cm.router.Peers.ForEach(func(name PeerName, peer *Peer) {
|
||||||
|
for peer2, conn := range peer.connections {
|
||||||
|
if _, found := ourself.ConnectionTo(peer2); !found &&
|
||||||
|
peer2 != ourself.Name {
|
||||||
|
log.Println("Unconnected peer:", peer2)
|
||||||
|
count_unconnected_peers += 1
|
||||||
|
// peer2 is a peer that someone else knows about, but we don't have a connection to.
|
||||||
|
address := conn.RemoteTCPAddr()
|
||||||
|
if host, port, err := ExtractHostPort(address); err == nil {
|
||||||
|
if port != Port {
|
||||||
|
cm.addToTargets(false, address)
|
||||||
|
}
|
||||||
|
cm.addToTargets(false, host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if count_unconnected_commandline == 0 && count_unconnected_peers == 0 {
|
||||||
|
// We are already connected to everyone we could possibly be connected to; nothing further to do.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now connect to any targets in scope
|
||||||
|
for address, target := range cm.targets {
|
||||||
|
if target.state == CSUnconnected && now.After(target.tryAfter) {
|
||||||
|
target.attemptCount += 1
|
||||||
|
target.state = CSAttempting
|
||||||
|
go cm.attemptConnection(address, target.isCmdLine)
|
||||||
}
|
}
|
||||||
cm.targets[address] = target
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) resetTarget(address string) {
|
func (cm *ConnectionMaker) addToTargets(isCmdLine bool, address string) {
|
||||||
target := cm.targets[address]
|
target := cm.targets[address]
|
||||||
// If we were in the middle of a connection attempt then back-off again
|
if target == nil {
|
||||||
if target.state == CSAttempting {
|
target = &Target{
|
||||||
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
state: CSUnconnected,
|
||||||
} else {
|
isCmdLine: isCmdLine,
|
||||||
|
}
|
||||||
target.tryAfter, target.tryInterval = tryImmediately()
|
target.tryAfter, target.tryInterval = tryImmediately()
|
||||||
|
cm.targets[address] = target
|
||||||
}
|
}
|
||||||
target.state = CSUnconnected
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) status() string {
|
func (cm *ConnectionMaker) status() string {
|
||||||
|
|||||||
@@ -155,8 +155,8 @@ type Topology struct {
|
|||||||
|
|
||||||
type ConnectionMakerInteraction struct {
|
type ConnectionMakerInteraction struct {
|
||||||
Interaction
|
Interaction
|
||||||
acceptAnyPeer bool
|
isCmdLine bool
|
||||||
address string
|
address string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionMaker struct {
|
type ConnectionMaker struct {
|
||||||
@@ -169,11 +169,11 @@ 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?
|
isCmdLine bool // was this address given to us on the command line?
|
||||||
state ConnectionState
|
state ConnectionState
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// UDPSender interface and implementations
|
// UDPSender interface and implementations
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkFatal(e error) {
|
func checkFatal(e error) {
|
||||||
@@ -106,6 +107,20 @@ func StripPortFromAddr(address string) string {
|
|||||||
return address
|
return address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// given an address like '1.2.3.4:567', return the host and port number,
|
||||||
|
// given an address like '1.2.3.4', return the host and weave's standard port number
|
||||||
|
func ExtractHostPort(address string) (host string, port int, err error) {
|
||||||
|
if host, port, err := net.SplitHostPort(address); err == nil {
|
||||||
|
if portnum, err := strconv.Atoi(port); err == nil {
|
||||||
|
return host, portnum, nil
|
||||||
|
} else {
|
||||||
|
return host, 0, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return host, Port, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// given an address like '1.2.3.4:567', return the address if it has a port,
|
// given an address like '1.2.3.4:567', return the address if it has a port,
|
||||||
// otherwise return the address with weave's standard port number
|
// otherwise return the address with weave's standard port number
|
||||||
func NormalisePeerAddr(peerAddr string) string {
|
func NormalisePeerAddr(peerAddr string) string {
|
||||||
|
|||||||
Reference in new issue
Block a user