Erlang Pattern: The Router

Hypothetical Labs - kevin - October 19, 2009

There are three components to this pattern. The client, the router, and the target. The client begins the process by calling the router with a message destined for a target. The router uses the message, and possibly other metadata, to locate a desirable target. Then the router hands off the message to the selected target. The target replies directly to the client and completes the process.

I’ve written this code a number of times in several different languages. What makes the Erlang implementation so nice is it’s brevity and simplicity. Using the gen_server behavior I can implement the core logic in just a few lines:

  1. Client calls the router with a message destined for a target:
    gen_server:call(?ROUTER, {invoke, Target, Msg})

  2. The router looks up the target and forwards the message on:

    handle_call({invoke, Target, Msg}, From, State) ->
      %% Target selection code goes here
      gen_server:cast(TargetPidOrName, {From, Msg}),
      {noreply, State};

  3. The target replies directly to the waiting client:

    handle_cast({Originator, Msg}, State) ->
      %% Server logic goes here
      gen_server:reply(Originator, Reply),
      {noreply, State};

The pretty bit is that all this plumbing can be hidden away inside a function. So the original call to the router in the client winds up looking like this: router:invoke_target(Target, Msg). The rest happens behind the scenes and appears as just another gen_server cal.

I’m pretty sure there are no bugs in using this approach. I’ve benchmarked a recent implementation of this pattern at over 6000 requests/sec without a hiccup.



Categories: Blogs  Hypothetical Labs  

Comments

anonymous avatar

I am pretty sure there are no bugs in using this approach.Cradlepoint

Posted by robert on 31 Mar 2010 at 19:57



 
anonymous avatar

The client, the router, and the target. 350-001 The client begins the process by calling the router with a message destined for a target. The router uses the message, and possibly other metadata, to locate a desirable target. Then the router hands off 642-681 the message to the selected target.VCP-410  The target replies directly to the client and completes the process.

Posted by roter on 05 Oct 2011 at 10:54



 
anonymous avatar

I just want to say that it is an elegant blog that have useful information. I will visit this site again for more information. Take care & carry this job of sharing nice information with others. burewadvi jgf dupinwan hu aokuneosrm dh automotiveja

Posted by dtyczka on 09 Nov 2011 at 07:46



 
anonymous avatar

Was pleased reading about!
research

Posted by Jack on 01 Dec 2011 at 12:36



 
anonymous avatar

I have been reading blogs on this subject for a few hours now and your content is the most informed I have read thus far. Thanks for putting this information iessay services reviews
one place.

Posted by top essay writing services on 04 Jan 2012 at 16:47



 
anonymous avatar

Post is nicely written and it contains many good things for me. I am glad to find your impressive way of writing the post. Now it become easy for me to understand and implement the concept. Thanks for sharing the post”ケラスターゼ

Posted by vindiesel on 06 Jan 2012 at 17:04



 
anonymous avatar

I completely agree with you. I really like this article. It contains a lot of useful information. I can set up my new idea from this post.”oakley eyewear

Posted by vindiesel on 15 Jan 2012 at 19:47



 
anonymous avatar

Very informative post thanks for share this with us i highly appreciate you for this information thanks once again for sharing information like this!

Essay writing UK

Posted by vindiesel on 17 Jan 2012 at 19:08



 


Add comment

Name:

Email:

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?