Hello lovely folks! I have a question with regards...
# ktor
i
Hello lovely folks! I have a question with regards to
HttpClient
re-use. What is the accepted wisdom here? Create a new one as needed, or never
close()
the existing one? What do people do? Thanks!
👀 1
s
there is a note in official docs at the end
Note that creating
HttpClient
is not a cheap operation, and it’s better to reuse its instance in the case of multiple requests.
âž• 1
for single request, you can also utilize
use
, so you don’t have to worry about closing manually
Copy code
val status = HttpClient().use { client ->
    // ...
}
i
right, so leaving it open and having it handle multiple requests is ok, without concerns for memory leaks?
s
you can also close it at a safe point when you feel it’s not going to be used anymore, but apart from that having it open shouldn’t be an issue. At least I haven’t faced any
i
sweet, we're in a memory and resources constrained env so we have to be careful 🙂
s
yeah then maybe figure out a way to
close
when you absolutely don’t need it
that way you’d be more at peace with it
i
thanks Jigar 😄
l
but what if you use the client in multiple (independent) places and each of them performs a few thousand requests/min, you basically don’t have a "safe point" to ever close it… is there any real risks on this scenario?
j
Is it a good approach to use just one instance of HttpClient to hit different uServices ? I mean multiple request but to different external services not just one service.