chrisjenx
07/11/2025, 3:41 PM//fun Application.installContextCaching() {
// intercept(ApplicationCallPipeline.Plugins) {
// withContextCache {
// proceedWith(subject)
// }
// }
//}
Seems the pipelining don't wrap interceptors which is what is normally expected.?minivac
07/11/2025, 4:03 PMval yourPlugin = createRouteScopedPlugin(
name = "Plugin",
createConfiguration = { Config() }) {
on(RequestContextRouteHook) { call, proceed ->
withContext(...) { proceed() }
}
}
chrisjenx
07/11/2025, 4:04 PMchrisjenx
07/11/2025, 4:42 PMchrisjenx
07/11/2025, 4:42 PMchrisjenx
07/11/2025, 4:47 PMchrisjenx
07/11/2025, 4:52 PMminivac
07/13/2025, 12:05 PMchrisjenx
07/14/2025, 6:18 AMAleksei Tirman [JB]
07/14/2025, 8:37 AMfun main() {
embeddedServer(Netty, port = 3333) {
routing {
withRequestContextCache {
get {
println(coroutineContext[MyContextElement])
call.respondText { "OK" }
}
}
}
}.start(wait = true)
}
fun Route.withRequestContextCache(
build: Route.() -> Unit
): Route {
val route = this
route.install(ContextCache)
route.build()
return route
}
val ContextCache = createRouteScopedPlugin(
name = "ContextCache",
createConfiguration = {}) {
on(RequestContextCacheRouteHook) { call, proceed ->
withContext(MyContextElement()) {
proceed()
}
}
}
internal object RequestContextCacheRouteHook : Hook<suspend (ApplicationCall, suspend () -> Unit) -> Unit> {
internal val RequestContextCachePhase: PipelinePhase = PipelinePhase("RequestContextCachePhase")
override fun install(
pipeline: ApplicationCallPipeline,
handler: suspend (ApplicationCall, suspend () -> Unit) -> Unit,
) {
pipeline.insertPhaseAfter(ApplicationCallPipeline.Plugins, RequestContextCachePhase)
pipeline.intercept(RequestContextCachePhase) {
handler(call, ::proceed)
}
}
}
class MyContextElement() : CoroutineContext.Element {
override val key: CoroutineContext.Key<*>
get() = MyContextElement
companion object : CoroutineContext.Key<MyContextElement>
}
chrisjenx
07/14/2025, 4:56 PM