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

Allow Semaphore to decrease the number of permits #5308

Closed
pkolaczk opened this issue Dec 21, 2022 · 4 comments · Fixed by #6331
Closed

Allow Semaphore to decrease the number of permits #5308

pkolaczk opened this issue Dec 21, 2022 · 4 comments · Fixed by #6331
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync

Comments

@pkolaczk
Copy link

Is your feature request related to a problem? Please describe.
Semaphore has a method to add permits, but misses a symmetric method to decrease the number of permits.
Sometimes a limit on the amount of the available resource can be set too large and although all the existing permits are not used their number cannot be reduced.

Describe the solution you'd like
Add methods fn remove_permits(count: usize) -> usize that would remove up to count permits. If not enough permits are available, it would remove only the available ones and return the number of permits removed.

Describe alternatives you've considered
Don't know if there is any workaround for that.

Additional context
I'm using a semaphore to limit the amount of memory used by my program. The user is able to dynamically change the limit, but the current implementation of the semaphore allows them to only increase the limit, and if they accidentally set it too high, they have no chance to correct the error without resetting the whole thing (restarting the whole app).

@pkolaczk pkolaczk added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Dec 21, 2022
@Darksonn Darksonn added the M-sync Module: tokio/sync label Dec 21, 2022
@Darksonn
Copy link
Contributor

I don't mind adding this, but for your proposed use-case, I would probably just do this:

tokio::spawn(async move { semaphore.acquire(NUM_TO_DELETE).await.forget(); });

@pkolaczk
Copy link
Author

Thank you! Looks like a nice workaround. That would almost solve it, but it is limited to u32.

@AdityaAtulTewari
Copy link

I'd like to try and solve this issue if it is still something that would be desirable.

@indietyp
Copy link

Would love to this see this, I am currently struggling in an application of mine where I'd like to temporarily decrease the amount of permits used in a specific section.

This is currently possible by first releasing the permits, then acquiring the reduced amount. This solution sadly has some major obvious drawbacks, because permits need to be acquired again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants