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

Add RateLimitPolicy #903

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
271cfd7
Add core RateLimiter implementations
reisenberger Mar 23, 2019
7d70287
Add example async TResult rate-limiter policy implementation
reisenberger Mar 23, 2019
6aa1e2a
Add example syntax
reisenberger Mar 23, 2019
8354e6d
Make the retryAfterFactory take Timespan as an input parameter!
reisenberger Mar 24, 2019
cba0b51
Initial LockFreeTokenBucketRateLimiterTests
reisenberger Jul 4, 2019
193b356
Tidy BulkheadSpecsHelper
reisenberger Jul 4, 2019
5496e66
Factor out test helpers
reisenberger Jul 4, 2019
4b17e56
Factor out common tests; add tests on lock-based rate limiter
reisenberger Jul 4, 2019
bfc3251
Allow for slow-running on CI servers
reisenberger Jul 4, 2019
8fbb193
Add tests on full bucket capacity
reisenberger Jul 4, 2019
d1c7ec8
Fix RateLimitRejectedException
reisenberger Jul 4, 2019
416c6ff
Remove unused configuration overloads
reisenberger Jul 4, 2019
fcaf1ac
Introduce a factory for obtaining the preferred rate-limiter implemen…
reisenberger Jul 4, 2019
ebd8a56
Pull some test helper methods into a common base-class
reisenberger Jul 4, 2019
c7af7e4
Add first specs on async policy syntax
reisenberger Jul 4, 2019
a2f6566
Add full set of specs on rate-limit policies thus far
reisenberger Jul 5, 2019
c92cb6b
Add tests on retryAfterFactory
reisenberger Jul 5, 2019
1532500
Add tests on context passed to retryAfterFactory
reisenberger Jul 5, 2019
56fea85
Add async non-generic syntax and specs
reisenberger Jul 5, 2019
182c635
Improve code layout
reisenberger Jul 5, 2019
2a1c508
Add sync rate-limit policies
reisenberger Jul 5, 2019
fd609ad
Add initial rate-limit doco; bump to v7.2.0
reisenberger Jul 9, 2019
0121b3d
Improve bulkhead doco in readme
reisenberger Jul 9, 2019
e061d50
Minor expressivity refinements
reisenberger Jul 15, 2019
db47862
Neaten bulkhead tests commentary
reisenberger Jul 16, 2019
daa42cb
Control visibility of IRateLimiter components
reisenberger Jul 16, 2019
c916be5
Fix non-generic rate-limit tests to be genuinely non-generic
reisenberger Jul 16, 2019
3afd688
Remove LockBasedTokenBucketRateLimiter
martincostello Dec 2, 2021
5a0cafc
Throw ArgumentNullException
martincostello Dec 2, 2021
fc7bd8f
Pass value to ArgumentOutOfRangeException
martincostello Dec 2, 2021
fcc8550
Make LockFreeTokenBucketRateLimiter sealed
martincostello Dec 2, 2021
881eedc
Make RateLimiterFactory static
martincostello Dec 2, 2021
f7a1ab0
Add parenthesis
martincostello Dec 2, 2021
423e8ab
Use string not String
martincostello Dec 2, 2021
9f72e8b
Merge branch 'v723-or-v730' into Rate-Limiter-Policy
martincostello Dec 2, 2021
f01b12e
Bump version to 7.3.0
martincostello Dec 2, 2021
c654e0e
Fix typo
martincostello Dec 2, 2021
4ec4d23
Code review fixes
martincostello Dec 2, 2021
8db33b5
Fix links
martincostello Jan 16, 2022
544d554
Fix typos
martincostello Jan 16, 2022
51cbb17
Add rate-limit documentation
martincostello Jan 16, 2022
fb169ba
Update Cake
martincostello Jan 16, 2022
bf0de16
Do not generate benchmarks package
martincostello Jan 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 7.3.0

