fix: inconsistent custom okhttp configuration #901
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Situation
In pull-request#857, we proposed a new feature that enables users to inject their own OkHttp client when configuring the SDK client.
A bug was reported to us that custom
Dispatcher
configurations onOkHttpClient
instances are being overridden by OkHttp's default configurations. Look the example below:In the previous example, we expect
maxRequests
andmaxRequestsPerHost
values to be configured properly to their given values. Unfortunately, that's not the case, and configurations are overridden to OkHttp's default values.We pass custom
OkHttpClient
instance toKtor
through theHttpClientEngineFactory
utility, which enables us to create and configure instances of theOkHttpEngine
.When we pass an
OkHttpClient
instance to theKtor
client,Ktor
attempts to clone ourOkHttpClient
instance, then executing our configuration code-block. The issue is thatKtor
creates a new dispatcher instance and setting it on the new instance after cloning our client. Looking theKtor
code-base, we can observe that behavior in the following code:Breaking down the code, we can observe the following facts:
OkHttpClient
instance is cloned and transformed into an instance ofOkHttpBuilder
.Dispatcher
instance is created and set on the new builder.builder.dispatcher(Dispatcher())
builder.apply(config.config)
As a result, our custom
Dispatcher
instance is overridden by a new instance holding the default values set in theDispatcher
class.Task
In order to resolve the issue and persist custom
Dispatcher
instances, we need to make sure the theDispatcher
instances is set again properly in theustom-configuration block
. We do not need to care about other values and configurations since they are cloned properly and not overridden later on.Action
For each client, we made sure
Dispatcher
instances are set properly on the custom-configuration block level.Testing
Verified manually. It would be hard to unit-test setting custom
OkHttpClient
instances because the instance is set to be inaccessible (private-visibility).Results
Custom
Dispatcher
instances are now preserved and respected. Given the following configuration as a sample:We can observe that our configurations are respected and preserved for each request.
Notes
NaN