How to 'bypass' the receive timer

Taavi Talvik <>
Fri Feb 8 16:12:51 CET 2002


On Fri, 8 Feb 2002, Lennart Öhman wrote:

Probably easiest is to use gen_server model and remember running timers
somewhere (ets tabel) a'la:

handle_call({do_something_useful, Params},  From, State )->
	do_something_useful,
	{ok, Tref} = timer:send_after(?TIMEOUT_VALUE,self(),{timeout,State#state.seq_no},
	ets:insert(State#state.timers, {State#state.seq_no, Tref}),
	Seq = State#state.seq + 1,
	{reply, Reply, State#state{seq_no=Seq});

handle_info({normal_answer, Seq_no},  State) ->
	[{_,Tref}] = ets:lookup(State#state.timers, Seq_no),
	ets:delete(State#state.timers,Seq_no),
	{ok, cancel} = timer:cancel(Tref),
	do_something_useful,
	{noreply, State};

handle_info({timeout,Seq_no}, State) ->
	[{_,Tref}] = ets:lookup(State#state.timers, Seq_no),
	ets:delete(State#state.timers,Seq_no),
	do_something_useful,
	{noreply,State};

best regards,
taavi


> it sounds to me that what you want to do is to start a set a timer
> in the timer process for each handled message which will be followed by
> a second message. Look in stdlib for timer.
> timer:send_after(Time,MessageIWillGetBack) for instance. If you get the
> expected message you either cancel the time-out or program in a way that
> you can ignore it when it eventually arrives.
>
> I guess the reason for going to a special receive statment (having the
> time-out) is that you need to handled the second message in a context
> dictated by the previous.
>
> You will achieve this by letting the tail-recursive function having the
> main receive statment have an argument where such relevant parameters
> for that context can be kept.
>
> Faustin Ngokse wrote:
> >
> > Hello erlang friends,
> >
> > This is my first contribution on this mailing list, I guess I am at the right
> > place.
> > I have an erlang process working as server and receiving a lot of messages.
> > Some messages trigger further "receive" with a timer to prevent the process from
> > waiting undefinetely.
> > It often happens that the messages destinated to the 'external' receive can't be
> > delivered because the timer for the 'internal' receiver is not yet over.
> > This make my server process very very slow.
> > Is there a possibility to influence the message so that messages for the
> > 'external' receive can be delivered while the 'internal' receive is still
> > waiting?
> > Do anybody have another idea how to solve this problem?
> >
> > Thanx in advance
> >
> > Faustin
>
> --
> -------------------------------------------------------------
> Lennart Ohman                   phone   : +46-8-587 623 27
> Sjoland & Thyselius Telecom AB  cellular: +46-70-552 6735
> Sehlstedtsgatan 6               fax     : +46-8-667 8230
> SE-115 28 STOCKHOLM, SWEDEN     email   : 
>

-----------------------------------------------------------
Taavi Talvik                    | Internet: 
AS Uninet                       | phone: +372 6800013
Parnu mnt. 105                  | fax: +372 6800001
Tallinn 11312, Estonia          | gsm: +372 56569996




More information about the erlang-questions mailing list