Check if we already have a connection on an address before attempting to connect to it
This commit is contained in:
1 parent
6809a43d49
commit
2f53df8a97
2 files changed
+31
-10
No files matched your search
+7
-3
@@ -115,9 +115,13 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
case now := <-tick:
|
case now := <-tick:
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
if target.state == CMUnconnected && now.After(target.tryAfter) {
|
if target.state == CMUnconnected && now.After(target.tryAfter) {
|
||||||
target.attemptCount += 1
|
if _, found := cm.router.Ourself.ConnectionOn(address); found {
|
||||||
target.state = CMAttempting
|
target.state = CMEstablished
|
||||||
go cm.attemptConnection(address, target.acceptAnyPeer)
|
} else {
|
||||||
|
target.attemptCount += 1
|
||||||
|
target.state = CMAttempting
|
||||||
|
go cm.attemptConnection(address, target.acceptAnyPeer)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tick = nil
|
tick = nil
|
||||||
|
|||||||
@@ -57,6 +57,29 @@ func (peer *Peer) ConnectionTo(name PeerName) (Connection, bool) {
|
|||||||
return conn, found // yes, you really can't inline that. FFS.
|
return conn, found // yes, you really can't inline that. FFS.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
func normalisePeerAddr(peerAddr string) string {
|
||||||
|
_, _, err := net.SplitHostPort(peerAddr)
|
||||||
|
if err == nil {
|
||||||
|
return peerAddr
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s:%d", peerAddr, Port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (peer *Peer) ConnectionOn(peerAddr string) (Connection, bool) {
|
||||||
|
peerAddr = normalisePeerAddr(peerAddr)
|
||||||
|
peer.RLock()
|
||||||
|
defer peer.RUnlock()
|
||||||
|
for _, conn := range peer.connections {
|
||||||
|
if conn.RemoteTCPAddr() == peerAddr {
|
||||||
|
return conn, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
func (peer *Peer) ForEachConnection(fun func(PeerName, Connection)) {
|
func (peer *Peer) ForEachConnection(fun func(PeerName, Connection)) {
|
||||||
peer.RLock()
|
peer.RLock()
|
||||||
defer peer.RUnlock()
|
defer peer.RUnlock()
|
||||||
@@ -197,13 +220,7 @@ func (peer *Peer) CreateConnection(peerAddr string, acceptNewPeer bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// We're dialing the remote so that means connections will come from random ports
|
// We're dialing the remote so that means connections will come from random ports
|
||||||
_, _, err := net.SplitHostPort(peerAddr)
|
addrStr := normalisePeerAddr(peerAddr)
|
||||||
var addrStr string
|
|
||||||
if err == nil {
|
|
||||||
addrStr = peerAddr
|
|
||||||
} else {
|
|
||||||
addrStr = fmt.Sprintf("%s:%d", peerAddr, Port)
|
|
||||||
}
|
|
||||||
tcpAddr, tcpErr := net.ResolveTCPAddr("tcp4", addrStr)
|
tcpAddr, tcpErr := net.ResolveTCPAddr("tcp4", addrStr)
|
||||||
udpAddr, udpErr := net.ResolveUDPAddr("udp4", addrStr)
|
udpAddr, udpErr := net.ResolveUDPAddr("udp4", addrStr)
|
||||||
if tcpErr != nil || udpErr != nil {
|
if tcpErr != nil || udpErr != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user