Reuse same TCP stream for multiple http requests in ktor

69 Views Asked by At
import io.ktor.client.*
import io.ktor.client.engine.java.*
import io.ktor.client.request.*
import kotlinx.coroutines.*

suspend fun main() {
    val scope = CoroutineScope(Dispatchers.IO)
    val client = HttpClient(Java)
    val responseDeferred = (0..2).map { scope.async { client.get("http://ktor.io/") } }
    responseDeferred.forEach { it.await() }
    scope.cancel()
    client.close()
}

The above code creates 3 TCP connections (one per "GET"). TCP connection is reused if I send those requests sequentially (instead of async). I would like to know I there any way to achieve this with async.

0

There are 0 best solutions below