Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory/reduce allocations in peer manager #4271

Merged
merged 15 commits into from
Aug 2, 2022
23 changes: 12 additions & 11 deletions src/Nethermind/Nethermind.Network.Test/PeerManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

Expand Down Expand Up @@ -157,7 +157,7 @@ public async Task Will_agree_on_which_session_to_disconnect_when_connecting_at_o
ConnectionDirection firstDirection)
{
await using Context ctx = new();

ctx.PeerPool.Start();
ctx.PeerManager.Start();
Session session1 = new(30303, Substitute.For<IChannel>(), NullDisconnectsAnalyzer.Instance,
Expand All @@ -168,7 +168,7 @@ public async Task Will_agree_on_which_session_to_disconnect_when_connecting_at_o
(firstDirection == ConnectionDirection.In)
? (shouldLose ? TestItem.PublicKeyA : TestItem.PublicKeyC)
: (shouldLose ? TestItem.PublicKeyC : TestItem.PublicKeyA);


if (firstDirection == ConnectionDirection.In)
{
Expand Down Expand Up @@ -317,20 +317,20 @@ public async Task
ctx.PeerManager.Start();

int currentCount = 0;
int count = 35;
for (int i = 0; i < 10; i++)
{
currentCount += 25;
currentCount += count;
await Task.Delay(_travisDelayLong);
ctx.RlpxPeer.ConnectAsyncCallsCount.Should().BeInRange(currentCount, currentCount + 25);
ctx.RlpxPeer.ConnectAsyncCallsCount.Should().BeInRange(currentCount, currentCount + count);
ctx.HandshakeAllSessions();
await Task.Delay(_travisDelay);
ctx.DisconnectAllSessions();
}
}

[Test, Retry(3)]
public async Task
Will_fill_up_over_and_over_again_on_disconnects_and_when_ids_keep_changing_with_max_candidates_40_with_random_incoming_connections()
public async Task Will_fill_up_over_and_over_again_on_disconnects_and_when_ids_keep_changing_with_max_candidates_40_with_random_incoming_connections()
{
await using Context ctx = new();
ctx.NetworkConfig.MaxCandidatePeerCount = 40;
Expand All @@ -340,12 +340,13 @@ public async Task
ctx.PeerPool.Start();
ctx.PeerManager.Start();

int count = 30;
int currentCount = 0;
for (int i = 0; i < 10; i++)
{
currentCount += 25;
currentCount += count;
await Task.Delay(_travisDelayLong);
ctx.RlpxPeer.ConnectAsyncCallsCount.Should().BeInRange(currentCount, currentCount + 25);
ctx.RlpxPeer.ConnectAsyncCallsCount.Should().BeInRange(currentCount, currentCount + count);
ctx.HandshakeAllSessions();
await Task.Delay(_travisDelay);
ctx.CreateIncomingSessions();
Expand Down
20 changes: 10 additions & 10 deletions src/Nethermind/Nethermind.Network/IPeerPool.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
//

using System;
using System.Collections.Concurrent;
Expand All @@ -30,10 +30,10 @@ public interface IPeerPool
{
ConcurrentDictionary<PublicKey, Peer> Peers { get; }
ConcurrentDictionary<PublicKey, Peer> ActivePeers { get; }
List<Peer> StaticPeers { get; }
List<Peer> NonStaticPeers { get; }

IEnumerable<Peer> StaticPeers { get; }
IEnumerable<Peer> NonStaticPeers { get; }

int PeerCount { get; }
int ActivePeerCount { get; }
int StaticPeerCount { get; }
Expand All @@ -43,15 +43,15 @@ public Peer GetOrAdd(NetworkNode networkNode)
Node node = new (networkNode);
return GetOrAdd(node);
}

Peer GetOrAdd(Node node);
bool TryGet(PublicKey id, out Peer peer);
bool TryRemove(PublicKey id, out Peer removed);
Peer Replace(ISession session);

event EventHandler<PeerEventArgs> PeerAdded;
event EventHandler<PeerEventArgs> PeerRemoved;

void Start();
Task StopAsync();
}
Loading