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

Add Wait method #12

Closed
gioccher opened this issue Mar 12, 2019 · 4 comments
Closed

Add Wait method #12

gioccher opened this issue Mar 12, 2019 · 4 comments

Comments

@gioccher
Copy link

gioccher commented Mar 12, 2019

RateLimiter requires to package the rate limited code into a method. This can get awkward very quickly if the code that needs to be rate limited contains other code that needs to be rate limited, and so following the flow of the program becomes a bit difficult.
As a result, I almost always end up defining and using the following Wait extension methods, to make RateLimiter look more like other synchronization classes (SemaphoreSlim, Monitor, WaitHandle) and avoid refactoring the code into a chain of methods.

public static class TimeLimiterExtensions
{
	private static readonly Action doNothing = delegate { };

	public static async Task Wait(this TimeLimiter timeLimiter)
	{
		await timeLimiter.Perform(doNothing);
	}

	public static async Task Wait(this TimeLimiter timeLimiter, CancellationToken cancellationToken)
	{
		await timeLimiter.Perform(doNothing, cancellationToken);
	}
}

I thought this would be beneficial to other users, and was wondering if you would incorporate something like this in the project.
I'm unsure about the contribution policy, so I'm asking here instead of just creating a pull request.

@David-Desmaisons
Copy link
Owner

Helllo @gioccher , thanks for the suggestion, I will need sometime to evaualate it and give you some feedback.

@David-Desmaisons
Copy link
Owner

Tnhiking of your use case and your implementation, I thnik that implementing an GetAwaiter on TimeLimiter (maybe as an extension method ) would provide the same usability and be more robust.
It would allow to await timelimter. Reefrence:
https://weblogs.asp.net/dixin/understanding-c-sharp-async-await-2-awaitable-awaiter-pattern

@gioccher
Copy link
Author

That approach is definitely more elegant than the Wait extension method I've been using. I've pushed a couple of commits that implement that

@David-Desmaisons
Copy link
Owner

TimeLimiter are now awaitable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants