What I want to do is try to acquire a permit from a TSemaphore
, but timeout if it takes too long to acquire.
Basically something like this:
ZIO.acquireReleaseWith(semaphore.acquire.commit.timeoutFail(new TimeoutException)(acquireTimeout))(_ => doStuff())(_ => semaphore.release.commit)
However, that doesn’t work, because it seems that the timeout
relies on interrupting the fiber, but acquireReleaseWith
doesn’t allow the acquire
step to be interrupted.
acquireReleaseInterruptible
doesn’t work, because it runs the release regardless of if the acquire
succeeds, and I don’t have a good out-of-band way to tell if the acquire succeeded.
And acquireReleaseInterruptibleExit
doesn’t really help either, because it only gives me the Exit
for the whole scope, so if the doStuff()
fails, then it will never release the permit.
What is the best way to have a timeout on the acquire step?