Reinserting a message into a gen_server's mailbox

Juan Jose Comellas juanjo@REDACTED
Thu Jul 29 17:15:31 CEST 2010


I have a situation where for one specific gen_server:call/2 invocation I
have to perform an action that may take a few seconds (e.g. reading from a
DB; reading from external files). I don't want to block the gen_server
process while I do this, so I want to spawn a helper process that takes care
of reading the data I need. The problem is that I can receive several
gen_server:call/2 invocations that depend on this data being present at the
same time and I don't want to have them fail because the data is not yet
ready. I've thought of two ways of solving this problem:

1) The "correct" one: keep the list of requests that come in while the data
is not ready and once I have the data in the helper process, notify the
gen_server process and do a gen_server:reply/2 with the response for each of
the pending requests.

2) The easy (non-kosher) one: while the data is not ready reinsert all the
requests (that depend on the data) that come into the gen_server process
back into its own mailbox (maybe with a little delay) and hope that when
they are processed again the data will be ready.

Does anybody see a problem with option 2? As far as I've seen, reinserting a
gen_server:call/2 request into the process' mailbox would be kind of a hack,
because according to the gen module, these messages have the following
format:

{'$gen_call', {pid(), ref()}, Request :: any()}

Does this format ever change? Can I rely on it not to change in the future?

Thanks,

Juanjo.


More information about the erlang-questions mailing list