https://kotlinlang.org logo
Title
e

Esa

10/11/2019, 7:35 AM
Hi folks, I’m working on a pretty standard spring boot / kotlin app, where we’ve an api written in kotlin and a frontend in react. We just did our first coroutines here, and I’ve some questions about it. The usecase was to optimize a very slow function, and we did that by adding 3
CoroutineScope(EmptyCoroutineContext).async {}
calls on the 3 slowest components of that function, and awaiting the results at the end, as well as optimizing the order of the calls. However, this means that function and every function up to the controller /endpoint now has a
suspend
qualifier, as well as the controller having a
runBlocking {}
wrapped around the function (it doesn’t seem like a major issue to me, that endpoint has about a 3.5s runtime now).. And as this is my first foray into coroutines, I’m sort of anxious there’s some drawbacks to this I’m missing.
d

Dariusz Kuc

10/11/2019, 1:44 PM
have you looked into Spring WebFlux?
with latest spring you can just write your controller methods can be suspendable eliminating the need for
runBlocking
a

Alowaniak

10/11/2019, 7:18 PM
As an aside: are you sure concurrency is the best way to optimize the slow function?
m

Mani

10/11/2019, 9:52 PM
@Dariusz Kuc could you elaborate what that function is doing more in terms of cpu-bound or io-bound?
d

Dariusz Kuc

10/11/2019, 11:04 PM
@Mani which function?
runBlocking
? suspendable (i.e. reactive) controller methods?
e

Esa

10/14/2019, 7:28 AM
@Alowaniak No, you’re right. At some point we will want to take this problem at the root, and change up the underlying structure that causes us to feel the need for concurrency now. However, because business, there’s no time or prioritisation for that right now.