[erlang-questions] gen_server:call and hibernate

Ulf Wiger <>
Thu May 19 11:43:30 CEST 2011


On 19 May 2011, at 02:05, ag3nt911 wrote:

> I want to be able to hibernate the calling process after making a call to
> the gen_server process but I can't seem to think of or know how to work
> around this in an elegant way. I'm also not sure where to begin to hack
> gen.erl to do this if it is possible.

I presume your client expects to wait a very long time then? :)

The "throws away the call stack" part is important to consider. Normally, you'd want a gen_server:call/2 to raise debuggable exceptions if something goes wrong. Hibernating in this situation would render that very difficult.

Given that hibernate/3 messes with some core semantics (exception handling semantics) in Erlang, it should be used sparingly, and only as an optimisation in cases where reducing memory footprint is of the essence.

Having said this, you can do what you ask without hacking gen.erl.

Either "fake" a gen:call() message: {'$gen_call', {self(), Ref}, Request},
and wait for the reply, {Ref, Reply}. Not hard, but generally, one shouldn't cheat,

or send a cast to the server and wait for an agreed reply.

A basic gen_server-like call is really very simple:

call(Server, Req) ->
   Ref = erlang:monitor(process, Server),
   Server ! {call, {self(), Ref}, Req},
   receive
      {Ref, Reply} -> Reply;
      {'DOWN', Ref, _, _, Reason} -> error(Reason)
   end.

BR,
Ulf W

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com






More information about the erlang-questions mailing list