Explorar o código

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
tigercl %!s(int64=6) %!d(string=hai) anos
pai
achega
ebc1bd77a9
Modificáronse 2 ficheiros con 8 adicións e 5 borrados
  1. 7 4
      src/emqx_channel.erl
  2. 1 1
      test/emqx_channel_SUITE.erl

+ 7 - 4
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) ->

+ 1 - 1
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).