rrva
08/26/2020, 9:06 PMimport 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()
}
}
octylFractal
08/26/2020, 9:06 PMrrva
08/26/2020, 9:12 PMoctylFractal
08/26/2020, 9:15 PMacquire()
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 semaphoreoctylFractal
08/26/2020, 9:15 PM* If timeout is exceeded, but withTimeout() block was not suspended, we would like to return block value,
* not a timeout exception.
louiscad
08/27/2020, 6:47 AMrrva
08/27/2020, 9:38 AM