Skip to content

Commit

Permalink
Add Fixed and Sliding Window RateLimiters + PartitionedRateLimiter.Cr…
Browse files Browse the repository at this point in the history
…eate (#68695)

* Add RL non-generic fixed window, sliding window implementations (#68087)

* Add initial impl of PartitionedRateLimiter.Create (#67677)

Co-authored-by: Shreya Verma <shve9863@colorado.edu>
  • Loading branch information
BrennanConroy and shreyaverma165 authored Apr 29, 2022
1 parent 083181f commit 9c37a3b
Show file tree
Hide file tree
Showing 21 changed files with 3,792 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public MetadataName(string name) { }
public static bool operator !=(System.Threading.RateLimiting.MetadataName<T> left, System.Threading.RateLimiting.MetadataName<T> right) { throw null; }
public override string ToString() { throw null; }
}
public static partial class PartitionedRateLimiter
{
public static System.Threading.RateLimiting.PartitionedRateLimiter<TResource> Create<TResource, TPartitionKey>(System.Func<TResource, System.Threading.RateLimiting.RateLimitPartition<TPartitionKey>> partitioner, System.Collections.Generic.IEqualityComparer<TPartitionKey>? equalityComparer = null) where TPartitionKey : notnull { throw null; }
}
public abstract partial class PartitionedRateLimiter<TResource> : System.IAsyncDisposable, System.IDisposable
{
protected PartitionedRateLimiter() { }
Expand Down Expand Up @@ -83,6 +87,21 @@ protected virtual void Dispose(bool disposing) { }
public abstract bool TryGetMetadata(string metadataName, out object? metadata);
public bool TryGetMetadata<T>(System.Threading.RateLimiting.MetadataName<T> metadataName, [System.Diagnostics.CodeAnalysis.MaybeNullAttribute] out T metadata) { throw null; }
}
public static partial class RateLimitPartition
{
public static System.Threading.RateLimiting.RateLimitPartition<TKey> CreateConcurrencyLimiter<TKey>(TKey partitionKey, System.Func<TKey, System.Threading.RateLimiting.ConcurrencyLimiterOptions> factory) { throw null; }
public static System.Threading.RateLimiting.RateLimitPartition<TKey> CreateNoLimiter<TKey>(TKey partitionKey) { throw null; }
public static System.Threading.RateLimiting.RateLimitPartition<TKey> CreateTokenBucketLimiter<TKey>(TKey partitionKey, System.Func<TKey, System.Threading.RateLimiting.TokenBucketRateLimiterOptions> factory) { throw null; }
public static System.Threading.RateLimiting.RateLimitPartition<TKey> Create<TKey>(TKey partitionKey, System.Func<TKey, System.Threading.RateLimiting.RateLimiter> factory) { throw null; }
}
public partial struct RateLimitPartition<TKey>
{
private readonly TKey _PartitionKey_k__BackingField;
private object _dummy;
private int _dummyPrimitive;
public RateLimitPartition(TKey partitionKey, System.Func<TKey, System.Threading.RateLimiting.RateLimiter> factory) { throw null; }
public readonly TKey PartitionKey { get { throw null; } }
}
public abstract partial class ReplenishingRateLimiter : System.Threading.RateLimiting.RateLimiter
{
protected ReplenishingRateLimiter() { }
Expand Down Expand Up @@ -113,4 +132,49 @@ public TokenBucketRateLimiterOptions(int tokenLimit, System.Threading.RateLimiti
public int TokenLimit { get { throw null; } }
public int TokensPerPeriod { get { throw null; } }
}
public sealed partial class SlidingWindowRateLimiter : System.Threading.RateLimiting.ReplenishingRateLimiter
{
public SlidingWindowRateLimiter(System.Threading.RateLimiting.SlidingWindowRateLimiterOptions options) { }
public override System.TimeSpan? IdleDuration { get { throw null; } }
public override bool IsAutoReplenishing { get { throw null; } }
public override System.TimeSpan ReplenishmentPeriod { get { throw null; } }
protected override System.Threading.RateLimiting.RateLimitLease AcquireCore(int requestCount) { throw null; }
protected override void Dispose(bool disposing) { }
protected override System.Threading.Tasks.ValueTask DisposeAsyncCore() { throw null; }
public override int GetAvailablePermits() { throw null; }
public override bool TryReplenish() { throw null; }
protected override System.Threading.Tasks.ValueTask<System.Threading.RateLimiting.RateLimitLease> WaitAsyncCore(int requestCount, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public sealed partial class SlidingWindowRateLimiterOptions
{
public SlidingWindowRateLimiterOptions(int permitLimit, System.Threading.RateLimiting.QueueProcessingOrder queueProcessingOrder, int queueLimit, System.TimeSpan window, int segmentsPerWindow, bool autoReplenishment = true) { }
public bool AutoReplenishment { get { throw null; } }
public int QueueLimit { get { throw null; } }
public System.Threading.RateLimiting.QueueProcessingOrder QueueProcessingOrder { get { throw null; } }
public System.TimeSpan Window { get { throw null; } }
public int PermitLimit { get { throw null; } }
public int SegmentsPerWindow { get { throw null; } }
}
public sealed partial class FixedWindowRateLimiter : System.Threading.RateLimiting.ReplenishingRateLimiter
{
public FixedWindowRateLimiter(System.Threading.RateLimiting.FixedWindowRateLimiterOptions options) { }
public override System.TimeSpan? IdleDuration { get { throw null; } }
public override bool IsAutoReplenishing { get { throw null; } }
public override System.TimeSpan ReplenishmentPeriod { get { throw null; } }
protected override System.Threading.RateLimiting.RateLimitLease AcquireCore(int requestCount) { throw null; }
protected override void Dispose(bool disposing) { }
protected override System.Threading.Tasks.ValueTask DisposeAsyncCore() { throw null; }
public override int GetAvailablePermits() { throw null; }
public override bool TryReplenish() { throw null; }
protected override System.Threading.Tasks.ValueTask<System.Threading.RateLimiting.RateLimitLease> WaitAsyncCore(int requestCount, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public sealed partial class FixedWindowRateLimiterOptions
{
public FixedWindowRateLimiterOptions(int permitLimit, System.Threading.RateLimiting.QueueProcessingOrder queueProcessingOrder, int queueLimit, System.TimeSpan window, bool autoReplenishment = true) { }
public bool AutoReplenishment { get { throw null; } }
public int QueueLimit { get { throw null; } }
public System.Threading.RateLimiting.QueueProcessingOrder QueueProcessingOrder { get { throw null; } }
public System.TimeSpan Window { get { throw null; } }
public int PermitLimit { get { throw null; } }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
Expand All @@ -16,20 +16,30 @@ System.Threading.RateLimiting.RateLimitLease</PackageDescription>
<ItemGroup>
<Compile Include="System\Threading\RateLimiting\ConcurrencyLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\ConcurrencyLimiterOptions.cs" />
<Compile Include="System\Threading\RateLimiting\FixedWindowRateLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\FixedWindowRateLimiterOptions.cs" />
<Compile Include="System\Threading\RateLimiting\MetadataName.cs" />
<Compile Include="System\Threading\RateLimiting\MetadataName.T.cs" />
<Compile Include="System\Threading\RateLimiting\NoopLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\PartitionedRateLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\PartitionedRateLimiter.T.cs" />
<Compile Include="System\Threading\RateLimiting\QueueProcessingOrder.cs" />
<Compile Include="System\Threading\RateLimiting\RateLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\RateLimitLease.cs" />
<Compile Include="System\Threading\RateLimiting\RateLimitPartition.cs" />
<Compile Include="System\Threading\RateLimiting\RateLimitPartition.T.cs" />
<Compile Include="System\Threading\RateLimiting\ReplenishingRateLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\SlidingWindowRateLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\SlidingWindowRateLimiterOptions.cs" />
<Compile Include="System\Threading\RateLimiting\TimerAwaitable.cs" />
<Compile Include="System\Threading\RateLimiting\TokenBucketRateLimiter.cs" />
<Compile Include="System\Threading\RateLimiting\TokenBucketRateLimiterOptions.cs" />
<Compile Include="$(CommonPath)System\Collections\Generic\Deque.cs" Link="Common\System\Collections\Generic\Deque.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Reference Include="System.Runtime" />
<Reference Include="System.Threading" />
<Reference Include="System.Collections" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ protected override ValueTask<RateLimitLease> WaitAsyncCore(int permitCount, Canc
RequestRegistration oldestRequest = _queue.DequeueHead();
_queueCount -= oldestRequest.Count;
Debug.Assert(_queueCount >= 0);
oldestRequest.Tcs.TrySetResult(FailedLease);
if (!oldestRequest.Tcs.TrySetResult(FailedLease))
{
// Updating queue count is handled by the cancellation code
_queueCount += oldestRequest.Count;
}
}
while (_options.QueueLimit - _queueCount < permitCount);
}
Expand Down Expand Up @@ -249,7 +253,7 @@ protected override void Dispose(bool disposing)
? _queue.DequeueHead()
: _queue.DequeueTail();
next.CancellationTokenRegistration.Dispose();
next.Tcs.SetResult(FailedLease);
next.Tcs.TrySetResult(FailedLease);
}
}
}
Expand Down
Loading

0 comments on commit 9c37a3b

Please sign in to comment.