[erlang-questions] Timeouted gen_server:call
Bernard Duggan
bernie@REDACTED
Thu May 13 01:21:22 CEST 2010
On 13/05/10 00:48, Dmitry Belyaev wrote:
> I have a gen_server and many clients. Clients send request to the server
> with gen_server:call(Server, Msg, Timeout) and when timeout occurs they
> ask server to abort appropriate reply. But server may reply right before
> it receives abort message and I may get trash in client's mailbox.
>
> I'm not sure that client can throw away this trash itself. So it is
> possible for client to fill his message queue with lots of such messages.
>
Why can't the client clean up its queue itself? A general rule of
thumb, in my experience at least, is that processes should flush their
message queue of any "unexpected" messages - if not all the time, at
least regularly. Not because they /should/ expect to get them, but
because you don't want them to misbehave if they do (you're right to be
concerned about a growing queue - as several people on this list, myself
included, have discovered it can be "bad" when a queue gets too big).
In the case of a gen_server and similar, this is easy - just add a
catch-all
handle_info(_, State) -> {noreply, State}
In the case of non "gen style" code, just make sure it regularly reaches
and loops on a 'receive' block which has a clause which matches
everything you're not interested in and discards it.
Cheers,
Bernard
More information about the erlang-questions
mailing list