From 3c75c8b04e897649b61d551a8391f4322fdf4815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 30 Apr 2017 16:26:44 +0200 Subject: Propagate the last connection failure reason Based on a patch by Pablo Polvorin. Thanks! --- src/gun.erl | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/gun.erl b/src/gun.erl index 44d818e..34d37f5 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -106,7 +106,8 @@ socket :: undefined | inet:socket() | ssl:sslsocket(), transport :: module(), protocol :: module(), - protocol_state :: any() + protocol_state :: any(), + last_error :: any() }). %% Connection. @@ -505,8 +506,8 @@ connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport=ranch_ _ -> {gun_http, http_opts} end, up(State, Socket, Protocol, ProtoOptsKey); - {error, _} -> - retry(State, Retries) + {error, Reason} -> + retry(State#state{last_error=Reason}, Retries) end; connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport}, Retries) -> TransportOpts = [binary, {active, false} @@ -519,8 +520,8 @@ connect(State=#state{host=Host, port=Port, opts=Opts, transport=Transport}, Retr [spdy] -> {gun_spdy, spdy_opts} end, up(State, Socket, Protocol, ProtoOptsKey); - {error, _} -> - retry(State, Retries) + {error, Reason} -> + retry(State#state{last_error=Reason}, Retries) end. up(State=#state{owner=Owner, opts=Opts, transport=Transport}, Socket, Protocol, ProtoOptsKey) -> @@ -532,11 +533,11 @@ up(State=#state{owner=Owner, opts=Opts, transport=Transport}, Socket, Protocol, down(State=#state{owner=Owner, opts=Opts, protocol=Protocol, protocol_state=ProtoState}, Reason) -> {KilledStreams, UnprocessedStreams} = Protocol:down(ProtoState), Owner ! {gun_down, self(), Protocol:name(), Reason, KilledStreams, UnprocessedStreams}, - retry(State#state{socket=undefined, protocol=undefined, protocol_state=undefined}, - maps:get(retry, Opts, 5)). + retry(State#state{socket=undefined, protocol=undefined, protocol_state=undefined, + last_error=Reason}, maps:get(retry, Opts, 5)). -retry(_, 0) -> - error(gone); +retry(#state{last_error=Reason}, 0) -> + error({gone, Reason}); retry(State=#state{keepalive_ref=KeepaliveRef}, Retries) when is_reference(KeepaliveRef) -> _ = erlang:cancel_timer(KeepaliveRef), %% Flush if we have a keepalive message -- cgit v1.2.3