finger exercises: port registry

In which I try to figure out a sensible pattern for broadcasting messages among a set of isolates.

The main issue is that isolates can not discover and send messages to each other so I need some kind of broker and a protocol for isolates to register themselves and send broadcast messages

The hash map is dynamic because there are no restrictions on what registrants can ask to save for a given ID (other than whatever is serializable across the port boundary). There is the implicit assumption that the entry is a dictionary-like object with at least a port entry so that messages can be propagated to the isolate that asked to be registered. Otherwise the registrants are not following the proper protocol and there is nothing to do really hence the try/catch and removal of any offending entries. I don’t know what happens to the sending port of an isolate that has died. I’m assuming it will throw an exception and get cleaned up but I’m probably wrong.

The registry will need a way to receive requests so I need some kind of message class and the corresponding receiving port and dispatch logic in the registry

I’m gonna let the request instance handle the actual command logic. I don’t really know what this pattern is called but it seems to fit the use case pretty well.

I think there is enough here to fill-in some of the command handling

Now some test code to exercise the code paths

Each spammer registers and then broadcasts a message. The output is what I expect it to be

The broadcasting is happening as I expect it to so I want to see what happens when a crashing isolate is added to the registry

After running the above it looks like nothing bad happens when there is a port that has a dead end for the corresponding receiver. I guess the message just goes into the void. These are truly low level primitives.

There are a few ways around this. I can add an error listener and figure out which entry from the registry to delete when there is an error. I can send a ping and expect a response back in a certain amount of time. Neither one seems good. The error listener I don’t even know how to connect back to the registry entry and the ping approach means some kind of timeout and I can orphan isolates simply because they took too long to process a ping request. The last one I can think of is add an error listener that just crashes the entire process if any of the isolates in the registry crash. I’m really starting to wish for bidirectional process linking right about now and starting to see the wisdom in Erlang’s process design. Somehow the whole thing feels way too manual in Dart.