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
No comments so far, you could be the first.Add comment
Erlang on Twitter
» KijijiJobs (KijijiJobs): Bay Area: Database Architect Erlang CouchDB Greenplum MySQL - (San Francisco) http://bit.ly/cQEJwb #Kijiji #Jobs
» KijijiJobs (KijijiJobs): Bay Area: Database Architect Erlang CouchDB Greenplum MySQL - (San Francisco) http://bit.ly/djetPm #Kijiji #Jobs
» robertbrook (Robert Brook): RT @sheysrebellion: sweet! Joe Armstrong’s library of Erlang code on GitHub! http://post.ly/SCnB
» KijijiJobs (KijijiJobs): Bay Area: Backend Developer MYSQL PHP LAMP Erlang CouchDB - (San Francisco) http://bit.ly/c2B7U9 #Kijiji #Jobs
» sldfjd (sldfjd ldajds): [from the.ruediger] joearms’s elib1 - “An Erlang library and collection of applications”: http://url4.eu/1l7Bo
» lhuga (Satoka F / る~が): @MiCHiLU イケメン課長のいる会社かーw つか Erlang そんな今盛り上がってんの? いつから?
» MiCHiLU (Takanao Endoh ): @lhuga やるとうちの会社に入れるかも。 http://www.erlang-factory.com/ とか儲かりそうじゃね?
» lhuga (Satoka F / る~が): てゆーかそこで Erlang なのかw
» balachandarkm (Balachandar): successfully installed erlang otp :-)
» SrPablo (Paul Meier): Erlang: music =:= dangerous. Java: music.equals(dangerous). Scheme: (eq? ‘music ‘dangerous). Okay that’s enough ^_^
Statistics
Number of aggregated posts: 9911
Number of comments: 380
Most recent article: March 11, 2010
Latest comments
» Jens on Eleven Years of Erlang: Hi, The link to how you started with Erlang is broken. Regards, Jens
» Jeff Martens on It Made Sense in 1978: I agree that word size is machine specific, but it becomes language-specific once it’s in a language definition. In Java an …
» Adley on XP Day Suisse 2009: What a great post. What an inspiration for everyone who is asking ‘Where is all this stuff I’ve asked for?’ and …