The parent coroutine context for all requests can be configured when starting the ktor server.
When creating the embedded server multiple helper methods can be used. The most commonly used, however, passes an EmptyCoroutineContext, and so that one cannot be used.
I used
public fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration>
CoroutineScope.embeddedServer(
factory: ApplicationEngineFactory<TEngine, TConfiguration>,
vararg connectors: EngineConnectorConfig = arrayOf(EngineConnectorBuilder()),
watchPaths: List<String> = listOf(WORKING_DIRECTORY_PATH),
parentCoroutineContext: CoroutineContext = EmptyCoroutineContext,
configure: TConfiguration.() -> Unit = {},
module: Application.() -> Unit
): TEngine
insead.
Ej:
GlobalScope.embeddedServer(
Netty,
port = config.port,
parentCoroutineContext = myThreadLocal.asContextElement()
) {
...
}