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

Martin Devillers

10/25/2018, 4:55 PM
The fix is to use
start = CoroutineStart.UNDISPATCHED
, but that feels a bit weird
e

elizarov

10/25/2018, 5:16 PM
The proper fix is to use
send
instead of
offer
m

Martin Devillers

10/25/2018, 9:06 PM
The thing is, I’m sending a command to the actor, but I’d purposely like for the actor to drop this command if it’s busy. I don’t think I can accomplish this with
send
.
e

elizarov

10/25/2018, 9:06 PM
In your case the actor was busy starting up 🙂
You can create a buffered channel, though. Use
actor(capacity) { ... }
.
m

Martin Devillers

10/25/2018, 9:09 PM
Thanks for your response. I’ll explain my use-case a bit more. I’m handling some user actions, but sometimes the system sends a bunch of click events really fast. In that case, I’d like for the first one to be processed, but the following calls should be dropped since the first process is still running. If I use a buffered channel, I’ll end up processing the event multiple times because the events will become queued up.
e

elizarov

10/25/2018, 9:09 PM
That’s what conflated channels for
Use
actor(Channel.CONFLATED) { ... }
m

Martin Devillers

10/25/2018, 9:10 PM
But conflated still essentially has a capacity of 1. So if I re-send a command while one is already running, then it’ll re-run it after the first one is done.
e

elizarov

10/25/2018, 9:11 PM
If you resend, yes. But it conflates repeated into just one.
m

Martin Devillers

10/25/2018, 9:12 PM
I understand, but I’m looking for a behavior where sent events are simply ignored if the previous one is still being processed
e

elizarov

10/25/2018, 9:12 PM
I am not sure what exact behavior do you want. Just drop any commands while its running? That’s what default rendezvous done (and you are obviously not happy with it)
You can use default with undispatched start then.
m

Martin Devillers

10/25/2018, 9:13 PM
I am very happy with it actually 🙂 I’m just surprised that I had to use
UNDISPATCHED
for the initial command in order to make sure that it wasn’t dropped
e

elizarov

10/25/2018, 9:13 PM
(just exactly as you did)
m

Martin Devillers

10/25/2018, 9:13 PM
So we agree 😆
e

elizarov

10/25/2018, 9:13 PM
Because it has to start
(it is not receiving anything while it is starting, so that is why it gets dropped)
m

Martin Devillers

10/25/2018, 9:17 PM
Yes I guess I actually understand it now, but it just felt strange seeing that there was this “initialisation” which required special care. Thanks for taking the time to explain. I guess just have to wrap my head around the fact that even though
actor
returns a channel, it’s still just a couroutine builder, so it follows the same principles of asynchronously launching the coroutine which receives from the channel.
e

elizarov

10/25/2018, 9:17 PM
Yes. It is
3 Views