https://kotlinlang.org logo
e

elizarov

03/15/2017, 8:07 PM
@uhe You are on a right track, but your solution is prone to resource exhaustion under load. Think about what is going to happen when you do
IncCounter
very often, but one of the subscribers is very slow. You can try it. You'll quickly run out of memory, because you
launch
new coroutine to send a message to each of your subscribers and, for the case of slow subscriber, they'll all just wait, consuming memory. A more scalable/reliable solution is to turn
sendCounterUpdate
into
supend fun
and remove
launch
from inside of it. Just send to each of the subscribers. This solution will properly propagate backpressure from the slow consumer to the original sender, making it scale to very high loads without exhausting resources.