[erlang-questions] gen_fsm and keepalive timer

Michael Santos <>
Thu Dec 29 19:35:25 CET 2011


On Thu, Dec 29, 2011 at 06:51:44PM +0100, Ivan Ostres wrote:
> Hi all!
> 
> I am trying to implement FSM of a protocol inside gen_fsm behavior.
> For a keepalive purpose I need a timer that would produce event when
> timer is expired so I could know when to send a keepalive packet and
> to know when holdtime is gone (I didn;t receive keepalive packet
> from other side). I was trying to get more info on
> gen_fsm:start_timer but documentation is a bit sparse - it's clear
> how to create timer but not how to catch it's event (well, at least
> to me since this is first time I am using gen_fsm).
> 
> If someone could explain this in few sentences I would be very
> grateful. Also if there is a better way for keepalive mechanism
> inside gen_fsm, I would like to know it.
> 
> Thanks and regards,
> Ivan Ostres

Using the optional timeout in gen_fsm states might be simpler to use:

-record(state, { timeout = 1000 }).

my_state(Data, #state{timeout = Timeout} = State) ->
    handle_protocol(Data),
    {next_state, my_state, State, Timeout};
my_state(timeout, #state{timeout = Timeout} = State) ->
    keepalive(),
    {next_state, my_state, State, Timeout}.



More information about the erlang-questions mailing list