dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.99k stars 4.67k forks source link

[API Proposal]: RateLimiter should implement IDisposable and IAsyncDisposable #62370

Closed BrennanConroy closed 2 years ago

BrennanConroy commented 2 years ago

Background and motivation

https://github.com/dotnet/runtime/pull/61788 added the Rate Limiting work from https://github.com/dotnet/runtime/issues/52079 and while working on it we realized it made sense for rate limiters to be disposable. It started with the TokenBucket implementation which has a timer instance which should be disposed of. But it also made sense for the other rate limiters to release any pending acquisition requests from limiter.WaitAsync(..., token); as well as clean up any cancellation token registrations.

Additionally, we expect some 3rd party implementations to be backed by a network resource and async disposal is preferred in those cases.

As well as implementing the two virtual methods on the rate limiter implementations (Concurrency, TokenBucket, FixedWindow, SlidingWindow)

We went ahead and made the API disposable, filing this to make sure we properly API review it.

API Proposal

public abstract class RateLimiter
+ : IAsyncDisposable, IDisposable
{
+    protected virtual void Dispose(bool disposing) { }

+    public void Dispose() { }

+    protected virtual ValueTask DisposeAsyncCore() { }

+    public async ValueTask DisposeAsync() { }
}

API Usage

var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions(1, QueueProcessingOrder.OldestFirst, 3));
using var lease = limiter.Acquire(1);

var task = limiter.WaitAsync(2);

await limiter.DisposeAsync();

var failedLease = await task;

Alternative Designs

No response

Risks

No response

ghost commented 2 years ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation https://github.com/dotnet/runtime/pull/61788 added the Rate Limiting work from https://github.com/dotnet/runtime/issues/52079 and while working on it we realized it made sense for rate limiters to be disposable. It started with the TokenBucket implementation which has a timer instance which should be disposed of. But it also made sense for the other rate limiters to release any pending acquisition requests from `limiter.WaitAsync(..., token);` as well as clean up any cancellation token registrations. Additionally, we expect some 3rd party implementations to be backed by a network resource and async disposal is preferred in those cases. As well as implementing the two virtual methods on the rate limiter implementations (Concurrency, TokenBucket, FixedWindow, SlidingWindow) We went ahead and made the API disposable, filing this to make sure we properly API review it. ### API Proposal ```diff public abstract class RateLimiter + : IAsyncDisposable, IDisposable { + protected virtual void Dispose(bool disposing) { } + public void Dispose() { } + protected virtual ValueTask DisposeAsyncCore() { } + public async ValueTask DisposeAsync() { } } ``` ### API Usage ```C# var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions(1, QueueProcessingOrder.OldestFirst, 3)); using var lease = limiter.Acquire(1); var task = limiter.WaitAsync(2); await limiter.DisposeAsync(); var failedLease = await task; ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: BrennanConroy
Assignees: -
Labels: `api-suggestion`, `area-System.Threading`, `untriaged`
Milestone: -
bartonjs commented 2 years ago

Video

Looks good as proposed

namespace System.Threading.RateLimiting
{
    public partial class RateLimiter : IAsyncDisposable, IDisposable
    {
        protected virtual void Dispose(bool disposing) { }

        public void Dispose() { }

        protected virtual ValueTask DisposeAsyncCore() { }

        public ValueTask DisposeAsync() { }
    }
}
BrennanConroy commented 2 years ago

Thanks 😃 Already done via https://github.com/dotnet/runtime/pull/61788