'after' in gen_server

Thu Mar 23 22:26:22 CET 2006

I want to do it from all callbacks.  Basically I want to make it so if 
the server is idle for X amount of time, then that means something is 
wrong and I need to do something to figure out what is wrong.

On Mar 23, 2006, at 11:04 AM, Rick Pettit wrote:

> On Thu, Mar 23, 2006 at 10:42:04AM -0500, orbitz@REDACTED wrote:
>> I am reworking a bit of code into gen_server pattern.  In this code I
>> have a typical loop construct.  In this I want to perform an action if
>> no messages have been receive in a certain amount of time, to do this 
>> I
>> simply have after sometimeout ->.  Is there any equivalence of this in
>> gen_server?
> If I understand you correctly, all you need to do is set a timeout when
> returning from one of the gen_server callback functions.
> For example, instead of returning {ok,State} from Module:init/1, return
> {ok,State,TimeoutMs}. You can do this for all the gen_server callback
> routines (at least handle_call/handle_cast, etc).
> This effectively sets a timer which will expire if no message is 
> received
> by the gen_server before TimeoutMs has elapsed. If/when the timer does
> expire, you receive a 'timeout' message in Module:handle_info/2.
> -Rick
> P.S. Since gen_server abstracts the server receive loop, I (as a 
> general rule
>      of thumb) *never* call receive in a gen_server callback module.
>> It has been suggested that I use a timer and record when the last
>> message has come in and when the timer signals check against that
>> message.  This seems a poor solution.  Another suggestions was to use 
>> a
>> gen_fsm with a timeout, but this seem a bit much just to get an 
>> 'after'
>> mechanism. Any other suggestions?  Perhaps my disregard of gen_fsm is 
>> a
>> bit hasty?

More information about the erlang-questions mailing list