- Add RateLimit policy - Thanks to [@reisenberger](https://github.com/reisenberger)

## 7.2.2

- Recursively search all `AggregateException` inner exceptions for predicate matches when using `HandleInner()` ([#818](https://github.com/App-vNext/Polly/issues/818)) - Thanks to [@sideproject](https://github.com/sideproject)
Expand Down
2 changes: 1 addition & 1 deletion GitVersionConfig.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
next-version: 7.2.2
next-version: 7.3.0
294 changes: 178 additions & 116 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var configuration = Argument<string>("configuration", "Release");
// EXTERNAL NUGET LIBRARIES
//////////////////////////////////////////////////////////////////////

#addin "Cake.FileHelpers"
#addin nuget:?package=Cake.Yaml
#addin nuget:?package=Cake.FileHelpers&version=3.3.0
#addin nuget:?package=Cake.Yaml&version=3.1.1
#addin nuget:?package=YamlDotNet&version=5.2.1

///////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ if(-Not $SkipToolPackageRestore.IsPresent)
# Install just Cake if missing config
else
{
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install Cake -Version 0.25.0 -ExcludeVersion" # Pin Cake version to 0.25.0; see https://github.com/App-vNext/Polly/issues/416
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install Cake -Version 0.38.5 -ExcludeVersion" # Pin Cake version to 0.38.5; see https://github.com/App-vNext/Polly/issues/416
Write-Verbose ($NuGetOutput | Out-String)
}
Pop-Location
Expand Down
1 change: 1 addition & 0 deletions src/Polly.Benchmarks/Polly.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
Expand Down
40 changes: 40 additions & 0 deletions src/Polly.Specs/Helpers/RateLimit/IRateLimiterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using FluentAssertions;
using Polly.RateLimit;

namespace Polly.Specs.Helpers.RateLimit
{
internal static class IRateLimiterExtensions
{
public static void ShouldPermitAnExecution(this IRateLimiter rateLimiter)
{
(bool permitExecution, TimeSpan retryAfter) canExecute = rateLimiter.PermitExecution();

canExecute.permitExecution.Should().BeTrue();
canExecute.retryAfter.Should().Be(TimeSpan.Zero);
}

public static void ShouldPermitNExecutions(this IRateLimiter rateLimiter, long numberOfExecutions)
{
for (int execution = 0; execution < numberOfExecutions; execution++)
{
rateLimiter.ShouldPermitAnExecution();
}
}

public static void ShouldNotPermitAnExecution(this IRateLimiter rateLimiter, TimeSpan? retryAfter = null)
{
(bool permitExecution, TimeSpan retryAfter) canExecute = rateLimiter.PermitExecution();

canExecute.permitExecution.Should().BeFalse();
if (retryAfter == null)
{
canExecute.retryAfter.Should().BeGreaterThan(TimeSpan.Zero);
}
else
{
canExecute.retryAfter.Should().Be(retryAfter.Value);
}
}
}
}
21 changes: 21 additions & 0 deletions src/Polly.Specs/Helpers/RateLimit/ResultClassWithRetryAfter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Polly.Specs.Helpers.RateLimit
{
internal class ResultClassWithRetryAfter : ResultClass
{
public TimeSpan RetryAfter { get; }

public ResultClassWithRetryAfter(ResultPrimitive result)
: base(result)
{
RetryAfter = TimeSpan.Zero;
}

public ResultClassWithRetryAfter(TimeSpan retryAfter)
: base(ResultPrimitive.Undefined)
{
RetryAfter = retryAfter;
}
}
}
49 changes: 49 additions & 0 deletions src/Polly.Specs/RateLimit/AsyncRateLimitPolicySpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Threading.Tasks;
using Polly.RateLimit;
using Polly.Specs.Helpers;
using Polly.Specs.Helpers.RateLimit;
using Polly.Utilities;
using Xunit;

namespace Polly.Specs.RateLimit
{
[Collection(Polly.Specs.Helpers.Constants.SystemClockDependentTestCollection)]
public class AsyncRateLimitPolicySpecs : RateLimitPolicySpecsBase, IDisposable
{
public void Dispose()
{
SystemClock.Reset();
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan)
{
return Policy.RateLimitAsync(numberOfExecutions, perTimeSpan);
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan, int maxBurst)
{
return Policy.RateLimitAsync(numberOfExecutions, perTimeSpan, maxBurst);
}

protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy policy)
{
if (policy is AsyncRateLimitPolicy typedPolicy)
{
try
{
typedPolicy.ExecuteAsync(() => Task.FromResult(new ResultClassWithRetryAfter(ResultPrimitive.Good))).GetAwaiter().GetResult();
return (true, TimeSpan.Zero);
}
catch (RateLimitRejectedException e)
{
return (false, e.RetryAfter);
}
}
else
{
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}
}
}
67 changes: 67 additions & 0 deletions src/Polly.Specs/RateLimit/AsyncRateLimitPolicyTResultSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Threading.Tasks;
using Polly.RateLimit;
using Polly.Specs.Helpers;
using Polly.Specs.Helpers.RateLimit;
using Polly.Utilities;
using Xunit;

namespace Polly.Specs.RateLimit
{
[Collection(Polly.Specs.Helpers.Constants.SystemClockDependentTestCollection)]
public class AsyncRateLimitPolicyTResultSpecs : RateLimitPolicyTResultSpecsBase, IDisposable
{
public void Dispose()
{
SystemClock.Reset();
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan)
{
return Policy.RateLimitAsync<ResultClassWithRetryAfter>(numberOfExecutions, perTimeSpan);
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan, int maxBurst)
{
return Policy.RateLimitAsync<ResultClassWithRetryAfter>(numberOfExecutions, perTimeSpan, maxBurst);
}

protected override IRateLimitPolicy<TResult> GetPolicyViaSyntax<TResult>(int numberOfExecutions, TimeSpan perTimeSpan, int maxBurst,
Func<TimeSpan, Context, TResult> retryAfterFactory)
{
return Policy.RateLimitAsync<TResult>(numberOfExecutions, perTimeSpan, maxBurst, retryAfterFactory);
}

protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy policy)
{
if (policy is AsyncRateLimitPolicy<ResultClassWithRetryAfter> typedPolicy)
{
try
{
typedPolicy.ExecuteAsync(() => Task.FromResult(new ResultClassWithRetryAfter(ResultPrimitive.Good))).GetAwaiter().GetResult();
return (true, TimeSpan.Zero);
}
catch (RateLimitRejectedException e)
{
return (false, e.RetryAfter);
}
}
else
{
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}

protected override TResult TryExecuteThroughPolicy<TResult>(IRateLimitPolicy<TResult> policy, Context context, TResult resultIfExecutionPermitted)
{
if (policy is AsyncRateLimitPolicy<TResult> typedPolicy)
{
return typedPolicy.ExecuteAsync(ctx => Task.FromResult(resultIfExecutionPermitted), context).GetAwaiter().GetResult();
}
else
{
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}
}
}
11 changes: 11 additions & 0 deletions src/Polly.Specs/RateLimit/LockFreeTokenBucketRateLimiterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using Polly.RateLimit;

namespace Polly.Specs.RateLimit
{
public class LockFreeTokenBucketRateLimiterTests : TokenBucketRateLimiterTestsBase
{
internal override IRateLimiter GetRateLimiter(TimeSpan onePer, long bucketCapacity)
=> new LockFreeTokenBucketRateLimiter(onePer, bucketCapacity);
}
}
48 changes: 48 additions & 0 deletions src/Polly.Specs/RateLimit/RateLimitPolicySpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Polly.RateLimit;
using Polly.Specs.Helpers;
using Polly.Specs.Helpers.RateLimit;
using Polly.Utilities;
using Xunit;

namespace Polly.Specs.RateLimit
{
[Collection(Polly.Specs.Helpers.Constants.SystemClockDependentTestCollection)]
public class RateLimitPolicySpecs : RateLimitPolicySpecsBase, IDisposable
{
public void Dispose()
{
SystemClock.Reset();
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan)
{
return Policy.RateLimit(numberOfExecutions, perTimeSpan);
}

protected override IRateLimitPolicy GetPolicyViaSyntax(int numberOfExecutions, TimeSpan perTimeSpan, int maxBurst)
{
return Policy.RateLimit(numberOfExecutions, perTimeSpan, maxBurst);
}

protected override (bool, TimeSpan) TryExecuteThroughPolicy(IRateLimitPolicy policy)
{
if (policy is RateLimitPolicy typedPolicy)
{
try
{
typedPolicy.Execute(() => new ResultClassWithRetryAfter(ResultPrimitive.Good));
return (true, TimeSpan.Zero);
}
catch (RateLimitRejectedException e)
{
return (false, e.RetryAfter);
}
}
else
{
throw new InvalidOperationException("Unexpected policy type in test construction.");
}
}
}
}
Loading