Dave Leeds
11/01/2021, 9:37 PMHttpClient
alive. I’m using the Ktor client inside a REST service, and that client will be used to call out to other services. A few options for lifetimes might include:
• Create one Ktor client instance for the lifetime of the service instance. All outbound requests will be made through that one client instance (across all inbound request threads) until the service instance is shut down, at which point we would call close()
on the Ktor client.
• Create one Ktor client instance for the lifetime of the inbound HTTP request. In this case, I’d call close()
after we’re done processing the inbound request.
• Create one Ktor client instance for each outbound HTTP request that we need to make. In this case, we’d call use { }
in order to make sure the instance was properly closed once the outbound request has been completed.
What do you recommend, and why do you recommend it? Thanks!hfhbd
11/01/2021, 10:09 PMinit
is not free: you would create (useless) objects every startup.
I use the same client for each service/endpoint. Main reason is configuration: (token) authentication, default url, serialization.Rustam Siniukov
11/01/2021, 11:19 PMBig Chungus
11/01/2021, 11:27 PMDave Leeds
11/02/2021, 1:25 PM