hi there, good day everyone. I have a quick questi...
# coroutines
f
hi there, good day everyone. I have a quick question that is related to coroutines and webflux .. I dont know if here or #spring channel is the more appropriate place, so sorry if here is not the correct place. My question is (I guess) simple: I understand that I don’t need suspend functions when I’m return a Flow, as the suspension call are all internal and will be triggered only when we collect the flow …. Therefore, webflux endpoints that return Flow don’t need to be suspend functions … but, is there any drawback on making all endpoints suspend (even if it returns a flow and don’t call any other suspend function) ?
j
I would say that, if nothing else, at least it could be confusing. When a function is suspending and returns a flow, it might indicate that it does things right away and builds a hot
Flow
instead of the more usual cold flows that are triggered on collect. So if yours really is a cold flow, I would remove the
suspend
keyword. That being said, what I just described can only really be confusing for the caller of your function, but since you're talking about Spring controller functions (AFAICT), I guess you don't confuse anyone since Spring is the only one calling them 😄
f
I get your point and I agree with you .. The team I’m working with are, in general, new to coroutines (or reactive in general), and, coming from mvc world … I’m trying to make this a little more ‘gradual’ for them … making all suspend seems easier at first
j
I'm not sure it really makes things easier. It's actually quite easy to follow the simple rule: if the compiler doesn't force you to, don't make it suspend. Then all you have to do is ensure the low-level primitives (DB accesses, network calls etc) are marked suspend, and the rest will follow
f
you have a point.. I can maybe try that
j
In general I believe if people are new it's better to learn the correct approaches right away instead of using "beginner-only" approach and then have to change the frame of mind. Not seeing suspend will also spark the right questions - why is it not suspend? Why doesn't it need to? And from there they can learn and really understand what it means for flows to be cold
2
y
+1 - making it suspend makes it easy to do some work before you subscribe to the Flow. So you never really know whether calling a method returning a Flow is side effect free. It breaks some of the functional assumptions also.
In the worst case you end with people doing work immediately and returning flowOf(simpleReturnValue)
1
j
Yeah actually you're right it's not only harmful to the readers of the method but also for the implementers (especially if they are beginners)