-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TranslateKey to PartitionedRateLimiter (#69407)
- Loading branch information
1 parent
2fc9108
commit d6fef3b
Showing
6 changed files
with
280 additions
and
2 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
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
66 changes: 66 additions & 0 deletions
66
...ies/System.Threading.RateLimiting/src/System/Threading/RateLimiting/TranslatingLimiter.cs
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 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Threading.RateLimiting | ||
{ | ||
internal sealed class TranslatingLimiter<TInner, TResource> : PartitionedRateLimiter<TResource> | ||
{ | ||
private readonly PartitionedRateLimiter<TInner> _innerRateLimiter; | ||
private readonly Func<TResource, TInner> _keyAdapter; | ||
|
||
private bool _disposed; | ||
|
||
public TranslatingLimiter(PartitionedRateLimiter<TInner> inner, Func<TResource, TInner> keyAdapter) | ||
{ | ||
_innerRateLimiter = inner; | ||
_keyAdapter = keyAdapter; | ||
} | ||
|
||
public override int GetAvailablePermits(TResource resource) | ||
{ | ||
ThrowIfDispose(); | ||
TInner key = _keyAdapter(resource); | ||
return _innerRateLimiter.GetAvailablePermits(key); | ||
} | ||
|
||
protected override RateLimitLease AcquireCore(TResource resource, int permitCount) | ||
{ | ||
ThrowIfDispose(); | ||
TInner key = _keyAdapter(resource); | ||
return _innerRateLimiter.Acquire(key, permitCount); | ||
} | ||
|
||
protected override ValueTask<RateLimitLease> WaitAndAcquireAsyncCore(TResource resource, int permitCount, CancellationToken cancellationToken) | ||
{ | ||
ThrowIfDispose(); | ||
TInner key = _keyAdapter(resource); | ||
return _innerRateLimiter.WaitAndAcquireAsync(key, permitCount, cancellationToken); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
_disposed = true; | ||
base.Dispose(disposing); | ||
} | ||
|
||
protected override ValueTask DisposeAsyncCore() | ||
{ | ||
_disposed = true; | ||
return base.DisposeAsyncCore(); | ||
} | ||
|
||
private void ThrowIfDispose() | ||
{ | ||
if (_disposed) | ||
{ | ||
throw new ObjectDisposedException(nameof(PartitionedRateLimiter)); | ||
} | ||
} | ||
} | ||
} |
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