[erlang-questions] Dynamic OTP Children and simple_one_for_one

Francesca Gangemi francesca.gangemi@REDACTED
Mon Oct 19 17:42:06 CEST 2009

Hi Mark,

mark peleus wrote:
> Hi,
> My OTP application need to create a gen_server for every user when he
> connects to the server.
> I think the supervisor should be of type simple_one_for_one because all the
> children will be instances of the same gen_server.
> I don't understand what should be the myapp_sup:init/1 function.
> It suppose to return {ok, {SupervisorSpecification, ChildSpecificationList}}
> In my case I don't have connected users yet when starting the application so
> ChildSpecificationList is empty?

Even though you don't have any connected users the child specification
list should not be empty.
Your init function should look like the following one

init(_Args) ->
    Child_Spec = [{my_server,{my_server,start_link,[]},
                   temporary, 5000, worker, [my_server]}],
    {ok,{{simple_one_for_one,3,1}, Child_Spec}}.

You can change same values as Restart (temporary) or Shutdown (5000) to
better suit your application.
When started, the supervisor will not start any child processes.

> When a user connects to the server I need to create a gen_server for him so
> I'll use:
> supervisor:start_child(SupervisorName, ChildSpec)
> SupervisorName is the pid or the registered supervisor name.
> Do I need to explicitly register the supervisor or is it registered
> automatically in myapp_sup:init/1 ?
You don't need to register the supervisor.
If the supervisor is started by calling

start_link() ->
    supervisor:start_link({local, myapp_sup}, myapp_sup, []).

it will be registered as myapp_sup and you can then add children
dynamically by calling

supervisor:start_child(myapp_sup, []).

If you want you can also specify a list of arguments that will be passed
to the start function specified in the child specification.

supervisor:start_child(myapp_sup, [A, B, C]).

results in

apply(my_server, start_link, [A,B,C]).

Kind regards

Francesca Gangemi, francesca@REDACTED
Erlang Training and Consulting

More information about the erlang-questions mailing list