Hello. I am using asCompletableFuture as a bridge between coroutines and futures for code where I w...
r
Hello. I am using asCompletableFuture as a bridge between coroutines and futures for code where I want to benefit from ktor client being able to run many active connections. In a small example, when errors occur the completablefuture never completes, not even with exception. How do I let the exceptions that occur bubble up correctly? https://github.com/rrva/ktor-client-connection-leak/blob/master/src/main/kotlin/se/rrva/Client.kt I am expecting ”Done with all” to be printed on each run
b
That's because you are catching the exception and ignoring it by simply returning
listOf(Foo("timeout"))
You need to allow exceptions to be thrown (including
CancellationException
) if you want those to work properly. You also don't get any benefit by attaching a blank job in your
CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO> + MDCContext())
scope
Copy code
fun fetchFooAsync(ids:List<String>): CompletableFuture<List<Foo>> {
    return GlobalScope.async(<http://Dispatchers.IO|Dispatchers.IO> + MDCContext()) {
        try {
            client.fetchFoo(ids)
        } catch (e: CancellationException) {
            throw e // you must propogate cancellation
        } catch (e: Throwable) {
            println(e.message)
            throw e // you must propogate an exception if you want CompletableFuture to know about it
        }
    }.asCompletableFuture()
}
r
thanks!
is Dispatchers.IO a proper choice?
With this code, I am seeing an unexpected high rate of connection timeouts despite the server not observing these. No syn and syn ack seen over the wire. So some internal resource and scheduling issue prevent a connection from being leased before timeout, or connections leak and are not released. What could be wrong wrt connection leaks in the above code, and why does running my Client.kt sample yield so many connect timeouts?