can I combine kotlinx.coroutines.sync.Semaphore.ac...
# coroutines
r
can I combine kotlinx.coroutines.sync.Semaphore.acquire and kotlinx.coroutines.withTimeout ?
Copy code
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.withTimeout

private val concurrentRequests = Semaphore(50)

suspend fun foo() {
    withTimeout(10) {
        concurrentRequests.acquire()
    }
    try {
        doStuff()
    } finally {
        concurrentRequests.release()
    }
}
o
what do you mean by that? haven't you already done so here?
r
I guess my question was: “kotlinx.coroutines.sync.Semaphore.acquire suspends until a permit is available. Is wrapping that inside withTimeout a valid way of waiting until a permit is available or the timeout is reached, whichever comes first, or is that code racy in some way in the sense that it is possible to acquire a permit AND receive a timeout in the same coroutine?”
o
I do not believe so, either
acquire()
does not see cancellation and successfully returns (and in that case withTimeout will not throw); OR
acquire()
sees the cancellation from timeout and will not acquire the semaphore
of note is this internal comment:
Copy code
* If timeout is exceeded, but withTimeout() block was not suspended, we would like to return block value,
     * not a timeout exception.
l
You can combine them that way, yes.
r
thanks