-
Notifications
You must be signed in to change notification settings - Fork 5k
[API Proposal]: Add leaveOpen concept to PartitionedRateLimiter.TranslateKey #72551
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
Comments
Tagging subscribers to this area: @mangod9 Issue DetailsBackground and motivationOriginally proposed and approved in #65400 (comment), the A few examples of prior-art for this type of API:
API Proposalnamespace System.Threading.RateLimiting;
public abstract class PartitionedRateLimiter<TResource> : IAsyncDisposable, IDisposable
{
- public PartitionedRateLimiter<TOuter> TranslateKey<TOuter>(Func<TOuter, TResource> keyAdapter);
+ public PartitionedRateLimiter<TOuter> TranslateKey<TOuter>(Func<TOuter, TResource> keyAdapter, bool leaveOpen = false);
} API Usagevar limiter = PartitionedRateLimiter.Create<string, int>(resource =>
{
if (resource == "1")
{
return RateLimitPartition.GetConcurrencyLimiter(1,
_ => new ConcurrencyLimiterOptions(1, QueueProcessingOrder.NewestFirst, 1));
}
return RateLimitPartition.GetNoLimiter(2);
});
var translateLimiter = limiter.TranslateKey<int>(i => i.ToString(), leaveOpen: false);
translateLimiter.Acquire(1);
translateLimiter.Dispose(); Alternative DesignsLeave as-is and require user to keep track of the inner limiter and call Dispose/DisposeAsync on that when done. RisksIf more options are added for I am unable to think of options we might want to add in the future though.
|
namespace System.Threading.RateLimiting;
public abstract class PartitionedRateLimiter<TResource> : IAsyncDisposable, IDisposable
{
- public PartitionedRateLimiter<TOuter> TranslateKey<TOuter>(Func<TOuter, TResource> keyAdapter);
+ public PartitionedRateLimiter<TOuter> WithTranslatedKey<TOuter>(Func<TOuter, TResource> keyAdapter, bool leaveOpen);
} |
Background and motivation
Originally proposed and approved in #65400 (comment), the
PartitionedRateLimiter<TResource>.TranslateKey(...)
method wraps and returns a new instance ofPartitionedRateLimiter
. Currently we are assuming no ownership of the wrapped limiter to be on the safe side. The proposal is to add a flag to give ownership to the wrapper soDispose
andDisposeAsync
will cleanup the wrapped limiter.A few examples of prior-art for this type of API:
runtime/src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs
Line 181 in dc2db86
runtime/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs
Line 93 in dc2db86
runtime/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReader.cs
Line 100 in b12e288
API Proposal
API Usage
Alternative Designs
Leave as-is and require user to keep track of the inner limiter and call Dispose/DisposeAsync on that when done.
Risks
If more options are added for
TranslateKey
then we'll end up with additional overloads. Like howStreamWriter
andSslStream
have a bunch of overloads.I am unable to think of options we might want to add in the future though.
The text was updated successfully, but these errors were encountered: