commit
fe4d8b7767
5 files changed
+28
-22
No files matched your search
+19
-15
@@ -18,7 +18,7 @@ type ConnectionMaker struct {
|
|||||||
ourself *LocalPeer
|
ourself *LocalPeer
|
||||||
peers *Peers
|
peers *Peers
|
||||||
targets map[string]*Target
|
targets map[string]*Target
|
||||||
cmdLineAddress map[string]bool
|
cmdLineAddress map[string]struct{}
|
||||||
actionChan chan<- ConnectionMakerAction
|
actionChan chan<- ConnectionMakerAction
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ func NewConnectionMaker(ourself *LocalPeer, peers *Peers) *ConnectionMaker {
|
|||||||
return &ConnectionMaker{
|
return &ConnectionMaker{
|
||||||
ourself: ourself,
|
ourself: ourself,
|
||||||
peers: peers,
|
peers: peers,
|
||||||
cmdLineAddress: make(map[string]bool),
|
cmdLineAddress: make(map[string]struct{}),
|
||||||
targets: make(map[string]*Target)}
|
targets: make(map[string]*Target)}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ func (cm *ConnectionMaker) Start() {
|
|||||||
|
|
||||||
func (cm *ConnectionMaker) InitiateConnection(address string) {
|
func (cm *ConnectionMaker) InitiateConnection(address string) {
|
||||||
cm.actionChan <- func() bool {
|
cm.actionChan <- func() bool {
|
||||||
cm.cmdLineAddress[NormalisePeerAddr(address)] = true
|
cm.cmdLineAddress[NormalisePeerAddr(address)] = void
|
||||||
if target, found := cm.targets[address]; found {
|
if target, found := cm.targets[address]; found {
|
||||||
target.tryAfter, target.tryInterval = tryImmediately()
|
target.tryAfter, target.tryInterval = tryImmediately()
|
||||||
}
|
}
|
||||||
@@ -111,19 +111,19 @@ func (cm *ConnectionMaker) queryLoop(actionChan <-chan ConnectionMakerAction) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
|
func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
|
||||||
validTarget := make(map[string]bool)
|
validTarget := make(map[string]struct{})
|
||||||
|
|
||||||
// copy the set of things we are connected to, so we can access them without locking
|
// copy the set of things we are connected to, so we can access them without locking
|
||||||
ourConnectedPeers := make(map[PeerName]bool)
|
ourConnectedPeers := make(PeerNameSet)
|
||||||
ourConnectedTargets := make(map[string]bool)
|
ourConnectedTargets := make(map[string]struct{})
|
||||||
for _, conn := range cm.ourself.Connections() {
|
for _, conn := range cm.ourself.Connections() {
|
||||||
ourConnectedPeers[conn.Remote().Name] = true
|
ourConnectedPeers[conn.Remote().Name] = void
|
||||||
ourConnectedTargets[conn.RemoteTCPAddr()] = true
|
ourConnectedTargets[conn.RemoteTCPAddr()] = void
|
||||||
}
|
}
|
||||||
|
|
||||||
addTarget := func(address string) {
|
addTarget := func(address string) {
|
||||||
if !ourConnectedTargets[address] {
|
if _, connected := ourConnectedTargets[address]; !connected {
|
||||||
validTarget[address] = true
|
validTarget[address] = void
|
||||||
cm.addTarget(address)
|
cm.addTarget(address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,10 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
|
|||||||
cm.peers.ForEach(func(peer *Peer) {
|
cm.peers.ForEach(func(peer *Peer) {
|
||||||
for _, conn := range peer.Connections() {
|
for _, conn := range peer.Connections() {
|
||||||
otherPeer := conn.Remote().Name
|
otherPeer := conn.Remote().Name
|
||||||
if otherPeer == cm.ourself.Name || ourConnectedPeers[otherPeer] {
|
if otherPeer == cm.ourself.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, connected := ourConnectedPeers[otherPeer]; connected {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
address := conn.RemoteTCPAddr()
|
address := conn.RemoteTCPAddr()
|
||||||
@@ -163,25 +166,26 @@ func (cm *ConnectionMaker) addTarget(address string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) connectToTargets(validTarget map[string]bool, ourConnectedTargets map[string]bool) time.Duration {
|
func (cm *ConnectionMaker) connectToTargets(validTarget map[string]struct{}, ourConnectedTargets map[string]struct{}) time.Duration {
|
||||||
now := time.Now() // make sure we catch items just added
|
now := time.Now() // make sure we catch items just added
|
||||||
after := MaxDuration
|
after := MaxDuration
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
if ourConnectedTargets[address] {
|
if _, connected := ourConnectedTargets[address]; connected {
|
||||||
delete(cm.targets, address)
|
delete(cm.targets, address)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if target.attempting {
|
if target.attempting {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !validTarget[address] {
|
if _, valid := validTarget[address]; !valid {
|
||||||
delete(cm.targets, address)
|
delete(cm.targets, address)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch duration := target.tryAfter.Sub(now); {
|
switch duration := target.tryAfter.Sub(now); {
|
||||||
case duration <= 0:
|
case duration <= 0:
|
||||||
target.attempting = true
|
target.attempting = true
|
||||||
go cm.attemptConnection(address, cm.cmdLineAddress[address])
|
_, isCmdLineAddress := cm.cmdLineAddress[address]
|
||||||
|
go cm.attemptConnection(address, isCmdLineAddress)
|
||||||
case duration < after:
|
case duration < after:
|
||||||
after = duration
|
after = duration
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,9 +48,9 @@ func newMockConnection(from, to *Peer) Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkEqualConns(t *testing.T, ourName PeerName, got, wanted map[PeerName]Connection) {
|
func checkEqualConns(t *testing.T, ourName PeerName, got, wanted map[PeerName]Connection) {
|
||||||
checkConns := make(map[PeerName]bool)
|
checkConns := make(PeerNameSet)
|
||||||
for _, conn := range wanted {
|
for _, conn := range wanted {
|
||||||
checkConns[conn.Remote().Name] = true
|
checkConns[conn.Remote().Name] = void
|
||||||
}
|
}
|
||||||
for _, conn := range got {
|
for _, conn := range got {
|
||||||
remoteName := conn.Remote().Name
|
remoteName := conn.Remote().Name
|
||||||
|
|||||||
+3
-1
@@ -23,6 +23,8 @@ type NameCollisionError struct {
|
|||||||
Name PeerName
|
Name PeerName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PeerNameSet map[PeerName]struct{}
|
||||||
|
|
||||||
func NewPeers(ourself *Peer, onGC func(*Peer)) *Peers {
|
func NewPeers(ourself *Peer, onGC func(*Peer)) *Peers {
|
||||||
return &Peers{
|
return &Peers{
|
||||||
ourself: ourself,
|
ourself: ourself,
|
||||||
@@ -169,7 +171,7 @@ func (peers *Peers) garbageCollect() []*Peer {
|
|||||||
func setFromPeersMap(peers map[PeerName]*Peer) PeerNameSet {
|
func setFromPeersMap(peers map[PeerName]*Peer) PeerNameSet {
|
||||||
names := make(PeerNameSet)
|
names := make(PeerNameSet)
|
||||||
for name := range peers {
|
for name := range peers {
|
||||||
names[name] = true
|
names[name] = void
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -350,8 +350,6 @@ func handleSpecialFrame(relayConn *LocalConnection, sender *net.UDPAddr, frame [
|
|||||||
|
|
||||||
// Gossiper methods - the Router is the topology Gossiper
|
// Gossiper methods - the Router is the topology Gossiper
|
||||||
|
|
||||||
type PeerNameSet map[PeerName]bool
|
|
||||||
|
|
||||||
type TopologyGossipData struct {
|
type TopologyGossipData struct {
|
||||||
peers *Peers
|
peers *Peers
|
||||||
update PeerNameSet
|
update PeerNameSet
|
||||||
@@ -360,14 +358,14 @@ type TopologyGossipData struct {
|
|||||||
func NewTopologyGossipData(peers *Peers, update ...*Peer) *TopologyGossipData {
|
func NewTopologyGossipData(peers *Peers, update ...*Peer) *TopologyGossipData {
|
||||||
names := make(PeerNameSet)
|
names := make(PeerNameSet)
|
||||||
for _, p := range update {
|
for _, p := range update {
|
||||||
names[p.Name] = true
|
names[p.Name] = void
|
||||||
}
|
}
|
||||||
return &TopologyGossipData{peers: peers, update: names}
|
return &TopologyGossipData{peers: peers, update: names}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *TopologyGossipData) Merge(other GossipData) {
|
func (d *TopologyGossipData) Merge(other GossipData) {
|
||||||
for name := range other.(*TopologyGossipData).update {
|
for name := range other.(*TopologyGossipData).update {
|
||||||
d.update[name] = true
|
d.update[name] = void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var void = struct{}{}
|
||||||
|
|
||||||
func checkFatal(e error) {
|
func checkFatal(e error) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
log.Fatal(e)
|
log.Fatal(e)
|
||||||
|
|||||||
Reference in new issue
Block a user