Skip to content

Commit

Permalink
Introduce hibernate_after flag
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 7, 2025
1 parent f8074c9 commit e23a081
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/escalus_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ use_zlib(#client{module = Mod, rcv_pid = Pid}) ->

-spec upgrade_to_tls(client()) -> ok.
upgrade_to_tls(#client{module = Mod, rcv_pid = Pid, props = Props}) ->
SSLOpts = proplists:get_value(ssl_opts, Props, [[{verify, verify_none}]]),
HibernateAfter = proplists:get_value(hibernate_after, Props, 500),
DefSslOpts = [[{hibernate_after, HibernateAfter}, {verify, verify_none}]],
SSLOpts = proplists:get_value(ssl_opts, Props, DefSslOpts),
Mod:upgrade_to_tls(Pid, SSLOpts).

wait_for_close(Client) ->
Expand Down
20 changes: 12 additions & 8 deletions src/escalus_tcp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
event_client => undefined | escalus_event:event_client(),
socket_opts => [gen_tcp:connect_option()],
ssl_opts => [ssl:tls_client_option()],
parser_opts => [exml_stream:parser_opt()]
parser_opts => [exml_stream:parser_opt()],
hibernate_after => timeout()
}.

%%%===================================================================
Expand All @@ -81,7 +82,8 @@
-spec connect([proplists:property()] | opts()) -> pid().
connect(Opts0) ->
Opts1 = opts_to_map(Opts0),
{ok, Pid} = gen_server:start_link(?MODULE, [Opts1, self()], []),
GenOpts = maps:to_list(maps:with([hibernate_after], Opts1)),
{ok, Pid} = gen_server:start_link(?MODULE, [Opts1, self()], GenOpts),
Pid.

-spec send(pid(), exml_stream:element() | [exml_stream:element()] | binary()) -> ok.
Expand Down Expand Up @@ -320,7 +322,8 @@ default_options() ->
event_client => undefined,
socket_opts => default_socket_options(),
ssl_opts => [{verify, verify_none}],
parser_opts => []}.
parser_opts => [],
hibernate_after => 500}.

-spec default_socket_options() -> [gen_tcp:connect_option()].
default_socket_options() ->
Expand Down Expand Up @@ -461,11 +464,12 @@ do_connect(#{ssl := IsSSLConn,
on_connect := OnConnectFun,
host := Host,
port := Port,
ssl_opts := SSLOpts} = Opts) ->
ssl_opts := SSLOpts,
hibernate_after := HibernateAfter} = Opts) ->
Address = host_to_inet(Host),
SocketOpts = get_socket_opts(Opts),
TimeB = erlang:system_time(microsecond),
Reply = maybe_ssl_connection(IsSSLConn, Address, Port, SocketOpts, SSLOpts),
Reply = maybe_ssl_connection(IsSSLConn, Address, Port, SocketOpts, SSLOpts, HibernateAfter),
TimeA = erlang:system_time(microsecond),
ConnectionTime = TimeA - TimeB,
case Reply of
Expand All @@ -476,9 +480,9 @@ do_connect(#{ssl := IsSSLConn,
end,
Reply.

maybe_ssl_connection(true, Address, Port, SocketOpts, SSLOpts) ->
ssl:connect(Address, Port, SocketOpts ++ SSLOpts);
maybe_ssl_connection(_, Address, Port, SocketOpts, _) ->
maybe_ssl_connection(true, Address, Port, SocketOpts, SSLOpts, HibernateAfter) ->
ssl:connect(Address, Port, SocketOpts ++ SSLOpts ++ [{hibernate_after, HibernateAfter}]);
maybe_ssl_connection(_, Address, Port, SocketOpts, _, _) ->
gen_tcp:connect(Address, Port, SocketOpts).

%%===================================================================
Expand Down

0 comments on commit e23a081

Please sign in to comment.