Hi, <https://ktor.io/docs/create-client.html#close...
# ktor
b
Hi, https://ktor.io/docs/create-client.html#close-client I’m using Httpclient with Koin such as
Copy code
single {
    HttpClient() 
}
But is it ok? Since documentations suggest that you need to close created http client
s
I'm curious about this, too. We're noticing that after a certain amount of time, it seems our client just keeps getting “timeouts” with “unknown ms” response time, and I seem to recall seeing somewhere that this could be because our client lived for too long. But it's not clear to me when to actually close the client, i.e. when we're “finished working with the HTTP client”. My use case is specifically Android, but it should be the same for other clients, too.
There's also
Note that creating
HttpClient
is not a cheap operation, and it's better to reuse its instance in the case of multiple requests.
👍 2
m
We create one instance of an HTTP client on start and use it for the whole lifetime of the application and never had any problems (inside a Ktor service/JVM using CIO engine)
b
There's onClose inside withOptions koin builder
You can close client there along with koin lifecycle
j
Hey Sindre Moen, did you ever figure out what causes the timeouts after so long? I've had the same issue for months, and haven't been able to nail down the cause. I can sometimes go days without a problem, but once it happens I get the first unknown ms timeout, client is broken until I do a clean install. I have noticed that if I change the client config (toggling pipelining) that seems to fix it without uninstalling. I have it scoped to my activity viewmodel, is that possibly wrong? Thanks in advance if anyone has some info.
s
No, unfortunately not, and it's still an issue for us. Haven't had time to focus on it, unfortunately. I'm not sure if I want to start closing the clients after “some time”, as I don't know when that time would be.
j
same. It's hard to troubleshoot with multi-day gaps. Have you also been having to uninstall/reinstall? Or are you just able to force restart?
also, is your use android?
s
My use-case is also on Android, yeah. And it's enough for us to kill the app and restart it.
Note that on newer Android versions, that requires swiping the app away, or maybe even force-stopping it, not just navigating back from the first screen.
j
good to know! force restopping doesn't fix mine. I literally have to uninstall, which seems so strange. It kind of makes me think it's about how/where i'm creating it. Maybe the viewmodel lives too long?
s
Do you create new clients for each screen/view model?
j
No, I create one client with logging, and one without. I have a single activity, and screens are all via compose navigation
s
Right, similar to us, then, except only a single client across the whole lifecycle of the application.
We never explicitly close the client, but when the app dies, it should die, too.
j
yeah,
have you seen any logs about unclosed resources when you start your app?
I occasionally see "a resource failed to call close" on startup. But almost never and I never close the client
s
Haven't seen that, no, but I also haven't looked for it.
👍 1