Просмотр исходного кода

Merge pull request #13677 from lafirest/fix/exclusive_delete

fix(exclusive): totally remove subscriptions when calling `exclusive delete`
lafirest 1 год назад
Родитель
Сommit
81f8acd1f5
2 измененных файлов с 20 добавлено и 1 удалено
  1. 9 0
      apps/emqx/src/emqx_exclusive_subscription.erl
  2. 11 1
      apps/emqx_management/src/emqx_mgmt_cli.erl

+ 9 - 0
apps/emqx/src/emqx_exclusive_subscription.erl

@@ -30,6 +30,7 @@
 -export([
 -export([
     check_subscribe/2,
     check_subscribe/2,
     unsubscribe/2,
     unsubscribe/2,
+    dirty_lookup_clientid/1,
     clear/0
     clear/0
 ]).
 ]).
 
 
@@ -99,6 +100,14 @@ unsubscribe(Topic, #{is_exclusive := true}) ->
 unsubscribe(_Topic, _SubOpts) ->
 unsubscribe(_Topic, _SubOpts) ->
     ok.
     ok.
 
 
+dirty_lookup_clientid(Topic) ->
+    case mnesia:dirty_read(?TAB, Topic) of
+        [#exclusive_subscription{clientid = ClientId}] ->
+            ClientId;
+        _ ->
+            undefined
+    end.
+
 clear() ->
 clear() ->
     mria:clear_table(?TAB).
     mria:clear_table(?TAB).
 
 

+ 11 - 1
apps/emqx_management/src/emqx_mgmt_cli.erl

@@ -1153,7 +1153,17 @@ exclusive(["list"]) ->
     end;
     end;
 exclusive(["delete", Topic0]) ->
 exclusive(["delete", Topic0]) ->
     Topic = erlang:iolist_to_binary(Topic0),
     Topic = erlang:iolist_to_binary(Topic0),
-    emqx_exclusive_subscription:unsubscribe(Topic, #{is_exclusive => true}),
+    case emqx_exclusive_subscription:dirty_lookup_clientid(Topic) of
+        undefined ->
+            ok;
+        ClientId ->
+            case emqx_mgmt:unsubscribe(ClientId, Topic) of
+                {unsubscribe, _} ->
+                    ok;
+                {error, channel_not_found} ->
+                    emqx_exclusive_subscription:unsubscribe(Topic, #{is_exclusive => true})
+            end
+    end,
     emqx_ctl:print("ok~n");
     emqx_ctl:print("ok~n");
 exclusive(_) ->
 exclusive(_) ->
     emqx_ctl:usage([
     emqx_ctl:usage([