refactor: change Routes.RandomNeighbours return for consistency
PeerNameSet -> []PeerName, which is what the other functions return. This is also easier to deal with at the call site.
This commit is contained in:
1 parent
61e3bf4a20
commit
2058ce3f3a
2 files changed
+15
-13
No files matched your search
@@ -167,20 +167,18 @@ func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) {
|
|||||||
func (c *GossipChannel) Send(srcName PeerName, data GossipData) {
|
func (c *GossipChannel) Send(srcName PeerName, data GossipData) {
|
||||||
// do this outside the lock below so we avoid lock nesting
|
// do this outside the lock below so we avoid lock nesting
|
||||||
c.routes.EnsureRecalculated()
|
c.routes.EnsureRecalculated()
|
||||||
selectedConnections := make(ConnectionSet)
|
destinations := c.routes.RandomNeighbours(srcName)
|
||||||
for name := range c.routes.RandomNeighbours(srcName) {
|
if len(destinations) == 0 {
|
||||||
if conn, found := c.ourself.ConnectionTo(name); found {
|
return
|
||||||
selectedConnections[conn] = void
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
c.sendDown(selectedConnections, data)
|
c.sendDown(c.ourself.ConnectionsTo(destinations), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GossipChannel) SendDown(conn Connection, data GossipData) {
|
func (c *GossipChannel) SendDown(conn Connection, data GossipData) {
|
||||||
c.sendDown(ConnectionSet{conn: void}, data)
|
c.sendDown([]Connection{conn}, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GossipChannel) sendDown(selectedConnections ConnectionSet, data GossipData) {
|
func (c *GossipChannel) sendDown(selectedConnections []Connection, data GossipData) {
|
||||||
if len(selectedConnections) == 0 {
|
if len(selectedConnections) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -207,7 +205,7 @@ func (c *GossipChannel) sendDown(selectedConnections ConnectionSet, data GossipD
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// start senders, if necessary, and send.
|
// start senders, if necessary, and send.
|
||||||
for conn := range selectedConnections {
|
for _, conn := range selectedConnections {
|
||||||
sender, found := c.senders[conn]
|
sender, found := c.senders[conn]
|
||||||
if !found {
|
if !found {
|
||||||
sender = c.makeSender(conn)
|
sender = c.makeSender(conn)
|
||||||
|
|||||||
+8
-4
@@ -92,20 +92,24 @@ func (routes *Routes) BroadcastAll(name PeerName) []PeerName {
|
|||||||
// returns a higher proportion of neighbours than elsewhere. In
|
// returns a higher proportion of neighbours than elsewhere. In
|
||||||
// extremis, on peers with fewer than log2(n_peers) neighbours, all
|
// extremis, on peers with fewer than log2(n_peers) neighbours, all
|
||||||
// neighbours are returned.
|
// neighbours are returned.
|
||||||
func (routes *Routes) RandomNeighbours(except PeerName) PeerNameSet {
|
func (routes *Routes) RandomNeighbours(except PeerName) []PeerName {
|
||||||
res := make(PeerNameSet)
|
destinations := make(PeerNameSet)
|
||||||
routes.RLock()
|
routes.RLock()
|
||||||
defer routes.RUnlock()
|
defer routes.RUnlock()
|
||||||
count := int(math.Log2(float64(len(routes.unicastAll))))
|
count := int(math.Log2(float64(len(routes.unicastAll))))
|
||||||
// depends on go's random map iteration
|
// depends on go's random map iteration
|
||||||
for _, dst := range routes.unicastAll {
|
for _, dst := range routes.unicastAll {
|
||||||
if dst != UnknownPeerName && dst != except {
|
if dst != UnknownPeerName && dst != except {
|
||||||
res[dst] = void
|
destinations[dst] = void
|
||||||
if len(res) >= count {
|
if len(destinations) >= count {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
res := make([]PeerName, 0, len(destinations))
|
||||||
|
for dst := range destinations {
|
||||||
|
res = append(res, dst)
|
||||||
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user