aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-04-30 16:26:44 +0200
committerLoïc Hoguin <[email protected]>2017-04-30 16:26:44 +0200
commit3c75c8b04e897649b61d551a8391f4322fdf4815 (patch)
tree04935f272731d29bb60f81fdfb34a4e38c333683 /src/gun.erl
parentcc424b01f8b86de965bcc7e5b5e5560a6b7ea385 (diff)
downloadgun-3c75c8b04e897649b61d551a8391f4322fdf4815.tar.gz
gun-3c75c8b04e897649b61d551a8391f4322fdf4815.tar.bz2
gun-3c75c8b04e897649b61d551a8391f4322fdf4815.zip
Propagate the last connection failure reason
Based on a patch by Pablo Polvorin. Thanks!
Diffstat (limited to 'src/gun.erl')
-rw-r--r--src/gun.erl19
1 files 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