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

Kshitij Patil

03/14/2023, 5:16 AM
I’m trying to implement concurrent system using actor models. Not able to wrap my head around sending delayed message to actor. Attaching my snippet to implement the same. Is this correct?
u

uli

03/14/2023, 8:28 AM
Hi @Kshitij Patil why yould you need the delayActor? Sending msgs to the stateActor does not need to be serialized. Maybe a simple
launch
would do and make your life easier?
k

Kshitij Patil

03/14/2023, 8:32 AM
I showed state actor with default construct for simplicity. But I believe stateful actors need to be serialized and should not have access to launch async job as this might lead to concurrency issues. Have posted one proposal for stateful actor here https://github.com/Kotlin/kotlinx.coroutines/issues/87#issuecomment-1467456899
u

uli

03/14/2023, 8:43 AM
I was thinking about somthing along the line of:
Copy code
fun delayedIncrement() {
    scope.launch { 
        delay(5.seconds)
        stateActor.sendChannel.send(StateIntent.Increment)
    }
}
The issue with the delayActor is, that actors give you a queue but with the delay parameter, order of sending might be different from order of execution
k

Kshitij Patil

03/14/2023, 9:03 AM
Then you’re tying delayed jobs to parent scope. I was thinking if we should have separate scope for all the delayed messages. We can even make delayActor to
launch { }
new coroutine on every message.
u

uli

03/14/2023, 9:04 AM
scope
is supposed to be the scope that you also start the state actor on
why would you need an actor? the main job of an actor is serializing execution and sending messages does not need serialization
k

Kshitij Patil

03/14/2023, 9:14 AM
One idea was this could be stateless actor launching multiple child actors (Async Coroutines) to process single mailbox. I thought it would be better if we encapsulate this into separate class so that it doesn’t interfere with other state variables in the
System
I believe we don’t need queue for this actor, we can have single dispatch method which is launching async coroutines which means essentially not using
CoroutineScope.actor{ }
but have our own implementation. I’m just calling this as Actor as per definition.
19 Views