diff options
author | Loïc Hoguin <[email protected]> | 2019-01-05 11:53:31 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-01-05 11:53:31 +0100 |
commit | 4bd87ba7d1699fc1b2901cd61b703429f1deff63 (patch) | |
tree | a24fe6638cbabc1a675f8f4df0ccd70ab5c0cbff | |
parent | c2ba3ff0999f523b665dc0dbf100ed634bd1d6af (diff) | |
download | gun-4bd87ba7d1699fc1b2901cd61b703429f1deff63.tar.gz gun-4bd87ba7d1699fc1b2901cd61b703429f1deff63.tar.bz2 gun-4bd87ba7d1699fc1b2901cd61b703429f1deff63.zip |
Ensure Gun retries connecting immediately
-rw-r--r-- | src/gun.erl | 20 | ||||
-rw-r--r-- | test/gun_SUITE.erl | 17 |
2 files changed, 29 insertions, 8 deletions
diff --git a/src/gun.erl b/src/gun.erl index 09c0b95..a8b76f8 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -179,8 +179,7 @@ transport :: module(), messages :: {atom(), atom(), atom()}, protocol :: module(), - protocol_state :: any(), - last_error :: any() + protocol_state :: any() }). %% Connection. @@ -703,9 +702,9 @@ not_connected(_, {retries, Retries}, {next_event, internal, {connected, Socket, Protocol}}}; {error, Reason} when Retries =:= 0 -> {stop, {shutdown, Reason}}; - {error, Reason} -> + {error, _Reason} -> Timeout = maps:get(retry_timeout, Opts, 5000), - {keep_state, State#state{last_error=Reason}, + {keep_state, State, {state_timeout, Timeout, {retries, Retries - 1}}} end; not_connected(Type, Event, State) -> @@ -912,10 +911,15 @@ disconnect(State=#state{owner=Owner, opts=Opts, {KilledStreams, UnprocessedStreams} = Protocol:down(ProtoState), Owner ! {gun_down, self(), Protocol:name(), Reason, KilledStreams, UnprocessedStreams}, Retry = maps:get(retry, Opts, 5), - {next_state, not_connected, - keepalive_cancel(State#state{socket=undefined, - protocol=undefined, protocol_state=undefined, last_error=Reason}), - {next_event, internal, {retries, Retry}}}. + case Retry of + 0 -> + {stop, {shutdown, Reason}}; + _ -> + {next_state, not_connected, + keepalive_cancel(State#state{socket=undefined, + protocol=undefined, protocol_state=undefined}), + {next_event, internal, {retries, Retry - 1}}} + end. disconnect_flush(State=#state{socket=Socket, messages={OK, Closed, Error}}) -> receive diff --git a/test/gun_SUITE.erl b/test/gun_SUITE.erl index cb2c262..a7bb0f1 100644 --- a/test/gun_SUITE.erl +++ b/test/gun_SUITE.erl @@ -18,6 +18,7 @@ -import(ct_helper, [doc/1]). -import(ct_helper, [name/0]). +-import(gun_test, [init_origin/3]). all() -> ct_helper:all(?MODULE). @@ -227,6 +228,22 @@ retry_1(_) -> error(timeout) end. +retry_immediately(_) -> + doc("Ensure Gun retries immediately."), + %% We have to make a first successful connection in order to test this. + {ok, _, OriginPort} = init_origin(tcp, http, + fun(_, ClientSocket, ClientTransport) -> + ClientTransport:close(ClientSocket) + end), + {ok, Pid} = gun:open("localhost", OriginPort, #{retry => 1, retry_timeout => 500}), + Ref = monitor(process, Pid), + receive + {'DOWN', Ref, process, Pid, {shutdown, _}} -> + ok + after 200 -> + error(timeout) + end. + retry_timeout(_) -> doc("Ensure the retry_timeout value is enforced."), {ok, Pid} = gun:open("localhost", 12345, #{retry => 1, retry_timeout => 1000}), |