https://kotlinlang.org logo
#ktor
Title
# ktor
v

Vinay Pothnis

10/22/2021, 4:59 PM
Hello KTor Team! I wanted to check with on couple of things related to
ktor-client
and the best practices around it. 1. Whats the right way to re-use connections (or use a connection pool) with
CIOEngine
? I am using the following configs:
Copy code
engine {
    maxConnectionsCount = 250
    pipelining = true
    endpoint {
      maxConnectionsPerRoute = 100
      pipelineMaxSize = 20
      keepAliveTime = 5000
      connectTimeout = 5000
      connectAttempts = 5
      threadsCount = 20
    }
  }
It looks like without the
pipielining = true
flag, new connections were being created for every request/call. Are these sufficient or is there any other config that I would need to set to effectively re-use connections? 2. Whats the recommended way to use
client.close()
or
client.use {}
- Especially when we are sharing/re-using the
HttpClient
I went through the docs here - https://ktor.io/docs/client.html#close-client With a shared
HttpClient
- if I used
client.use {}
- i was seeing errors on subsequent requests after some initial successful requests. Errors like -
kotlinx.coroutines.JobCancellationException: Parent job is Completed; job=JobImpl{Completed}@5e4f8454
So, when should this actually be used? During shutdown to release/cleanup the resources? Thanks so much for your help with this!
a

Aleksei Tirman [JB]

10/25/2021, 8:43 AM
2. The
use
extension function just executes the block and then closes a client. So if you want to share a client make sure to close it after making all necessary requests.
By the way, the CIO engine is not production-ready.
v

Vinay Pothnis

10/25/2021, 4:17 PM
@Aleksei Tirman [JB] thanks so much for your inputs! Oh! I see, thanks for the note on CIO engine.
I see that the OkHTTP is listed as applicable for
Android
in the docs. Can this be used for JVM (non Android) as well? I see that Jetty Engine only supports Http2. So, that leaves only the Apache Engine. I wasn’t sure if that supported connection pooling. (At least it wasn’t evident from the docs) Wondering what would be your recommendation for a production ready engine that can support connection pooling/reuse?
a

Aleksei Tirman [JB]

10/25/2021, 5:34 PM
OkHttp engine could be used for JVM (non Android).
Wondering what would be your recommendation for a production ready engine that can support connection pooling/reuse?
I don't actually know. @e5l could you please help with advice?
v

Vinay Pothnis

10/25/2021, 5:35 PM
OkHttp engine could be used for JVM (non Android).
Thanks @Aleksei Tirman [JB]!
e

e5l

10/25/2021, 5:36 PM
Apache engine is also good option
v

Vinay Pothnis

10/25/2021, 5:37 PM
Gotcha! Thanks @e5l !
👍 1
l

Lucas Milotich

10/27/2021, 2:13 PM
I have been using ktor apache since this year, after some tuning options I can say it has a good performance
You can configure reuse connection strategy, keep alive strategy, io threads, etc
7 Views