-
Notifications
You must be signed in to change notification settings - Fork 38.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consistent handling of coroutine context #32578
Comments
Hi, thanks for the detailed feedback and sorry for the delay, I think there is room for refinements indeed. I think we need to discuss to try to identify more focused individual refinements. Any chance you could provide focused repro(s) as a link to a repositiory or an attached project for the individual issues you are raising here? I am wondering if adding a
What do you mean by "EPs"? |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Alright, so as for the reproducer I have created something that you can refer to here:
Ah, sorry, EPs would stand for "endpoints" in this case. |
Essentially, I would expect that V1 implementations would work out of the box: when defined in the router like this. However, in reality to achieve context propagation between the filters I had to rewrite them like follows: And additionally set up a context provider on the coRouter level. And this context provider is basically called for each filter in the chain separately. |
Thanks for your detailed feedback, I will let you know when I have clarified what we can/can't do and when. |
Ran into another (mostly unrelated) problem with the coroutine context propagation and raised the following PR: #33548 |
While refactoring the filter chain used in one of our services, which is based on
Kotlin
,Spring Boot
,WebFlux
,coRouter
&coroutines
, I've run in the following scenario:observationRegistry.asContextElement()
needs to be added early on to the context, so that the observation from http request is correctly propagatedcoRouter
, some of the filters need to be only applied to some of the routes defined in the DSLHere are the facilities I'm aware of / was able to find, which seem to be relevant for the problem at hand:
CoWebFilter
fun filter(filterFunction: suspend (ServerRequest, suspend (ServerRequest) -> ServerResponse) -> ServerResponse)
inCoRouterFunctionDsl
fun context(provider: suspend (ServerRequest) -> CoroutineContext)
inCoRouterFunctionDsl
Now, here are the problems I've run into:
CoWebFilter
s would require making them all aware of which particular EPs to wrap and which to pass onfun filter(filterFunction)
fromCoRouterFunctionDsl
allows to apply these in some parts ofcoRouter
, but thesefilter
functions aren't picking up the context thatCoWebFilter
may have left inCOROUTINE_CONTEXT_ATTRIBUTE
.fun filter(filterFunction)
will not inherit context from one another and aren't able to modify the context that the actual handler will usefun context(provider)
is used, it's executed multiple times for 1 request. I.e. it will be called to create a context for each filter, and then for the corresponding handler.In the end, I've ended up with the following "magical" implementation:
which is at least able to meet our current needs, but it still has a problem that filters added this way would only be able to modify
coroutineContext
of one another by modifyingrequest.attributes()[CoWebFilter.COROUTINE_CONTEXT_ATTRIBUTE]
explicitly.IMO, Spring Framework could:
fun context(provider)
ofcoRouter
that would be executed early on and provide the context for the first `CoWebFilter in the chainfun context(provider)
should be executed bycoRouter
during handling of a single request (e.g. it could be for example used as a fallback to provide coroutineContext once, if by the time execution goes intocoRouter
code there was noCoWebFilter
invoked).Tested on: Spring Boot 3.2.4 / Spring 6.1.5
The text was updated successfully, but these errors were encountered: