<https://kotlinlang.slack.com/archives/C0BJ0GTE2/p...
# spring
g
Hey Sebastien, when using coroutines with Spring MVC, should I expect my suspending code to always run on the same thread? Or every time I execute a suspending function, it might return in a different thread? The reason why I’m asking is to understand if it would break all those libraries that use
ThreadLocal
(like it happens with Webflux and Reactor)
s
Hey, Coroutines support in Spring MVC is based on Servlet async processing as described in https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web.html#mvc-ann-async, with differences betwen MVC and WebFlux explained in https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/web.html#mvc-ann-async-vs-webflux. Request and response processing could happen on a different thread, since internally the suspending function is converted to a
DeferredResult
but
ThreadLocal
should be fine for some use cases and not for others. could you share more about your use cases?
g
In my team, we have a small library to collect the context of a request and log it at the end of it, and because we know that the request is always on the same thread, we took the shortcut to use
ThreadLocal
to make it simple. The only extension is for the WebClient using the
subscriberContext
. I’d like for us to use coroutines, but before doing it I need to understand how much work it would take us, and how many of our libraries we can keep using or we have to rewrite
s
Are you using async requests today or not yet?
Threadlocal
are setup and cleaned via filters, see for example
RequestContextHolder
and
LocaleContextHolder
.
g
No, we are not using async requests today. I guess this is a problem also for people using MDC for logging, or opentracing agents, right? Is it something that Spring/Coroutines already solved somehow and that we can take inspiration from to refactor our library?
s
On WebFlux side we use Reactor/Coroutines context interop for that. Here we are in between 3 worlds since some interop between servlet async processing, Reactor and Coroutines context would be needed. Worth to experiment if you can.
g
Won't promise anything, but I will try 😅 in any case, thanks for the explanation 🙏
👍 2