Skip to content

Commit

Permalink
ssl: Use supervisor:which_child/2 to improve start up
Browse files Browse the repository at this point in the history
  • Loading branch information
IngelaAndin committed Dec 20, 2024
1 parent e76656e commit 1683519
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lib/ssl/src/dtls_connection_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ init(_) ->
period => 3600
},
ChildSpecs = [#{id => undefined,
start => {ssl_gen_statem, start_link, []},
start => {ssl_gen_statem, dtls_start_link, []},
restart => temporary,
shutdown => 4000,
modules => [ssl_gen_statem, dtls_connection],
Expand Down
2 changes: 1 addition & 1 deletion lib/ssl/src/ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2454,7 +2454,7 @@ handshake(Socket, SslOptions, Timeout)
ok = tls_socket:setopts(Transport, Socket, tls_socket:internal_inet_values()),
{ok, Port} = tls_socket:port(Transport, Socket),
{ok, SessionIdHandle} = tls_socket:session_id_tracker(ssl_unknown_listener, SslOpts),
tls_gen_connection:start_fsm(server, "localhost", Port, Socket,
tls_gen_connection:start_fsm(Port, Socket,
{SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{}),
[{session_id_tracker, SessionIdHandle}]},
Expand Down
18 changes: 10 additions & 8 deletions lib/ssl/src/ssl_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
-include("tls_connection.hrl").

%% Initial Erlang process setup
-export([start_link/7,
start_link/8,
-export([tls_start_link/7,
dtls_start_link/7,
init/1]).

%% TLS connection setup
Expand Down Expand Up @@ -108,26 +108,26 @@
%%% Initial Erlang process setup
%%--------------------------------------------------------------------
%%--------------------------------------------------------------------
-spec start_link(client| server, pid(), ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
-spec tls_start_link(client| server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
{ok, pid()} | ignore | {error, ssl:reason()}.
%%
%% Description: Creates a process which calls Module:init/1 to
%% choose appropriat gen_statem and initialize.
%%--------------------------------------------------------------------
start_link(Role, Sender, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo) ->
tls_start_link(Role, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo) ->
ReceiverOpts = maps:get(receiver_spawn_opts, SslOpts, []),
Opts = [link | proplists:delete(link, ReceiverOpts)],
Pid = proc_lib:spawn_opt(?MODULE, init, [[Role, Sender, Host, Port, Socket, Options, User, CbInfo]], Opts),
Pid = proc_lib:spawn_opt(?MODULE, init, [[Role, self(), Host, Port, Socket, Options, User, CbInfo]], Opts),
{ok, Pid}.

%%--------------------------------------------------------------------
-spec start_link(atom(), ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
-spec dtls_start_link(atom(), ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
{ok, pid()} | ignore | {error, ssl:reason()}.
%%
%% Description: Creates a gen_statem process which calls Module:init/1 to
%% initialize.
%%--------------------------------------------------------------------
start_link(Role, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo) ->
dtls_start_link(Role, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo) ->
ReceiverOpts = maps:get(receiver_spawn_opts, SslOpts, []),
Opts = [link | proplists:delete(link, ReceiverOpts)],
Pid = proc_lib:spawn_opt(?MODULE, init, [[Role, Host, Port, Socket, Options, User, CbInfo]], Opts),
Expand All @@ -138,9 +138,11 @@ start_link(Role, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo) ->
-spec init(list()) -> no_return().
%% Description: Initialization
%%--------------------------------------------------------------------
init([Role, Sender |[Host, Port, _Socket, {TLSOpts, _, _}, _User, _CbInfo] = InitArgs]) ->
init([Role, Sup | [Host, Port, _Socket, {TLSOpts, _, _}, _User, _CbInfo] = InitArgs]) ->
process_flag(trap_exit, true),

{ok, {_, Sender,_,_}} = supervisor:which_child(Sup, sender),

case maps:get(erl_dist, TLSOpts, false) of
true ->
process_flag(priority, max);
Expand Down
18 changes: 6 additions & 12 deletions lib/ssl/src/tls_dyn_connection_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,27 @@
-behaviour(supervisor).

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

%% Supervisor callback
-export([init/1]).

%%%=========================================================================
%%% API
%%%=========================================================================
start_link() ->
supervisor:start_link(?MODULE, []).
start_link(SenderArgs, ReciverArgs) ->
supervisor:start_link(?MODULE, [SenderArgs, ReciverArgs]).

start_child(Sup, sender, Args) ->
supervisor:start_child(Sup, sender(Args));
start_child(Sup, receiver, Args) ->
supervisor:start_child(Sup, receiver(Args)).

%%%=========================================================================
%%% Supervisor callback
%%%=========================================================================
init(_) ->
init([SenderArgs, ReciverArgs]) ->
SupFlags = #{strategy => one_for_all,
auto_shutdown => any_significant,
intensity => 0,
period => 3600
},
ChildSpecs = [],
ChildSpecs = [sender(SenderArgs), receiver(ReciverArgs)],
{ok, {SupFlags, ChildSpecs}}.

sender(Args) ->
Expand All @@ -74,7 +68,7 @@ receiver(Args) ->
restart => temporary,
type => worker,
significant => true,
start => {ssl_gen_statem, start_link, Args},
start => {ssl_gen_statem, tls_start_link, Args},
modules => [ssl_gen_statem,
tls_client_connection,
tls_server_connection,
Expand Down
72 changes: 27 additions & 45 deletions lib/ssl/src/tls_gen_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
-include("tls_record_1_3.hrl").

%% Setup
-export([start_fsm/8,
-export([start_fsm/6,
start_fsm/7,
pids/1,
initialize_tls_sender/1]).

Expand Down Expand Up @@ -72,20 +73,28 @@
%%====================================================================
%% Setup
%%====================================================================
start_fsm(Role, Host, Port, Socket, {SSLOpts, _, _Trackers} = Opts,
start_fsm(Host, Port, Socket, {SSLOpts, _, _Trackers} = Opts,
User, CbInfo, Timeout) ->
ErlDist = maps:get(erl_dist, SSLOpts, false),
SenderSpawnOpts = maps:get(sender_spawn_opts, SSLOpts, []),
SenderOptions = handle_sender_options(ErlDist, SenderSpawnOpts),
Starter = start_connection_tree(User, ErlDist, SenderOptions,
Role, [Host, Port, Socket, Opts, User, CbInfo]),
receive
{Starter, {ok, SockReceiver}} ->
receive {SockReceiver, user_socket, UserSocket} ->
socket_control(UserSocket, Timeout)
end;
{Starter, Error} ->
Error
{ok, DynSup} = start_connection_tree(ErlDist, SenderOptions,
[client, Host, Port, Socket, Opts, User, CbInfo]),
{ok, {_, Receiver,_,_}} = supervisor:which_child(DynSup, receiver),
receive {Receiver, user_socket, UserSocket} ->
socket_control(UserSocket, Timeout)
end.

start_fsm(Port, Socket, {SSLOpts, _, _Trackers} = Opts,
User, CbInfo, Timeout) ->
ErlDist = maps:get(erl_dist, SSLOpts, false),
SenderSpawnOpts = maps:get(sender_spawn_opts, SSLOpts, []),
SenderOptions = handle_sender_options(ErlDist, SenderSpawnOpts),
{ok, DynSup} = start_connection_tree(ErlDist, SenderOptions,
[server, "localhost", Port, Socket, Opts, User, CbInfo]),
{ok, {_, Receiver,_,_}} = supervisor:which_child(DynSup, receiver),
receive {Receiver, user_socket, UserSocket} ->
socket_control(UserSocket, Timeout)
end.

handle_sender_options(ErlDist, SpawnOpts) ->
Expand All @@ -96,40 +105,13 @@ handle_sender_options(ErlDist, SpawnOpts) ->
[[{spawn_opt, SpawnOpts}]]
end.

start_connection_tree(User, IsErlDist, SenderOpts, Role, ReceiverOpts) ->
StartConnectionTree =
fun() ->
try start_dyn_connection_sup(IsErlDist) of
{ok, DynSup} ->
case tls_dyn_connection_sup:start_child(DynSup, sender, SenderOpts) of
{ok, Sender} ->
Args = [Role, Sender | ReceiverOpts],
case tls_dyn_connection_sup:start_child(DynSup, receiver, Args) of
{ok, Receiver} ->
User ! {self(), {ok, Receiver}};
{error, _} = Error ->
User ! {self(), Error},
exit(DynSup, shutdown)
end;
{error, _} = Error ->
User ! {self(), Error},
exit(DynSup, shutdown)
end;
{error, _Error} = Error ->
User ! {self(), Error}
catch exit:{noproc, _} ->
User ! {self(), {error, ssl_not_started}};
_:Reason:ST -> %% Don't hang signal internal error
?SSL_LOG(notice, internal_error, [{error, Reason}, {stacktrace, ST}]),
User ! {self(), {error, internal_error}}
end
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_connection_tree(IsErlDist, SenderOpts, ReceiverOpts) ->
case IsErlDist of
false ->
tls_connection_sup:start_child([SenderOpts, ReceiverOpts]);
true ->
tls_connection_sup:start_child_dist([SenderOpts, ReceiverOpts])
end.

socket_control(SslSocket, Timeout) ->
case ssl_gen_statem:socket_control(SslSocket) of
Expand Down
28 changes: 12 additions & 16 deletions lib/ssl/src/tls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ upgrade(Socket, #config{transport_info = {Transport,_,_,_,_}= CbInfo,
ok = setopts(Transport, Socket, tls_socket:internal_inet_values()),
case peername(Transport, Socket) of
{ok, {Host, Port}} ->
try tls_gen_connection:start_fsm(client, Host, Port, Socket,
try tls_gen_connection:start_fsm(Host, Port, Socket,
{SslOptions,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout) of
Expand All @@ -139,7 +139,7 @@ connect(Host, Port,
{Transport, _, _, _, _} = CbInfo,
try Transport:connect(Host, Port, SocketOpts, Timeout) of
{ok, Socket} ->
try tls_gen_connection:start_fsm(client, Host, Port, Socket,
try tls_gen_connection:start_fsm(Host, Port, Socket,
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout) of
Expand Down Expand Up @@ -420,20 +420,16 @@ call(Pid, Msg) ->
gen_server:call(Pid, Msg, infinity).

start_tls_server_connection(SslOpts, Port, Socket, EmOpts, Trackers, CbInfo) ->
try
{ok, DynSup} = tls_connection_sup:start_child([]),
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,
{SslOpts, emulated_socket_options(EmOpts, #socket_options{}), Trackers}, self(), CbInfo],
{ok, Pid} = tls_dyn_connection_sup:start_child(DynSup, receiver, ConnArgs),
receive {Pid, user_socket, UserSocket} ->
ssl_gen_statem:socket_control(UserSocket)
end
catch
error:{badmatch, {error, _} = Error} ->
Error
end.
try tls_gen_connection:start_fsm(Port, Socket,
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), Trackers},
self(), CbInfo, infinity) of
Result ->
Result
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end.

split_options(Opts) ->
split_options(Opts, emulated_options(), [], []).
Expand Down

0 comments on commit 1683519

Please sign in to comment.