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

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

Closed
BrennanConroy opened this issue Dec 3, 2021 · 3 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading
Milestone

Comments

@BrennanConroy
Copy link
Member

Background and motivation

#61788 added the Rate Limiting work from #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

@BrennanConroy BrennanConroy added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Dec 3, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Threading untriaged New issue has not been triaged by the area owner labels Dec 3, 2021
@ghost
Copy link

ghost commented Dec 3, 2021

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

Issue Details

Background and motivation

#61788 added the Rate Limiting work from #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

Author: BrennanConroy
Assignees: -
Labels:

api-suggestion, area-System.Threading, untriaged

Milestone: -

@BrennanConroy BrennanConroy added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner labels Feb 3, 2022
@BrennanConroy BrennanConroy added this to the 7.0.0 milestone Feb 3, 2022
@bartonjs
Copy link
Member

bartonjs commented Mar 15, 2022

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() { }
    }
}

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Mar 15, 2022
@BrennanConroy
Copy link
Member Author

Thanks 😃
Already done via #61788

@BrennanConroy BrennanConroy self-assigned this Mar 15, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading
Projects
None yet
Development

No branches or pull requests

2 participants