Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supervisor/gen spawn options #9256

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/ssl/src/tls_dyn_connection_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
-behaviour(supervisor).

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

%% Supervisor callback
Expand All @@ -41,8 +41,9 @@
%%%=========================================================================
%%% API
%%%=========================================================================
start_link() ->
supervisor:start_link(?MODULE, []).
-spec start_link(Options :: [gen_server:start_opt()]) -> supervisor:startlink_ret().
start_link(Options) ->
supervisor:start_link(?MODULE, [], Options).

start_child(Sup, sender, Args) ->
supervisor:start_child(Sup, sender(Args));
Expand Down
15 changes: 8 additions & 7 deletions lib/ssl/src/tls_gen_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@
start_fsm(Role, Host, Port, Socket, {SSLOpts, _, _Trackers} = Opts,
User, CbInfo, Timeout) ->
ErlDist = maps:get(erl_dist, SSLOpts, false),
SupOpts = maps:to_list(maps:with([timeout, debug, hibernate_after, spawn_opt], SSLOpts)),
SenderSpawnOpts = maps:get(sender_spawn_opts, SSLOpts, []),
SenderOptions = handle_sender_options(ErlDist, SenderSpawnOpts),
Starter = start_connection_tree(User, ErlDist, SenderOptions,
Starter = start_connection_tree(User, ErlDist, SenderOptions, SupOpts,
Role, [Host, Port, Socket, Opts, User, CbInfo]),
receive
{Starter, {ok, SockReceiver}} ->
Expand All @@ -96,10 +97,10 @@ handle_sender_options(ErlDist, SpawnOpts) ->
[[{spawn_opt, SpawnOpts}]]
end.

start_connection_tree(User, IsErlDist, SenderOpts, Role, ReceiverOpts) ->
start_connection_tree(User, IsErlDist, SenderOpts, SupOpts, Role, ReceiverOpts) ->
StartConnectionTree =
fun() ->
try start_dyn_connection_sup(IsErlDist) of
try start_dyn_connection_sup(IsErlDist, SupOpts) of
{ok, DynSup} ->
case tls_dyn_connection_sup:start_child(DynSup, sender, SenderOpts) of
{ok, Sender} ->
Expand All @@ -126,10 +127,10 @@ start_connection_tree(User, IsErlDist, SenderOpts, Role, ReceiverOpts) ->
end,
spawn(StartConnectionTree).

start_dyn_connection_sup(true) ->
tls_connection_sup:start_child_dist([]);
start_dyn_connection_sup(false) ->
tls_connection_sup:start_child([]).
start_dyn_connection_sup(true, Opts) ->
tls_connection_sup:start_child_dist([Opts]);
start_dyn_connection_sup(false, Opts) ->
tls_connection_sup:start_child([Opts]).

socket_control(SslSocket, Timeout) ->
case ssl_gen_statem:socket_control(SslSocket) of
Expand Down
3 changes: 2 additions & 1 deletion lib/ssl/src/tls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ call(Pid, Msg) ->

start_tls_server_connection(SslOpts, Port, Socket, EmOpts, Trackers, CbInfo) ->
try
{ok, DynSup} = tls_connection_sup:start_child([]),
SupOpts = maps:to_list(maps:with([timeout, debug, hibernate_after, spawn_opt], SslOpts)),
{ok, DynSup} = tls_connection_sup:start_child([SupOpts]),
SenderOpts = maps:get(sender_spawn_opts, SslOpts, []),
{ok, Sender} = tls_dyn_connection_sup:start_child(DynSup, sender, [[{spawn_opt, SenderOpts}]]),
ConnArgs = [server, Sender, "localhost", Port, Socket,
Expand Down
31 changes: 24 additions & 7 deletions lib/stdlib/src/supervisor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ but the map is preferred.
-behaviour(gen_server).

%% External exports
-export([start_link/2, start_link/3,
-export([start_link/2, start_link/3, start_link/4,
start_child/2, restart_child/2,
delete_child/2, terminate_child/2,
which_children/1, which_child/2,
Expand Down Expand Up @@ -530,12 +530,28 @@ start_link(Mod, Args) ->
-doc """
Creates a supervisor process as part of a supervision tree.

The supervisor can be nameless or registered,
and it can receive options or none at all.
""".
-spec start_link(SupNameOrModule, ModuleOrArgs, ArgsOrOptions) -> Return when
SupNameOrModule :: sup_name() | module(),
ModuleOrArgs :: module() | term(),
ArgsOrOptions :: term() | [gen_server:start_opt()],
Return :: startlink_ret().
start_link(Mod, Args, Options) when is_atom(Mod), is_list(Options) ->
gen_server:start_link(supervisor, {self, Mod, Args}, Options);
start_link(SupName, Mod, Args) when is_tuple(SupName), is_atom(Mod) ->
gen_server:start_link(SupName, supervisor, {SupName, Mod, Args}, []).

-doc """
Creates a supervisor process as part of a supervision tree.

For example, the function ensures that the supervisor is linked to the calling
process (its supervisor).

The created supervisor process calls [`Module:init/1`](`c:init/1`) to find out
about restart strategy, maximum restart intensity, and child processes. To
ensure a synchronized startup procedure, `start_link/2,3` does not return until
ensure a synchronized startup procedure, `start_link/2,3,4` does not return until
[`Module:init/1`](`c:init/1`) has returned and all child processes have been
started.

Expand Down Expand Up @@ -571,13 +587,14 @@ started.
processes with reason `shutdown` and then terminate itself and returns
`{error, {shutdown, Reason}}`.
""".
-spec start_link(SupName, Module, Args) -> startlink_ret() when
-spec start_link(SupName, Module, Args, Options) -> startlink_ret() when
SupName :: sup_name(),
Module :: module(),
Args :: term().
start_link(SupName, Mod, Args) ->
gen_server:start_link(SupName, supervisor, {SupName, Mod, Args}, []).

Args :: term(),
Options :: [gen_server:start_opt()].
start_link(SupName, Mod, Args, Options) ->
gen_server:start_link(SupName, supervisor, {SupName, Mod, Args}, Options).

%%% ---------------------------------------------------
%%% Interface functions.
%%% ---------------------------------------------------
Expand Down
13 changes: 11 additions & 2 deletions lib/stdlib/test/supervisor_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
middle9212/0, gen_server9212/0, handle_info/2, start_registered_name/1, log/2]).

%% API tests
-export([ sup_start_normal/1, sup_start_ignore_init/1,
-export([ sup_start_normal/1, sup_start_ignore_init/1, sup_start_gen_flags/1,
sup_start_ignore_child/1, sup_start_ignore_temporary_child/1,
sup_start_ignore_temporary_child_start_child/1,
sup_start_ignore_temporary_child_start_child_simple/1,
Expand Down Expand Up @@ -125,7 +125,7 @@ all() ->

groups() ->
[{sup_start, [],
[sup_start_normal, sup_start_ignore_init,
[sup_start_normal, sup_start_ignore_init, sup_start_gen_flags,
sup_start_ignore_child, sup_start_ignore_temporary_child,
sup_start_ignore_temporary_child_start_child,
sup_start_ignore_temporary_child_start_child_simple,
Expand Down Expand Up @@ -231,6 +231,15 @@ sup_start_ignore_init(Config) when is_list(Config) ->
ignore = start_link(ignore),
check_no_exit(100).

%%-------------------------------------------------------------------------
%% Tests what happens if init-callback returns ignore.
sup_start_gen_flags(Config) when is_list(Config) ->
process_flag(trap_exit, true),
Ret = {ok, {{one_for_one, 2, 3600}, []}},
{ok, Pid} = supervisor:start_link(?MODULE, Ret, [{hibernate_after, 0}]),
check_no_exit(100),
terminate(Pid, shutdown).

%%-------------------------------------------------------------------------
%% Tests what happens if init-callback returns ignore.
sup_start_ignore_child(Config) when is_list(Config) ->
Expand Down
Loading