aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2019-05-13 13:18:01 +0200
committerLoïc Hoguin <[email protected]>2019-05-13 13:18:01 +0200
commit3aa7168404e62f9ea9e290380b93569eff4372a8 (patch)
treeab8cb6f89c2e5e3abb5cb27d34b9b902b427625f /src/gun.erl
parentcfd702a716f834c431abd46532e8cfa4debd1468 (diff)
downloadgun-3aa7168404e62f9ea9e290380b93569eff4372a8.tar.gz
gun-3aa7168404e62f9ea9e290380b93569eff4372a8.tar.bz2
gun-3aa7168404e62f9ea9e290380b93569eff4372a8.zip
Add the disconnect event callback
Diffstat (limited to 'src/gun.erl')
-rw-r--r--src/gun.erl13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/gun.erl b/src/gun.erl
index e958d2e..44dbdeb 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -1006,7 +1006,8 @@ commands([{switch_protocol, Protocol, _ProtoState0}|Tail],
disconnect(State=#state{owner=Owner, opts=Opts,
socket=Socket, transport=Transport,
- protocol=Protocol, protocol_state=ProtoState}, Reason) ->
+ protocol=Protocol, protocol_state=ProtoState,
+ event_handler=EventHandler, event_handler_state=EventHandlerState0}, Reason) ->
Protocol:close(Reason, ProtoState),
%% @todo Need a special state for orderly shutdown of a connection.
Transport:close(Socket),
@@ -1015,14 +1016,20 @@ disconnect(State=#state{owner=Owner, opts=Opts,
%% @todo Stop keepalive timeout, flush message.
{KilledStreams, UnprocessedStreams} = Protocol:down(ProtoState),
Owner ! {gun_down, self(), Protocol:name(), Reason, KilledStreams, UnprocessedStreams},
+ %% Trigger the disconnect event.
+ DisconnectEvent = #{
+ reason => Reason
+ },
+ EventHandlerState = EventHandler:disconnect(DisconnectEvent, EventHandlerState0),
Retry = maps:get(retry, Opts, 5),
case Retry of
0 ->
- {stop, {shutdown, Reason}};
+ {stop, {shutdown, Reason}, State#state{event_handler_state=EventHandlerState}};
_ ->
{next_state, not_connected,
keepalive_cancel(State#state{socket=undefined,
- protocol=undefined, protocol_state=undefined}),
+ protocol=undefined, protocol_state=undefined,
+ event_handler_state=EventHandlerState}),
{next_event, internal, {retries, Retry - 1}}}
end.