kazuki tanida
02/05/2023, 11:44 AMCoroutineContext
has a ReactorContext
which wraps a context of reactor
.
In my case the context of reactor has two key/value pairs as the following image but I want to add one more pair to the current context mutably not immutable!Nick Zois
02/05/2023, 1:30 PMself
and spawned coroutines. So context modifications in the children may not be available to the parent.kazuki tanida
02/05/2023, 5:55 PMAdding information to the context make it available toI see. That make sense. 😢and spawned coroutines. So context modifications in the children may not be available to the parent.self
I put here an issue, if you face the same let me know, I am also trying to solve it in SpringbootWorld. Well, you can add extra info to the ReactorContext, but I faced issues when I call other suspend-able methods in springbootI also use Kotlin with Spring Boot (WebFlux) and in, which somehow eliminates the context that I ve added. I haven’t investigated more on that, but if you are in springboot you may face similar issues. e.g. Lets say that I have a rest-controller, and I add the user id and span id in the reactor’s context, if I call a method to a service component, the context keys have erased.@service
WebFilter
and I found I can add extra info to the context of Reactor which belongs to ReactorContext. (but I want to modify it after WebFilter…)
like this
@Component
class HttpRequestWebFilter : WebFilter {
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return chain.filter(exchange)
.contextWrite { ctx ->
ctx.put(SomeClass::class.java, someValue)
}
}
}
I found it works in suspending function in some bean class.
coroutineContext[ReactorContext].context.get(SomeClass::class.java)
// will result in someValue