Hi, is there a reason why the <OkHttpEngine> is no...
# ktor
n
Hi, is there a reason why the OkHttpEngine is not using the dispatcher from the
config.preconfigured
(if it is set)? it looks like it is always creating a default OkHttp Dispatcher
builder.dispatcher(Dispatcher())
Example: in the following code snippet the custom dispatcher is not used ⚠️
Copy code
val threadFactory = ThreadFactory { runnable ->
            val thread = Thread(runnable)
            thread.name = "CustomThread-${thread.id}"
            thread
        }
        val executorService = Executors.newFixedThreadPool(4, threadFactory)
        val dispatcher = Dispatcher(executorService) // <---------- This Dispatcher is not used 

        val okHttpClient = OkHttpClient
            .Builder()
            .dispatcher(dispatcher)
            .build()

        // Create a Ktor HttpClient using the OkHttp engine
        val ktorHttpClient = HttpClient(OkHttp) {
            engine {
                preconfigured = okHttpClient

            }
        }
Let me know if you need this reported as an issue
n
Ran into an issue with this too because the default dispatcher allows quite few concurrent calls, so I switched to CIO for now
gratitude thank you 1
I looked at it again and figured out that this config works:
Copy code
engine {
    config {
        dispatcher(
            Dispatcher().apply {
                maxRequestsPerHost = 200
                maxRequests = 1000
            }
        )
    }
}
This is how ktor executes it. The config is applied after the dispatcher is applied, so you can override it:
Copy code
private fun createOkHttpClient(timeoutExtension: HttpTimeoutConfig?): OkHttpClient {
    val builder = (config.preconfigured ?: okHttpClientPrototype).newBuilder()

    builder.dispatcher(Dispatcher())
    builder.apply(config.config)
    config.proxy?.let { builder.proxy(it) }
    timeoutExtension?.let {
        builder.setupTimeoutAttributes(it)
    }

    return builder.build()
}
n
I’ll give it a try, thank you Nils 👍