Hello everyone! I have a question about best prac...
# coroutines
v
Hello everyone! I have a question about best practices when using coroutines in a Spring MVC application. We are stuck on a version 2.3 for now and don't have access to coroutine support in the controller. So I use runBlocking in my controller methods, and call suspending functions in my services. Ultimately this will lead to blocking calls to another service. Is it better to run all coroutines including the one created by the runBlocking call with Dispatchers.IO by specifying it in the controller or should I just have the coroutines that make the blocking calls specify the dispatcher? I'm worried about testability mainly. Thanks!
j
I would usually use
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
closer to the point of the blocking call, unless you're making a bunch of blocking operations in a row, in which case you should wrap the whole thing in a single
withContext
to avoid excessive context switching. Note that using the IO dispatcher right from the controller would be a waste of threads, because Spring's threads will almost always be idle if you immediately switch to the IO pool
👍 1