Sharing child processes across supervisors

Garrett Smith <>
Fri Mar 5 23:04:50 CET 2010


I have a gen_server that maintains a connection to something. I'd like
to have a single such gen_server per release (VM instance).

I generally run this server under a one_for_all supervisor -- anyone
who depends on that connection is also under this supervisor. When the
connection fails, the dependencies are all restarted.

If I have multiple OTP applications that share this connection, each
application will want to supervise the gen_server. I could merge the
supervisory trees of the multiple applications into one, but this
doesn't feel right at all - I want to keep the applications as
separate as possible.

I'm tempted to modify the start_link of the connection to look like this:

  start_link() ->
    case gen_server:start_link({local, ?SERVER}, ?MODULE, [], []) of
      {ok, Pid} -> {ok, Pid};
      {already_started, Pid} -> {ok, Pid};
      Other -> Other
    end.

My thinking is that this would fake out supervisors that subsequently
tried to start the connection, causing them to link and supervisor as
if they actually started it. Naively, it would seem that a connection
failure would be detected by all of the linked supervisors, triggering
the expected cascades. One of the applications would end up restarting
the connection and the rest would link per the "fake out" above.

Would this approach be bad for any reason? Is there a better or
standard way of getting supervision across applications?

Garrett


More information about the erlang-questions mailing list