Skip to content

Commit

Permalink
Guarantee minimum when selecting peers to broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Jan 22, 2020
1 parent c4785f6 commit e3b19de
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Libplanet/Net/Protocols/RoutingTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace Libplanet.Net.Protocols
{
internal class RoutingTable
{
// FIXME: This would be configurable.
private const int _minPeersToBroadcast = 10;

private readonly Address _address;
private readonly int _tableSize;
private readonly int _bucketSize;
Expand Down Expand Up @@ -80,9 +83,18 @@ private IEnumerable<KBucket> NonEmptyBuckets

public IEnumerable<BoundPeer> PeersToBroadcast(Address? except)
{
return NonEmptyBuckets
var peers = NonEmptyBuckets
.Select(bucket => bucket.GetRandomPeer(except))
.Where(peer => !(peer is null));
.Where(peer => !(peer is null)).ToList();
var count = peers.Count;
if (count < _minPeersToBroadcast)
{
peers.AddRange(Peers
.Where(peer => except is null || !peer.Address.Equals(except.Value))
.Take(_minPeersToBroadcast - count));
}

return peers;
}

public IEnumerable<BoundPeer> PeersToRefresh(TimeSpan maxAge)
Expand Down

0 comments on commit e3b19de

Please sign in to comment.