Propagate CoroutineContext in CoWebFilter
This provides an elegant and dynamic way to customize the CoroutineContext in WebFlux with the annotation programming model. Closes gh-27522
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
package org.springframework.web.server
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.reactor.awaitSingleOrNull
|
||||
import kotlinx.coroutines.reactor.mono
|
||||
import reactor.core.publisher.Mono
|
||||
@@ -26,6 +28,7 @@ import reactor.core.publisher.Mono
|
||||
* using coroutines.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Sebastien Deleuze
|
||||
* @since 6.0.5
|
||||
*/
|
||||
abstract class CoWebFilter : WebFilter {
|
||||
@@ -34,6 +37,7 @@ abstract class CoWebFilter : WebFilter {
|
||||
return mono(Dispatchers.Unconfined) {
|
||||
filter(exchange, object : CoWebFilterChain {
|
||||
override suspend fun filter(exchange: ServerWebExchange) {
|
||||
exchange.attributes[COROUTINE_CONTEXT_ATTRIBUTE] = currentCoroutineContext().minusKey(Job.Key)
|
||||
chain.filter(exchange).awaitSingleOrNull()
|
||||
}
|
||||
})}.then()
|
||||
@@ -47,6 +51,12 @@ abstract class CoWebFilter : WebFilter {
|
||||
*/
|
||||
protected abstract suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain)
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmField
|
||||
val COROUTINE_CONTEXT_ATTRIBUTE = CoWebFilter::class.java.getName() + ".context"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user