Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  handle_cast v.s. handle_info in gen_server

bkl
Posted: Wed Feb 18, 2009 3:18 am Reply with quote
User Joined: 17 Aug 2008 Posts: 19
What is the difference between implementing an asynchronous message using handle_cast() v.s. handle_info() in gen_server? Similarly, there are handle_info() and handle_event() in gen_fsm. What should be the designing guideline of choosing one against another implementation? What's the justification of two separate API for handling asynchronous messaging?

Thanks in advance,

Bkl.
View user's profile Send private message
uwiger
Posted: Thu Feb 26, 2009 9:31 am Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
bkl wrote:
What is the difference between implementing an asynchronous message using handle_cast() v.s. handle_info() in gen_server? Similarly, there are handle_info() and handle_event() in gen_fsm. What should be the designing guideline of choosing one against another implementation? What's the justification of two separate API for handling asynchronous messaging?


The rule of thumb is that if you are casting to your own gen_server, you use gen_server:cast(). You use handle_info() to process messages whose format you cannot control, and there should be no unknown casts to your server.
View user's profile Send private message Visit poster's website
bkl
Posted: Sat Feb 28, 2009 7:51 pm Reply with quote
User Joined: 17 Aug 2008 Posts: 19
Wiger,

Thanks for the reply. Is this just a guideline, or there is some solid technical reason behind this "rule of thumb". For example, if my server is monitoring or linking w/ another process, then should handle_cast() or handle_info() process the exit signal (i.e.{'EXIT',...})?


Bkl.
View user's profile Send private message
uwiger
Posted: Sun Mar 01, 2009 12:21 am Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
bkl wrote:
Wiger,

Thanks for the reply. Is this just a guideline, or there is some solid technical reason behind this "rule of thumb". For example, if my server is monitoring or linking w/ another process, then should handle_cast() or handle_info() process the exit signal (i.e.{'EXIT',...})?


To be really honest, it's mainly my own rule of thumb. Wink

I just think it makes a lot of sense. The general guideline is to not let your protocol escape out of the module, i.e. you hide the calls and casts behind API functions. By extension, you shouldn't have any unknown casts. This leaves the messages that are either predefined (like timer messages, DOWN, EXIT, etc.) or just come to you out of the blue (in a robust system, it's a good idea not to crash on unknown messages). They all go in handle_info().

In other words (to answer your question), handle_cast() cannot deal with EXIT messages etc, since they are not tagged as casts: {'$gen_cast',Msg}. It only deals with messages sent using gen_server:cast/2 (or faked to look as such).
View user's profile Send private message Visit poster's website
bkl
Posted: Sun Mar 01, 2009 7:53 am Reply with quote
User Joined: 17 Aug 2008 Posts: 19
Wiger,

Got it. Thanks again.

Bkl.
View user's profile Send private message
wuji
Posted: Wed Aug 22, 2012 7:20 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
of unsuspecting malware-infected individuals without online access.What temporary Internet servers, servers, cheap jordans servers, you ask? They might have been connecting you to
YouTube, and -- ahem! -- ABCNews.com for the last month, month, cheap polo shirts month, and you didn't even know it. Really.Why is this
It all has to do with a piece of computer computer jordan 6s computer malware called DNS Changer.It started in 2007, when a
of hackers -- six Estonians and one Russian -- allegedly allegedly [h2]cheap replica *beep*[/h2] allegedly started masquerading as Internet advertisers who were paid by
click, according to an 2011 indictment from the U.S. Attorney Attorney cheap polo shirts Attorney General's Office in the Southern District of New York.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum