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}}.
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!
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 termState
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With