From ebc1bd77a9e7124e52db152467a1e0ac685433a4 Mon Sep 17 00:00:00 2001 From: tigercl Date: Fri, 23 Aug 2019 14:41:52 +0800 Subject: [PATCH] Send the will message immediately when the network connection is closed by the client (#2834) Send the will message immediately when the network connection is closed by the client --- src/emqx_channel.erl | 11 +++++++---- test/emqx_channel_SUITE.erl | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/emqx_channel.erl b/src/emqx_channel.erl index c11b1e651..5b9a89a31 100644 --- a/src/emqx_channel.erl +++ b/src/emqx_channel.erl @@ -639,14 +639,17 @@ handle_info({unsubscribe, TopicFilters}, Channel = #channel{client = Client}) -> handle_info(sock_closed, Channel = #channel{connected = false}) -> shutdown(closed, Channel); -handle_info(sock_closed, Channel = #channel{session = Session}) -> +handle_info(sock_closed, Channel = #channel{protocol = Protocol, + session = Session}) -> + publish_will_msg(emqx_protocol:info(will_msg, Protocol)), + NChannel = Channel#channel{protocol = emqx_protocol:clear_will_msg(Protocol)}, Interval = emqx_session:info(expiry_interval, Session), case Interval of ?UINT_MAX -> - {ok, ensure_disconnected(Channel)}; + {ok, ensure_disconnected(NChannel)}; Int when Int > 0 -> - {ok, ensure_timer(expire_timer, ensure_disconnected(Channel))}; - _Other -> shutdown(closed, Channel) + {ok, ensure_timer(expire_timer, ensure_disconnected(NChannel))}; + _Other -> shutdown(closed, NChannel) end; handle_info(Info, Channel) -> diff --git a/test/emqx_channel_SUITE.erl b/test/emqx_channel_SUITE.erl index e7326cd76..1423046ab 100644 --- a/test/emqx_channel_SUITE.erl +++ b/test/emqx_channel_SUITE.erl @@ -143,7 +143,7 @@ t_handle_pingreq(_) -> t_handle_disconnect(_) -> with_channel( fun(Channel) -> - {stop, normal, Channel1} = handle_in(?DISCONNECT_PACKET(?RC_SUCCESS), Channel), + {stop, {shutdown, normal}, Channel1} = handle_in(?DISCONNECT_PACKET(?RC_SUCCESS), Channel), ?assertMatch(#{will_msg := undefined}, emqx_channel:info(protocol, Channel1)) end).