-
Notifications
You must be signed in to change notification settings - Fork 0
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
Main-thread acquireSyncOrFail
and withSyncOrFail
#2
Comments
@bakkot I did indeed mean a synchronous I am not yet convinced of the need to know the current number of available tokens. Specifically, if you have a synchronous acquire method you can do a non racy synchronous acquire, even in multi-agent scenarios, without needing to know the available tokens. I am worried that exposing the current number of available tokens would lead people to write acquiry code that contains TOCTOU races. - if (semaphore.availableTokens > 0) {
- semaphore.tryAcquireSync(); // this could throw!
- }
+ try {
+ semaphore.tryAcquireSync();
+ } catch {
+ // failed to acquire, try again later
+ } I see your point about throwing vs returning |
I feel strongly that avoiding the |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Why is it not the common case? I think it could be the common case in some applications. |
For "common case" instead read "happy path". That is, in the "resource is not available" case you're going to have to go back and wait for someone to release the resource, which is probably going to take a lot longer than whatever overhead there is from the exception. |
@bakkot I don't think that is necessarily true. You may want to use this as a "only collect traces for 10 req at a time" mechanism where you check on every incoming request whether you should trace it (based on the governor's capacity) |
It's true that it's possible to write code where the performance cost of the exception would matter, but I really do not expect that to be at all common. |
Issue originally posted by @bakkot in lucacasonato/proposal-semaphore#1
One of the questions in the readme:
I think definitely yes.
Moreover I think there should be a way to synchronously acquire a token when one is available, as in
or
The
await semaphore.acquire()
construct is convenient in places which are already async, but not all places can afford to be async.Unfortunately this pattern fits awkwardly with cross-thread semaphores, because someone can in theory acquire a token between when you check if any are available and when you call
acquireSyncOrFail
. But as long asacquireSyncOrFail
is atomic, that just gives you an error, not a race.Maybe this is what you're getting at with the "try acquire" possibility raised in the readme, if you're suggesting that such a method be synchronous, but I think it's important that it throw rather than returning
null
. Otherwise it's too easy to writeusing permit = semaphore.tryAcquire()
and not notice that it failed to acquire the token.The text was updated successfully, but these errors were encountered: