I’m trying to implement concurrent system using ac...
# coroutines
k
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
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
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
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
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
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
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.