When I `val ktorClient2 = ktorClient.config { .. }...
# ktor
u
When I
val ktorClient2 = ktorClient.config { .. }
am I sharing some resources under the hood or is it completely a deep copy? I'm asking since I have a single instance shared all across the app, but a for set of apis the host changes. I know I can spell out the full url in those apis, but creating a client per that set of apis with a single default request config would be more elegant And the question is how expensive is this "scoped" ktor instance
a
The underlying client engine is shared. How many different hosts do you expect?
u
2-3; does it matter if engine is shared?
a
The resource usage for 2-3 Ktor clients shouldn't be a problem.
does it matter if engine is shared?
Do you mean matters for what?
u
well if "all" the resources are in the engine, and that instance is shared, is it a difference if I have 2 or 200?
a
I would say that most of the resources are in the engine, not all of them.
u
what would than be deep copied? everything not in the engine?
a
The installed plugins and their configurations, and the custom interceptors.
u
why doesn't it make sense to shared the instances as well?
okhttp, retrofit do that
a
Do you mean to mutate the instance instead of making its copy?
u
I mean a shallow copy - shared every instance other than the overriden ones
Copy code
val authRetrofit = appRetrofit.newBuilder()
           .baseUrl(baseAuthUrl)
           .build()
for example; this shares all the interceptor instances, just swaps the base url bit
a
Because the
DefaultRequest
is a plugin, and unfortunately, there is no mechanism to uninstall a plugin.
u
but arent plugins a set anyways? I mean I can set a new default request in the
config { .. }
lambda I presume it's not 2 then in the plugins collection
okay I dont know the internals, obviously Im imagining it simplisticly, but something to the like of
newClient(overridenPlugin ?: previousInstance.plugin)
a
I think it's possible to make the plugins replaceable/mutable but the current API doesn't allow that.