From fb05af363a18d6618f1f73bdf092f6652f099c85 Mon Sep 17 00:00:00 2001 From: sugus-seu Date: Fri, 4 Nov 2016 20:35:51 +0800 Subject: Add connect timeout --- doc/src/manual/gun.asciidoc | 2 ++ src/gun.erl | 6 ++++-- 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}; -- cgit v1.2.3