Got this working! Here’s the solution.
The thread started by @andrewarrow was incredibly useful in understanding the quirks. The solution I used was a mix of second and third. A read RPC for all messages to see duplicate would’ve been incredibly expensive. Hence, I sent the message out, and let it be dealt with by the receiver node.
I’ll share the corner cases, their possible explanations and the relevant fixes:
Corner Cases
- Sending message kept showing
no handler
error. This happened because the node broadcasting message sent it with typebroadcast
and the other node replied, but the current node didn’t handle it. A way around was to use node.RPC, and create empty handler function. - Sending message from n1 to other nodes creates an infinite loop because they’ll try sending the message back to n1 too. This is why unhandled cases saw a huge number of
broadcast 0
’s. They weren’t coming from the server. Instead, they happened becausen1 -> n2 -> n1 -> n2 -> ...
- Fix is to check for either
msg_id
, or find duplicate in array/ map where you stored message. If msg_id is missing or duplicate found in map, return nil. - In other cases, broadcast message to nodes that aren’t source or self.
- Fix is to check for either
In case something isn’t clear, please feel free to ask more questions.