aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsugus-seu <[email protected]>2016-11-04 20:35:51 +0800
committerLoïc Hoguin <[email protected]>2017-04-30 17:04:41 +0200
commitfb05af363a18d6618f1f73bdf092f6652f099c85 (patch)
tree7aa8ce548e5ccff1efb2c2ab74e3f358296429d2
parentbb3ed8f6da301c3cbb3e3cab7777c94f225ef28b (diff)
downloadgun-fb05af363a18d6618f1f73bdf092f6652f099c85.tar.gz
gun-fb05af363a18d6618f1f73bdf092f6652f099c85.tar.bz2
gun-fb05af363a18d6618f1f73bdf092f6652f099c85.zip
Add connect timeout
-rw-r--r--doc/src/manual/gun.asciidoc2
-rw-r--r--src/gun.erl6
2 files changed, 6 insertions, 2 deletions
diff --git a/doc/src/manual/gun.asciidoc b/doc/src/manual/gun.asciidoc
index baaadcf..83f119a 100644
--- a/doc/src/manual/gun.asciidoc
+++ b/doc/src/manual/gun.asciidoc
@@ -18,6 +18,8 @@ Configuration for the connection.
The following keys are defined:
+connect_timeout => timeout()::
+ Connection timeout. Defaults to `infinity`.
http_opts => http_opts()::
Options specific to the HTTP protocol. See below.
protocols => [http | spdy]::
diff --git a/src/gun.erl b/src/gun.erl
index 9b3dd26..c5fbbc5 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -135,6 +135,8 @@ open(Host, Port, Opts) when is_list(Host); is_atom(Host) ->
check_options([]) ->
ok;
+check_options([{connect_timeout, T}|Opts]) when is_integer(T), T >= 0 ->
+ check_options(Opts);
check_options([{http_opts, ProtoOpts}|Opts]) when is_map(ProtoOpts) ->
case gun_http:check_options(ProtoOpts) of
ok ->
@@ -498,7 +500,7 @@ connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport=ranch_
{alpn_advertised_protocols, Protocols},
{client_preferred_next_protocols, {client, Protocols, <<"http/1.1">>}}
|maps:get(transport_opts, Opts, [])],
- case Transport:connect(Host, Port, TransportOpts) of
+ case Transport:connect(Host, Port, TransportOpts, maps:get(connect_timeout, Opts, infinity)) of
{ok, Socket} ->
{Protocol, ProtoOptsKey} = case ssl:negotiated_protocol(Socket) of
{ok, <<"h2">>} -> {gun_http2, http2_opts};
@@ -512,7 +514,7 @@ connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport=ranch_
connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport}, Retries) ->
TransportOpts = [binary, {active, false}
|maps:get(transport_opts, Opts, [])],
- case Transport:connect(Host, Port, TransportOpts) of
+ case Transport:connect(Host, Port, TransportOpts, maps:get(connect_timeout, Opts, infinity)) of
{ok, Socket} ->
{Protocol, ProtoOptsKey} = case maps:get(protocols, Opts, [http]) of
[http] -> {gun_http, http_opts};