Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# cyclic dependencies with message processing

I'm trying to build system using actor model with complex logic across message passing, so there is an actor that sends message from start and listens for incoming responses. Also, message passing done using, for example, UDP datagrams.

So, given actor depends on message sender which means that system must create message sender firstly. In order to pass incoming messages to actor, message listener must depend on actor (it does something like actor.Post incomingMessage).

The question is about initialization order: seems like i should create sender first, then actor and, at the end listener, but if system will be built this way, there is a potential incoming message loss when actor already has sent an message and listens for reply, but listener is not initialized yet. Of cource, UDP is not reliable transport too and may lost some messages, therefore there is some mechanisms to prevent system failure. But is seems like general design is broken in my case. Is there any solutions or, maybe, different design principles?

like image 569
Shishkin Pavel Avatar asked Jul 26 '26 05:07

Shishkin Pavel


1 Answers

Typically you sort out your mechanisms to respond to messages before you do anything which could cause messages to start arriving. So if you're only expecting them to appear if you've already sent certain messages, don't send any of those messages until after you've configured the listener.

The actor should wait until everything is in place before it starts its normal operations.

It's not always possible to be 100% sure about this of course, because you usually can't tell if anything else is listening on a message bus which you might be expecting to be listening, but if this is important it's not unreasonable to define some messages which various actors can use to verify that other actors are ready to go before things kick off.

Alternatively, you can use a message bus implementation which stores messages until a listener actively retrieves them. This allows messages to be sent without a listener present without loss, but there can be issues with the size of the message queue increasing if listeners aren't retrieving them.

like image 167
Matthew Walton Avatar answered Jul 28 '26 14:07

Matthew Walton