-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf/smoother peer discovery (#5846)
* Continuously connect * Added another delay due to disconnect * Increase useless peer timeout * Simple rate limiter * Integrate with peer manager * Adjust some stats * Check all peer, and uses pending variable * Make logic clearer * Minor cleanup * Missed cancellation token * Cancel setup outgoing peer connection if throttled * Lockless ratelimiter * Addressing comment * Minor adjustments * Whitespace * Having trouble getting candidate at higher speed * Make test more consistent * Even more lenient
- Loading branch information
Showing
7 changed files
with
234 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Core.Test; | ||
|
||
[Parallelizable(ParallelScope.Self)] | ||
public class RateLimiterTests | ||
{ | ||
[TestCase(100, 1, 1000)] | ||
[TestCase(100, 1, 100)] | ||
[TestCase(1000, 1, 100)] | ||
[TestCase(100, 4, 1000)] | ||
[TestCase(100, 4, 100)] | ||
[TestCase(1000, 4, 100)] | ||
public async Task RateLimiter_should_delay_wait_to_rate_limit(int eventPerSec, int concurrency, int durationMs) | ||
{ | ||
RateLimiter rateLimiter = new(eventPerSec); | ||
|
||
TimeSpan duration = TimeSpan.FromMilliseconds(durationMs); | ||
DateTimeOffset startTime = DateTimeOffset.Now; | ||
DateTimeOffset deadline = startTime + duration; | ||
long counter = 0; | ||
|
||
Task[] tasks = Enumerable.Range(0, concurrency).Select(async (_) => | ||
{ | ||
while (DateTimeOffset.Now < deadline) | ||
{ | ||
Interlocked.Increment(ref counter); | ||
await rateLimiter.WaitAsync(CancellationToken.None); | ||
} | ||
}).ToArray(); | ||
|
||
Task.WaitAll(tasks); | ||
|
||
int effectivePerSec = (int)(counter / (DateTimeOffset.Now - startTime).TotalSeconds); | ||
effectivePerSec.Should().BeInRange((int)(eventPerSec * 0.5), (int)(eventPerSec * 1.1)); | ||
} | ||
|
||
[Test] | ||
public async Task RateLimiter_should_throw_when_cancelled() | ||
{ | ||
RateLimiter rateLimiter = new(1); | ||
await rateLimiter.WaitAsync(CancellationToken.None); | ||
CancellationTokenSource cts = new(); | ||
ValueTask waitTask = rateLimiter.WaitAsync(cts.Token); | ||
cts.Cancel(); | ||
|
||
Func<Task> act = async () => await waitTask; | ||
await act.Should().ThrowAsync<OperationCanceledException>(); | ||
} | ||
|
||
[Test] | ||
public async Task RateLimiter_should_return_true_on_is_throttled_if_throttled() | ||
{ | ||
RateLimiter rateLimiter = new(1); | ||
await rateLimiter.WaitAsync(CancellationToken.None); | ||
rateLimiter.IsThrottled().Should().BeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Nethermind.Core.Extensions; | ||
|
||
namespace Nethermind.Core; | ||
|
||
/// <summary> | ||
/// Simple rate limiter that limits rate of event, by delaying the caller so that a minimum amount of time elapsed | ||
/// between event. | ||
/// </summary> | ||
public class RateLimiter | ||
{ | ||
private readonly long _delay; | ||
private long _nextSlot; | ||
|
||
public RateLimiter(int eventPerSec) : this(1.0 / eventPerSec) | ||
{ | ||
} | ||
|
||
private RateLimiter(double intervalSec) | ||
{ | ||
_delay = (long)(Stopwatch.Frequency * intervalSec); | ||
|
||
_nextSlot = GetCurrentTick(); | ||
} | ||
|
||
public static long GetCurrentTick() | ||
{ | ||
return Stopwatch.GetTimestamp(); | ||
} | ||
|
||
private static double TickToMs(long tick) | ||
{ | ||
return tick * 1000.0 / Stopwatch.Frequency; | ||
} | ||
|
||
/// <summary> | ||
/// Return true if its definitely will be throttled when calling WaitAsync. May still get throttled even if this | ||
/// return false. | ||
/// </summary> | ||
/// <returns></returns> | ||
public bool IsThrottled() | ||
{ | ||
return GetCurrentTick() < _nextSlot; | ||
} | ||
|
||
public async ValueTask WaitAsync(CancellationToken ctx) | ||
{ | ||
while (true) | ||
{ | ||
long originalNextSlot = _nextSlot; | ||
|
||
// Technically its possible that two `GetCurrentTick()` call at the same time can return same value, | ||
// but its very unlikely. | ||
long now = GetCurrentTick(); | ||
if (now >= originalNextSlot | ||
&& Interlocked.CompareExchange(ref _nextSlot, now + _delay, originalNextSlot) == originalNextSlot) | ||
{ | ||
return; | ||
} | ||
|
||
long toWait = originalNextSlot - now; | ||
if (toWait < 0) continue; | ||
|
||
await Task.Delay(TimeSpan.FromMilliseconds(TickToMs(toWait)), ctx); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.