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

Marc Knaup

12/23/2020, 3:17 PM
What is the reason I have to manually
close()
an
HttpClient
? Why isn’t it designed so that the
HttpClient
is bound to a
CoroutineScope
and automatically closes when the scope is canceled/completed? Isn’t this breaking structured concurrency (or at least requires workarounds)?
👀 2
j

Jorge R

12/23/2020, 6:28 PM
How should a CoroutineScope know that operation is needed? they are two different objects. If you had an HttpClientCoroutineScope, then that could make sense. Maybe try to define a httpClientCoroutineScope fun wrapping coroutineScope fun?
m

Marc Knaup

12/23/2020, 8:05 PM
I don’t know what you mean 🤔
Copy code
coroutineScope {
   HttpClient().request(…)
}
The
HttpClient
should either be closed with the scope, or the
HttpClient
be closed automatically when the scope is manually canceled.
a

Alexander Weickmann

01/10/2021, 11:53 AM
There is little guidance on the ktor docs on how to handle the HttpClient instance. What we do is we create a holder object for the http client where we instantiate it as a val. This httpClient is never closed. It will stay there as long as the app runs.
m

Marc Knaup

01/10/2021, 7:45 PM
That doesn’t work well with unit testing though.
I’m trying to find a good solution for other similar clients like API clients or workers. Should they be closed? Or be launched into a scope? Ktor doesn’t seem to be a good reference here 🤔