rrva
09/07/2020, 10:47 AMprivate val concurrentRequests = Semaphore(50)
private suspend fun <T> requestWithLimit(block: suspend () -> T): T {
try {
withTimeout(10) {
concurrentRequests.acquire()
}
} catch (e: Exception) {
throw RuntimeException("Overloaded", e)
}
try {
return block()
} finally {
concurrentRequests.release()
}
}
I usually call io.ktor.client.HttpClient.request inside block(). If I want to write it as an extension function on io.ktor.client.HttpClient (just to make the call site a bit cleaner), how would I define it? It seems that the extension function cannot access the variable concurrentRequests
wasyl
09/07/2020, 10:58 AMrequestWithLimit
shouldn’t change how you access concurrentRequests
. This does seem to work for me:
suspend fun <T> HttpClient.requestWithLimit(block: suspend HttpClient.() -> T): T
abstract class HttpClient {
abstract val request: Any
}
val concurrentRequests = Semaphore(50)
private suspend fun <T> HttpClient.requestWithLimit(block: suspend HttpClient.() -> T): T {
try {
withTimeout(10) {
concurrentRequests.acquire()
}
} catch (e: Exception) {
throw RuntimeException("Overloaded", e)
}
try {
return block()
} finally {
concurrentRequests.release()
}
}
suspend fun usage(httpClient: HttpClient) {
httpClient.requestWithLimit {
println("Doing something with $request")
}
}