Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the one-to-one supervisor in many of the cowboy examples

Tags:

erlang

cowboy

In the examples provided on the Cowboy github, and in some of the other examples I have found on-line, there is a one-to-one supervisor that does not seem to do anything. I even believe that I saw an example that had the following comment, "like a real supervisor doesn't do anything".

What is the purpose of the supervisor module that seems to be part of so many cowboy examples?

From the echo_get example:

%% Feel free to use, reuse and abuse the code in this file.

%% @private-module(echo_get_sup).
-behaviour(supervisor).

%% API.
-export([start_link/0]).

%% supervisor.
-export([init/1]).

%% API.
-spec start_link() -> {ok, pid()}.
 start_link() ->    
     supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% supervisor.
init([]) ->
    Procs = [], 
    {ok, {{one_for_one, 10, 10}, Procs}}.
like image 280
Jr0 Avatar asked Oct 05 '12 15:10

Jr0


2 Answers

You don't need to specify all the children that a supervisor manages when it starts. You can add/start them dynamically using supervisor:start_child/2 and manage them using supevisor:restart_child/2, supervisor:terminate_child/2 and supervisor:delete_child/2. This means that even if a supervisor has no children from the beginning it does not mean it is just a dummy.

The comment that a real supervisor "doesn't do anything" most likely relates to the fact that a supervisor that can only supervisor processes and not do any work like worker processes. Or at least that's it should mean!

like image 27
rvirding Avatar answered Sep 25 '22 14:09

rvirding


From erlang application behavior documentation:

start is called when starting the application and should create the supervision tree by starting the top supervisor. It is expected to return the pid of the top supervisor and an optional term State, which defaults to []. This term is passed as-is to stop.

He has this dummy supervisor, so he can call it at the end of the start function here. I think it has no practical purpose except to satisfy this condition.

like image 107
cashmere Avatar answered Sep 25 '22 14:09

cashmere