-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
tokio-sync: Acquire n permits from a semaphore #1550
Comments
👍 It probably would be nice to be able to acquire an arbitrary number of permits. I am unsure about how easy / hard it would be to add that though! If anyone wants to take a stab, I can try to take a look at strategy. |
I took a quick look and I think it shouldn't be that hard. I'll take a stab at it once I'll need it, hopefully this week. |
Alright I took a better look and it would be incompatible with the current semaphore design since it assumes that it only needs to wakeup a waiter if the permit count is zero. This is because, when the permit count is zero, then the |
What if the |
This would limit the max amount of queued wakers, but I don't think thats much of a problem. |
futures_intrusive has an implementation for such a semaphore. |
I’ll leave the issue open as I think the API would be useful and I would appreciate a contribution. |
I implemented something like this for limiting the amount of bytes that could be allocated for a certain purpose. My implementation definitely wouldn't be a general purpose solution, but I want to add my use case here. I implemented this using a |
The low-level API for Semaphore has already implemented this feature. It would be great to expose this. In my case I want to have a soft limit on amount of memory used in my server, so having permits equal to reserved memory and |
Maybe we should also implement |
At this point, the issue should be fairly easy. Maybe something like: |
I will give this a try. |
Currently the
0.2.0-alpha.4
Semaphore
&Permit
API oftokio-sync
only allows one to acquire permits one at a time (as far as i can tell). Wouldn't theSemaphore
API be more usefull is one could acquire an arbitrary number of permits at a time?For instance
Permit::poll_acquire_n
would take an extra arg use to decide how much permits to acquire. And an extra check would be added to make sure the requested number of permits does not exceed the semaphore's total capacity.My specific use case would be a async channel that blocks based on the combined len of the message it contains.
The text was updated successfully, but these errors were encountered: