Sending and receiving messages

As we mentioned previously, vertices communicate with each other by exchanging messages. Sending the same message to all immediate neighbors of a particular vertex is an often recurring pattern in several graph algorithms. Let's define a convenience method for handling this fairly common use case:

BroadcastToNeighbors simply iterates the list of edges for a particular vertex and attempts to send the message to each neighbor with the help of the SendMessage method. With the help of SendMessage, compute functions can send a message to any vertex in the graph, provided that its ID is known to them (for example, discovered through the use of a gossip protocol).

Let's take a look at the implementation for SendMessage:

First things first, we need to look up the destination vertex in the graph's vertex map. If the lookup yields a valid Vertex instance, then we can enqueue the message so that it can be delivered to the vertex in the following super-step.

Things get a bit more interesting when the vertex lookup fails… A failed lookup can occur because of two reasons:

To handle vertices that are potentially hosted remotely, the Graph type allows users of the bspgraph package to register a helper that can relay messages between remote graph instances. More specifically, these helpers:

User-defined relay helpers must implement the Relayer interface and can be registered with a graph instance through the RegisterRelayer method:

To make it easier for users to provide functions or closures as a suitable Relayer implementation, let's also go ahead and define the RelayerFunc adapter, which converts a function with the appropriate signature into a Relayer:

If the destination vertex ID cannot be located by the graph and the user has registered a Relayer instance, SendMessage invokes its Relay method and checks the response for errors. If we get an error other than ErrDestinationLocal, we return the error as-is back to the caller.

If the relay helper detects that the destination vertex ID should, in fact, be managed by the local graph instance, it will fail with the typed ErrDestinationIsLocal error to indicate this. In such a case, we assume that the vertex ID is invalid and return the typed ErrInvalidMessageDestination error to the caller.