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

2

5

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}}.
Adz answered 5/10, 2012 at 15:57 Comment(0)
T
6

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.

Thereupon answered 5/10, 2012 at 19:41 Comment(0)
A
1

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!

Adrian answered 6/10, 2012 at 0:44 Comment(1)
I see no evidence that any start_child/2, etc. functions are being called. I suspect the answer that cashmere @Thereupon gave is more likely to be the case. Of course, I could definitely be missing something...Adz

© 2022 - 2024 — McMap. All rights reserved.