I have a `Flow` of requests that need to be proces...
# coroutines
l
I have a
Flow
of requests that need to be processed by (many) different workers, and I was wondering which option is better from the following: 1. Have each worker apply
filter {  }
on the main
Flow
to check if this request belongs to it. 2. Have a separate
Flow
for each worker, and a general worker that checks all the main `Flow`'s requests and re-emit each request to the corresponding worker's
Flow
. 3. (possibly something I haven't thought of?)
d
Is each request processed by a single worker or by each worker?
l
A single worker, and it depends on a parameter in the request. Sorry if that wasn't clear.
s
Would adding the
filter
lambda as a function parameter be suitable?
That would enable each consumer to control the filter, which is more flexible
l
That's option 1. I'm just afraid going this direction would make the program less efficient, but I have no clue how flows work internally
a
If you have too many request, making all the subscribers filter might be such a good idea. Instead of filtering the request after subscription, why don't you filter the flows during subscription? that way, every collector will subscribe to their respective flows? Yes, it means you are gonna have to have multiple flows
l
So basically
filter {  }
then a terminal operation? Is that what you're suggesting each worker do?
a
No. say you have Flow<A>, Flow<B>, Flow<C>, then you define a method say,
fun <T> eventFlow(type: T) : Flow<T> = when(type
) { is A -> flow<A>{} is B -> flow<B>{} // . . . . }
l
I see, you're for option 2. I think that's what I'll end up using. Thanks!
a
No, your option 2, re-emits requests. I am suggesting to filter the flows themselves. Not the requests. Or did I miss something?
l
When you have an upstream flow, how do you use
when {  }
to decide which downstream flow it is going to without re-emitting it into that downstream flow? Unless I missed your point.
a
that is not an operator. It is just a when expression