https://kotlinlang.org logo
#coroutines
Title
# coroutines
n

nil2l

12/17/2018, 3:37 PM
Is there something ready-to-use for Throttling (skip some events for a while and handle only last one)? 🤔
m

marstran

12/17/2018, 3:39 PM
If your channel has a set capacity, then your senders could use
offer
. If the queue is full, then
offer
will return false and the message is dropped.
n

nil2l

12/17/2018, 3:42 PM
Ok. And what if I want to get it after some time (e.g. 1000 ms)? Capacity may be 1.
m

marstran

12/17/2018, 3:46 PM
You could send it to an actor, and have it forward the message after some delay. But wouldn't that just give you the same problem? It feels like you could just set the capacity to unlimited in that case.
It seems like you want to skip events, but actually not skip them anyway 🙂
n

nil2l

12/17/2018, 3:48 PM
and have it forward the message after some delay
But it must be canceled if new message received. And it doesn’t seem to be ready-to-use. 🙂
m

marstran

12/17/2018, 3:49 PM
Ah, so if the channel is full, you want the last element to be overwritten with the newest event?
d

dave08

12/17/2018, 3:52 PM
You should search the channel... this was asked before and a few implementations were proposed, I think 🙂
n

nil2l

12/17/2018, 3:56 PM
Right. Thanks.
b

bdawg.io

12/17/2018, 4:33 PM
This is called a Conflated channel iirc
d

dave08

12/17/2018, 4:35 PM
Nope, cause some values might be missed (depending on the state of the sender/receiver), he might want all the values, just at a specific exact interval.
s

streetsofboston

12/17/2018, 4:53 PM
It depends. If it is a ‘throttleLast’ type of throttling, a Conflated channel as the throttler in the middle can do the trick. It loops over the incoming values from the producer, sends the value to the next consumer in the pipe-line and then sleeps (delay) for a set number of milliseconds.
A ‘throttleFirst’ type of throttling would require a Channel with a capacity of one.
b

bdawg.io

12/17/2018, 5:52 PM
skip some events for a while and handle only last one
that sounds like a ConflatedChannel with an actor that receives after some delay
u

uliluckas

12/17/2018, 9:27 PM
Is this what you want? https://goo.gl/8zvJY2
8 Views