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

Viktor Petrovski

09/08/2021, 8:16 AM
When using Ktor client for consuimg REST API as far as I understand
Copy code
val status = HttpClient().use { client ->
    // ...
}
should be preferred to
Copy code
client.get("<https://ktor.io/>")
So there is no need to call the
.close()
function
👌 1
a

Aleksei Tirman [JB]

09/08/2021, 10:21 AM
Although it will be resource consuming if you create an HTTP client for each request.
1
v

Viktor Petrovski

09/08/2021, 10:45 AM
Oh yeah of course not, the client will be passed to the constructor of the API, this was just an example
This doesn’t work tho, after the first request I get Parent job is Completed;
a

Aleksei Tirman [JB]

09/08/2021, 4:34 PM
Please share a code snippet.
v

Viktor Petrovski

09/09/2021, 8:46 AM
image.png
a

Aleksei Tirman [JB]

09/09/2021, 10:39 AM
Could you please share a complete code snippet as text to reproduce your problem?
b

Bogdan

09/10/2021, 1:53 PM
https://ktor.io/docs/client.html#close-client with this code, you need to recreate the HttpClient for each request
after close execution and completion of all requests (which started before calling close) - threads are stopped, closed coroutines scopes etc.
👍 1
v

Viktor Petrovski

09/10/2021, 1:58 PM
Yeah makes sense, I assume it’s not a good idea to create a new client for each request, but use the same one application wide
2 Views