Al A
Al A

Reputation: 225

How can I make a ktor netty server use a CachedThreadPool to process requests?

Using ktor (1.4, can upgrade if needed) for a server side application, vanilla initialization:

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
...
fun Application.module() {
    ... stuff
}

I would like the underlying Netty engine to use a CachedThreadPool to process requests. Quite a few of my requests take substantial time to process (e.g. running long queries against a database), which I assume will block the thread processing the request and potentially make the server unresponsive.

How do I do that? Any other options? Do I need to make other changes (e.g. to the coroutine dispatchers) to make sure this has the desired effect?

Upvotes: 1

Views: 500

Answers (1)

expert
expert

Reputation: 30145

I think it should be something like this

val env: ApplicationEngineEnvironment = ....
val server = embeddedServer(Netty, env) {
    configureBootstrap = {
        group(NioEventLoopGroup(..., CachedThreadPool(..)))
    }
}

server.start(wait = true)

Upvotes: 1

Related Questions