Skip to content

Routers

Paul Louth edited this page Feb 20, 2017 · 2 revisions

Routers are proxy processes. They have their own inbox, their own failure strategies, etc. A router simply receives messages and then forwards them on based on what type of router it is. There are two 'flavours' of routing. One is where the router itself creates N child processes and distributes the messages it receives to those child processes. The other is where you give it a set of ProcessIds and the router does the same. The benefit of you giving it a set of ProcessIds is that they could be anywhere in the cluster, whereas the ones created by a router will always be in the same AppDomain.

The standard set that are provided in LanguageExt are:

Name Behaviour
Broadcast Sends any received message to all routees
Least-busy Finds the routee with the smallest mailbox and forwards the message to it
Round-robin Sends any received message to the routees in turn
Random Sends any received message to a random routee

Here's a basic example of spawning a broadcast router that has 10 workers:

    var router = Router.broadcast("router", 10, (string msg) => Console.WriteLine(msg));
    tell(router, "Hello");

All built-in routers have variants for accepting a collection of routees instead, but also variants that perform mapping. For example with Router.broadcast(...) there's also:

    Router.broadcastMap(...)
    Router.broadcastMapMany(...)

The Map variant will project the input message into another type before passing it on to the routees. The MapMany variant will project the input message into an enumerable, and it will route those items in the enumerable as though it had received the items as messages through its inbox.

Routers are powerful as gate-keepers to more complex systems - and are very much about the receiver taking control over the incoming messages, but they can also be a bottle-neck in their own right, because there is one router inbox that is doing its best to allocate the messages to routees as quickly as possible; if you have a particularly high rate of incoming messages then it may not be the best approach, see dispatchers for how this routing responsibility can be moved to the sender.

Clone this wiki locally