Support multiple CoWebFilter changing the context

This commit ensures CoWebFilter merges the exchange
CoroutineContext with the filter one if needed.

Closes gh-31792
This commit is contained in:
Sébastien Deleuze
2023-12-11 11:22:36 +01:00
parent e2c2268c39
commit d75a7c3818
2 changed files with 65 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.reactor.awaitSingleOrNull
import kotlinx.coroutines.reactor.mono
import reactor.core.publisher.Mono
import kotlin.coroutines.CoroutineContext
/**
* Kotlin-specific implementation of the [WebFilter] interface that allows for
@@ -34,7 +35,8 @@ import reactor.core.publisher.Mono
abstract class CoWebFilter : WebFilter {
final override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return mono(Dispatchers.Unconfined) {
val context = exchange.attributes[COROUTINE_CONTEXT_ATTRIBUTE] as CoroutineContext?
return mono(context ?: Dispatchers.Unconfined) {
filter(exchange, object : CoWebFilterChain {
override suspend fun filter(exchange: ServerWebExchange) {
exchange.attributes[COROUTINE_CONTEXT_ATTRIBUTE] = currentCoroutineContext().minusKey(Job.Key)