Skip to content

Commit 952753f

Browse files
authored
Merge pull request #781 from benoitc/fix-ssl-timeout-handling
fix: respect connect timeout in ssl:connect
2 parents d176c98 + b665d3f commit 952753f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/hackney_ssl.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,22 @@ connect(Host, Port, Opts0, Timeout) when is_list(Host), is_integer(Port),
130130
SSLOpts = proplists:get_value(ssl_options, Opts0),
131131
BaseOpts = [binary, {active, false}, {packet, raw}],
132132
Opts1 = hackney_util:merge_opts(BaseOpts, proplists:delete(ssl_options, Opts0)),
133+
Now = erlang:monotonic_time(millisecond),
133134
case hackney_happy:connect(Host, Port, Opts1, Timeout) of
134135
{ok, Sock} ->
135-
ssl:connect(Sock, SSLOpts);
136+
case Timeout of
137+
infinity ->
138+
ssl:connect(Sock, SSLOpts);
139+
_ ->
140+
Elapsed = erlang:monotonic_time(millisecond) - Now,
141+
case Timeout - Elapsed of
142+
TimeoutLeft when TimeoutLeft > 0 ->
143+
ssl:connect(Sock, SSLOpts, TimeoutLeft);
144+
_ ->
145+
gen_tcp:close(Sock),
146+
{error, timeout}
147+
end
148+
end;
136149
Error ->
137150
Error
138151
end.

0 commit comments

Comments
 (0)