Erlang Pattern: The Router
Hypothetical Labs - kevin - October 19, 2009There 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:
-
Client calls the router with a message destined for a target:
gen_server:call(?ROUTER, {invoke, Target, Msg}) -
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};
-
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
I am pretty sure there are no bugs in using this approach.Cradlepoint
Posted by robert on 31 Mar 2010 at 19:57The 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:54I 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:46I 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.
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:04I 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:47Very informative post thanks for share this with us i highly appreciate you for this information thanks once again for sharing information like this!
Posted by vindiesel on 17 Jan 2012 at 19:08
Add comment
Erlang on Twitter
» mshiba64 (Masami Shibatani): ということで、ErlangのBit Syntaxに突入。language for distributed and concurrent programだからね。
» despenjahatdos (Jon champion): Eits jangan salah begini2 saya titisan dewa erlang RT @yolapitalokaa: Yg ngepost twit kyknya jg lg galau drtd ... http://t.co/QfCyVSIl
» erlangtriaji (erlang triaji ): Sini sun ahahaha RT @Encays: Udah udah, lo berduaan aja RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aj
» Encays (antarif cahyadi): Menjepit RT @erlangtriaji: Tegang! RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh
» erlangtriaji (erlang triaji ): Tegang! RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama banget
» Encays (antarif cahyadi): Udah udah, lo berduaan aja RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama
» revianh (Revian Hermansyah): Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama banget -_-
» erlangtriaji (erlang triaji ): Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama banget -_-
» Encays (antarif cahyadi): Udah, sama erlang aja RT @revianh: Nanggepnya lama banget -_-
» mshiba64 (Masami Shibatani): Erlangではシリアライズはterm_to_binaryというBuilt-in-functionで実行される。画像データもErlangで扱われるいくつかのTermも全てBinary型に可逆変換できる。
Statistics
Number of aggregated posts: 10454
Number of comments: 1392
Most recent article: January 31, 2012
Latest comments
» nobelboy on OpaDo Data Storage: Feel free to add some Qs here or contact me offline, and I will see what I can work into…
» darrensy on The Twisted Matrix: This has been a great idea you have shared. covers for kindle
» jony on Principle Software Engineer at LonoCloud (Full-time): That provides will become a internet marketer of little kinds of expert methods developers developing strategy using Erlang/OTP. There will…