From 4ef86b47c7be3db0e7d5063d2c5b0694e76c55b2 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 15 Jun 2022 18:21:57 +0800 Subject: [PATCH 1/4] fix: flaky test case for emqx_delayed --- apps/emqx_modules/test/emqx_delayed_SUITE.erl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/emqx_modules/test/emqx_delayed_SUITE.erl b/apps/emqx_modules/test/emqx_delayed_SUITE.erl index a476b6f75..d1af9a064 100644 --- a/apps/emqx_modules/test/emqx_delayed_SUITE.erl +++ b/apps/emqx_modules/test/emqx_delayed_SUITE.erl @@ -65,6 +65,7 @@ end_per_testcase(_Case, _Config) -> t_enable_disable_case(_) -> emqx_delayed:unload(), + timer:sleep(100), Hooks = emqx_hooks:lookup('message.publish'), MFA = {emqx_delayed, on_message_publish, []}, ?assertEqual(false, lists:keyfind(MFA, 2, Hooks)), @@ -81,6 +82,7 @@ t_enable_disable_case(_) -> ?assertMatch(#{data := Datas} when Datas =/= [], emqx_delayed:list(#{})), emqx_delayed:unload(), + timer:sleep(100), ?assertEqual(false, lists:keyfind(MFA, 2, Hooks)), ?assertMatch(#{data := []}, emqx_delayed:list(#{})), ok. @@ -188,6 +190,7 @@ t_unknown_messages(_) -> ). t_get_basic_usage_info(_Config) -> + emqx:update_config([delayed, max_delayed_messages], 10000), ?assertEqual(#{delayed_message_count => 0}, emqx_delayed:get_basic_usage_info()), lists:foreach( fun(N) -> From 39b1b2050681a537527ac22dc636b03e64f5ffde Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 15 Jun 2022 19:03:40 +0800 Subject: [PATCH 2/4] feat: fix the hook priorities --- apps/emqx/include/emqx_hooks.hrl | 38 +++++++++++++++++++ apps/emqx/src/emqx_authentication.erl | 6 +-- apps/emqx/src/emqx_hooks.erl | 6 +-- apps/emqx/src/emqx_sys.erl | 3 +- apps/emqx_authz/src/emqx_authz.erl | 5 ++- .../src/emqx_auto_subscribe.erl | 6 ++- apps/emqx_bridge/src/emqx_bridge.erl | 3 +- apps/emqx_exhook/src/emqx_exhook_mgr.erl | 2 +- apps/emqx_exhook/src/emqx_exhook_server.erl | 3 +- apps/emqx_modules/src/emqx_delayed.erl | 3 +- apps/emqx_modules/src/emqx_rewrite.erl | 7 ++-- apps/emqx_modules/src/emqx_topic_metrics.erl | 7 ++-- apps/emqx_psk/src/emqx_psk.erl | 5 ++- apps/emqx_retainer/src/emqx_retainer.erl | 11 ++++-- .../emqx_rule_engine/src/emqx_rule_events.erl | 5 ++- apps/emqx_slow_subs/src/emqx_slow_subs.erl | 8 ++-- 16 files changed, 88 insertions(+), 30 deletions(-) create mode 100644 apps/emqx/include/emqx_hooks.hrl diff --git a/apps/emqx/include/emqx_hooks.hrl b/apps/emqx/include/emqx_hooks.hrl new file mode 100644 index 000000000..08fa07bc8 --- /dev/null +++ b/apps/emqx/include/emqx_hooks.hrl @@ -0,0 +1,38 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2021-2022 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +%% Definitions for Hook Priorities + +%% == Highest Priority +-define(HP_PSK, 1000). +-define(HP_REWRITE, 1000). +-define(HP_AUTHN, 990). +-define(HP_AUTHZ, 980). +-define(HP_SYS_MSGS, 960). +-define(HP_TOPIC_METRICS, 950). +-define(HP_RETAINER, 940). +-define(HP_AUTO_SUB, 930). + +-define(HP_RULE_ENGINE, 900). + +%% apps that can work with the republish action +-define(HP_SLOW_SUB, 980). +-define(HP_BRIDGE, 970). +-define(HP_DELAY_PUB, 960). + +%% apps that can stop the hooks +-define(HP_EXHOOK, 100). +%% == Lowest Priority diff --git a/apps/emqx/src/emqx_authentication.erl b/apps/emqx/src/emqx_authentication.erl index 101be754d..b46e1faa8 100644 --- a/apps/emqx/src/emqx_authentication.erl +++ b/apps/emqx/src/emqx_authentication.erl @@ -25,7 +25,7 @@ -include("emqx.hrl"). -include("logger.hrl"). -include("emqx_authentication.hrl"). - +-include_lib("emqx/include/emqx_hooks.hrl"). -include_lib("stdlib/include/ms_transform.hrl"). -define(CONF_ROOT, ?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME_ATOM). @@ -696,7 +696,7 @@ maybe_hook(#{hooked := false} = State) -> ) of true -> - _ = emqx:hook('client.authenticate', {?MODULE, authenticate, []}), + ok = emqx_hooks:put('client.authenticate', {?MODULE, authenticate, []}, ?HP_AUTHN), State#{hooked => true}; false -> State @@ -715,7 +715,7 @@ maybe_unhook(#{hooked := true} = State) -> ) of true -> - _ = emqx:unhook('client.authenticate', {?MODULE, authenticate, []}), + ok = emqx_hooks:del('client.authenticate', {?MODULE, authenticate, []}), State#{hooked => false}; false -> State diff --git a/apps/emqx/src/emqx_hooks.erl b/apps/emqx/src/emqx_hooks.erl index 72319bae0..632257c4f 100644 --- a/apps/emqx/src/emqx_hooks.erl +++ b/apps/emqx/src/emqx_hooks.erl @@ -121,7 +121,7 @@ callback_priority(#callback{priority = P}) -> P. %% Hooks API %%-------------------------------------------------------------------- -%% @doc Register a callback +%% @doc `add/2,3,4` add a new hook, returns 'already_exists' if the hook exists. -spec add(hookpoint(), action() | callback()) -> ok_or_error(already_exists). add(HookPoint, Callback) when is_record(Callback, callback) -> gen_server:call(?SERVER, {add, HookPoint, Callback}, infinity); @@ -140,7 +140,7 @@ add(HookPoint, Action, Priority) when is_integer(Priority) -> add(HookPoint, Action, Filter, Priority) when is_integer(Priority) -> add(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}). -%% @doc Like add/2, it register a callback, discard 'already_exists' error. +%% @doc `put/2,3,4` updates the existing hook, add it if not exists. -spec put(hookpoint(), action() | callback()) -> ok. put(HookPoint, Callback) when is_record(Callback, callback) -> case add(HookPoint, Callback) of @@ -296,7 +296,7 @@ insert_hook(HookPoint, Callbacks) -> ets:insert(?TAB, #hook{name = HookPoint, callbacks = Callbacks}), ok. update_hook(HookPoint, Callbacks) -> - Ms = ets:fun2ms(fun({hook, K, V}) when K =:= HookPoint -> {hook, K, Callbacks} end), + Ms = ets:fun2ms(fun({hook, K, _V}) when K =:= HookPoint -> {hook, K, Callbacks} end), ets:select_replace(emqx_hooks, Ms), ok. diff --git a/apps/emqx/src/emqx_sys.erl b/apps/emqx/src/emqx_sys.erl index 2b1f87746..284fefac2 100644 --- a/apps/emqx/src/emqx_sys.erl +++ b/apps/emqx/src/emqx_sys.erl @@ -21,6 +21,7 @@ -include("emqx.hrl"). -include("types.hrl"). -include("logger.hrl"). +-include("emqx_hooks.hrl"). -export([ start_link/0, @@ -190,7 +191,7 @@ load_event_hooks(Events) -> ok; ({K, true}) -> {HookPoint, Fun} = hook_and_fun(K), - emqx_hooks:put(HookPoint, {?MODULE, Fun, []}) + emqx_hooks:put(HookPoint, {?MODULE, Fun, []}, ?HP_SYS_MSGS) end, Events ). diff --git a/apps/emqx_authz/src/emqx_authz.erl b/apps/emqx_authz/src/emqx_authz.erl index e41ef71ce..410920f97 100644 --- a/apps/emqx_authz/src/emqx_authz.erl +++ b/apps/emqx_authz/src/emqx_authz.erl @@ -19,6 +19,7 @@ -include("emqx_authz.hrl"). -include_lib("emqx/include/logger.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -ifdef(TEST). -compile(export_all). @@ -100,7 +101,7 @@ init() -> Sources = emqx_conf:get(?CONF_KEY_PATH, []), ok = check_dup_types(Sources), NSources = create_sources(Sources), - ok = emqx_hooks:add('client.authorize', {?MODULE, authorize, [NSources]}, -1). + ok = emqx_hooks:put('client.authorize', {?MODULE, authorize, [NSources]}, ?HP_AUTHZ). deinit() -> ok = emqx_hooks:del('client.authorize', {?MODULE, authorize}), @@ -501,7 +502,7 @@ get_source_by_type(Type, Sources) -> %% @doc put hook with (maybe) initialized new source and old sources update_authz_chain(Actions) -> - emqx_hooks:put('client.authorize', {?MODULE, authorize, [Actions]}, -1). + emqx_hooks:put('client.authorize', {?MODULE, authorize, [Actions]}, ?HP_AUTHZ). check_acl_file_rules(RawRules) -> %% TODO: make sure the bin rules checked diff --git a/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl b/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl index f7f06b13c..44d3d04c5 100644 --- a/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl +++ b/apps/emqx_auto_subscribe/src/emqx_auto_subscribe.erl @@ -16,6 +16,8 @@ -module(emqx_auto_subscribe). +-include_lib("emqx/include/emqx_hooks.hrl"). + -define(HOOK_POINT, 'client.connected'). -define(MAX_AUTO_SUBSCRIBE, 20). @@ -114,5 +116,7 @@ update_hook() -> update_hook(Config) -> {TopicHandler, Options} = emqx_auto_subscribe_handler:init(Config), - emqx_hooks:put(?HOOK_POINT, {?MODULE, on_client_connected, [{TopicHandler, Options}]}), + emqx_hooks:put( + ?HOOK_POINT, {?MODULE, on_client_connected, [{TopicHandler, Options}]}, ?HP_AUTO_SUB + ), ok. diff --git a/apps/emqx_bridge/src/emqx_bridge.erl b/apps/emqx_bridge/src/emqx_bridge.erl index c4a657a0c..200c8c2a9 100644 --- a/apps/emqx_bridge/src/emqx_bridge.erl +++ b/apps/emqx_bridge/src/emqx_bridge.erl @@ -17,6 +17,7 @@ -behaviour(emqx_config_handler). -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl"). -export([post_config_update/5]). @@ -105,7 +106,7 @@ load_hook(Bridges) -> do_load_hook(#{local_topic := _} = Conf) -> case maps:get(direction, Conf, egress) of - egress -> emqx_hooks:put('message.publish', {?MODULE, on_message_publish, []}); + egress -> emqx_hooks:put('message.publish', {?MODULE, on_message_publish, []}, ?HP_BRIDGE); ingress -> ok end; do_load_hook(_Conf) -> diff --git a/apps/emqx_exhook/src/emqx_exhook_mgr.erl b/apps/emqx_exhook/src/emqx_exhook_mgr.erl index c18dd6239..cf83e8eb9 100644 --- a/apps/emqx_exhook/src/emqx_exhook_mgr.erl +++ b/apps/emqx_exhook/src/emqx_exhook_mgr.erl @@ -317,7 +317,7 @@ code_change(_OldVsn, State, _Extra) -> unload_exhooks() -> [ - emqx:unhook(Name, {M, F}) + emqx_hooks:del(Name, {M, F}) || {Name, {M, F, _A}} <- ?ENABLED_HOOKS ]. diff --git a/apps/emqx_exhook/src/emqx_exhook_server.erl b/apps/emqx_exhook/src/emqx_exhook_server.erl index 88aee8e7d..b0c9b8454 100644 --- a/apps/emqx_exhook/src/emqx_exhook_server.erl +++ b/apps/emqx_exhook/src/emqx_exhook_server.erl @@ -18,6 +18,7 @@ -include("emqx_exhook.hrl"). -include_lib("emqx/include/logger.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). %% The exhook proto version should be fixed as `v2` in EMQX v5.x %% to make sure the exhook proto version is compatible @@ -255,7 +256,7 @@ ensure_hooks(HookSpecs) -> false -> ?SLOG(error, #{msg => "skipped_unknown_hookpoint", hookpoint => Hookpoint}); {Hookpoint, {M, F, A}} -> - emqx_hooks:put(Hookpoint, {M, F, A}), + emqx_hooks:put(Hookpoint, {M, F, A}, ?HP_EXHOOK), ets:update_counter(?HOOKS_REF_COUNTER, Hookpoint, {2, 1}, {Hookpoint, 0}) end end, diff --git a/apps/emqx_modules/src/emqx_delayed.erl b/apps/emqx_modules/src/emqx_delayed.erl index 99ae97dd3..99bd62022 100644 --- a/apps/emqx_modules/src/emqx_delayed.erl +++ b/apps/emqx_modules/src/emqx_delayed.erl @@ -22,6 +22,7 @@ -include_lib("emqx/include/types.hrl"). -include_lib("emqx/include/logger.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). %% Mnesia bootstrap -export([mnesia/1]). @@ -379,7 +380,7 @@ do_publish(Key = {Ts, _Id}, Now, Acc) when Ts =< Now -> delayed_count() -> mnesia:table_info(?TAB, size). do_load_or_unload(true, State) -> - emqx_hooks:put('message.publish', {?MODULE, on_message_publish, []}), + emqx_hooks:put('message.publish', {?MODULE, on_message_publish, []}, ?HP_DELAY_PUB), State; do_load_or_unload(false, #{publish_timer := PubTimer} = State) -> emqx_hooks:del('message.publish', {?MODULE, on_message_publish}), diff --git a/apps/emqx_modules/src/emqx_rewrite.erl b/apps/emqx_modules/src/emqx_rewrite.erl index edad6dfc4..79465045d 100644 --- a/apps/emqx_modules/src/emqx_rewrite.erl +++ b/apps/emqx_modules/src/emqx_rewrite.erl @@ -19,6 +19,7 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/emqx_mqtt.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -ifdef(TEST). -export([ @@ -76,9 +77,9 @@ register_hook([]) -> unregister_hook(); register_hook(Rules) -> {PubRules, SubRules, ErrRules} = compile(Rules), - emqx_hooks:put('client.subscribe', {?MODULE, rewrite_subscribe, [SubRules]}), - emqx_hooks:put('client.unsubscribe', {?MODULE, rewrite_unsubscribe, [SubRules]}), - emqx_hooks:put('message.publish', {?MODULE, rewrite_publish, [PubRules]}), + emqx_hooks:put('client.subscribe', {?MODULE, rewrite_subscribe, [SubRules]}, ?HP_REWRITE), + emqx_hooks:put('client.unsubscribe', {?MODULE, rewrite_unsubscribe, [SubRules]}, ?HP_REWRITE), + emqx_hooks:put('message.publish', {?MODULE, rewrite_publish, [PubRules]}, ?HP_REWRITE), case ErrRules of [] -> ok; diff --git a/apps/emqx_modules/src/emqx_topic_metrics.erl b/apps/emqx_modules/src/emqx_topic_metrics.erl index 76d0c6e14..5a0925771 100644 --- a/apps/emqx_modules/src/emqx_topic_metrics.erl +++ b/apps/emqx_modules/src/emqx_topic_metrics.erl @@ -22,6 +22,7 @@ -include_lib("emqx/include/logger.hrl"). -include_lib("emqx/include/emqx_mqtt.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -export([ on_message_publish/1, @@ -106,9 +107,9 @@ max_limit() -> ?MAX_TOPICS. enable() -> - emqx_hooks:put('message.publish', {?MODULE, on_message_publish, []}), - emqx_hooks:put('message.dropped', {?MODULE, on_message_dropped, []}), - emqx_hooks:put('message.delivered', {?MODULE, on_message_delivered, []}). + emqx_hooks:put('message.publish', {?MODULE, on_message_publish, []}, ?HP_TOPIC_METRICS), + emqx_hooks:put('message.dropped', {?MODULE, on_message_dropped, []}, ?HP_TOPIC_METRICS), + emqx_hooks:put('message.delivered', {?MODULE, on_message_delivered, []}, ?HP_TOPIC_METRICS). disable() -> emqx_hooks:del('message.publish', {?MODULE, on_message_publish}), diff --git a/apps/emqx_psk/src/emqx_psk.erl b/apps/emqx_psk/src/emqx_psk.erl index 294a969c2..99354d230 100644 --- a/apps/emqx_psk/src/emqx_psk.erl +++ b/apps/emqx_psk/src/emqx_psk.erl @@ -19,6 +19,7 @@ -behaviour(gen_server). -include_lib("emqx/include/logger.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -export([ load/0, @@ -85,10 +86,10 @@ mnesia(boot) -> %%------------------------------------------------------------------------------ load() -> - emqx:hook('tls_handshake.psk_lookup', {?MODULE, on_psk_lookup, []}). + ok = emqx_hooks:put('tls_handshake.psk_lookup', {?MODULE, on_psk_lookup, []}, ?HP_PSK). unload() -> - emqx:unhook('tls_handshake.psk_lookup', {?MODULE, on_psk_lookup}). + ok = emqx_hooks:del('tls_handshake.psk_lookup', {?MODULE, on_psk_lookup}). on_psk_lookup(PSKIdentity, _UserState) -> case mnesia:dirty_read(?TAB, PSKIdentity) of diff --git a/apps/emqx_retainer/src/emqx_retainer.erl b/apps/emqx_retainer/src/emqx_retainer.erl index 6764f5e82..c15b0f5bd 100644 --- a/apps/emqx_retainer/src/emqx_retainer.erl +++ b/apps/emqx_retainer/src/emqx_retainer.erl @@ -20,6 +20,7 @@ -include("emqx_retainer.hrl"). -include_lib("emqx/include/logger.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -export([start_link/0]). @@ -423,13 +424,15 @@ close_resource(_) -> -spec load(context()) -> ok. load(Context) -> - _ = emqx:hook('session.subscribed', {?MODULE, on_session_subscribed, [Context]}), - _ = emqx:hook('message.publish', {?MODULE, on_message_publish, [Context]}), + ok = emqx_hooks:put( + 'session.subscribed', {?MODULE, on_session_subscribed, [Context]}, ?HP_RETAINER + ), + ok = emqx_hooks:put('message.publish', {?MODULE, on_message_publish, [Context]}, ?HP_RETAINER), emqx_stats:update_interval(emqx_retainer_stats, fun ?MODULE:stats_fun/0), ok. unload() -> - emqx:unhook('message.publish', {?MODULE, on_message_publish}), - emqx:unhook('session.subscribed', {?MODULE, on_session_subscribed}), + ok = emqx_hooks:del('message.publish', {?MODULE, on_message_publish}), + ok = emqx_hooks:del('session.subscribed', {?MODULE, on_session_subscribed}), emqx_stats:cancel_update(emqx_retainer_stats), ok. diff --git a/apps/emqx_rule_engine/src/emqx_rule_events.erl b/apps/emqx_rule_engine/src/emqx_rule_events.erl index 9aa7d0aa0..a25ad0e2e 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_events.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_events.erl @@ -19,6 +19,7 @@ -include("rule_engine.hrl"). -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -export([ reload/0, @@ -87,7 +88,9 @@ reload() -> load(Topic) -> HookPoint = event_name(Topic), - emqx_hooks:put(HookPoint, {?MODULE, hook_fun(HookPoint), [#{event_topic => Topic}]}). + emqx_hooks:put( + HookPoint, {?MODULE, hook_fun(HookPoint), [#{event_topic => Topic}]}, ?HP_RULE_ENGINE + ). unload() -> lists:foreach( diff --git a/apps/emqx_slow_subs/src/emqx_slow_subs.erl b/apps/emqx_slow_subs/src/emqx_slow_subs.erl index f0605baca..2a07518b3 100644 --- a/apps/emqx_slow_subs/src/emqx_slow_subs.erl +++ b/apps/emqx_slow_subs/src/emqx_slow_subs.erl @@ -21,6 +21,7 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). -include_lib("emqx_slow_subs/include/emqx_slow_subs.hrl"). +-include_lib("emqx/include/emqx_hooks.hrl"). -export([ start_link/0, @@ -212,7 +213,7 @@ load(State) -> threshold := Threshold } = emqx:get_config([slow_subs]), MaxSize = erlang:min(MaxSizeT, ?MAX_SIZE), - _ = emqx:hook( + ok = emqx_hooks:put( 'delivery.completed', {?MODULE, on_delivery_completed, [ #{ @@ -220,14 +221,15 @@ load(State) -> stats_type => StatsType, threshold => Threshold } - ]} + ]}, + ?HP_SLOW_SUB ), State1 = start_timer(expire_timer, fun expire_tick/0, State), State1#{enable := true, last_tick_at => ?NOW}. unload(#{expire_timer := ExpireTimer} = State) -> - emqx:unhook('delivery.completed', {?MODULE, on_delivery_completed}), + emqx_hooks:del('delivery.completed', {?MODULE, on_delivery_completed}), State#{ enable := false, expire_timer := cancel_timer(ExpireTimer) From 48f685a0dd4afac172da249708613f76b9810f8a Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 15 Jun 2022 23:28:33 +0800 Subject: [PATCH 3/4] fix: remove the APIs for emqx_hooks:put/2 and emqx_hooks:add/2 --- apps/emqx/src/emqx.erl | 29 --------- apps/emqx/src/emqx_hooks.erl | 54 +++++++--------- apps/emqx/test/emqx_SUITE.erl | 56 +++++++++-------- apps/emqx/test/emqx_hooks_SUITE.erl | 62 ++++++++++--------- apps/emqx_gateway/test/emqx_coap_SUITE.erl | 4 +- apps/emqx_gateway/test/emqx_exproto_SUITE.erl | 22 ++++--- lib-ee/emqx_license/src/emqx_license.erl | 7 ++- 7 files changed, 102 insertions(+), 132 deletions(-) diff --git a/apps/emqx/src/emqx.erl b/apps/emqx/src/emqx.erl index bd5bb083d..ed246cff5 100644 --- a/apps/emqx/src/emqx.erl +++ b/apps/emqx/src/emqx.erl @@ -49,10 +49,6 @@ %% Hooks API -export([ - hook/2, - hook/3, - hook/4, - unhook/2, run_hook/2, run_fold_hook/3 ]). @@ -160,31 +156,6 @@ subscribed(SubId, Topic) when is_atom(SubId); is_binary(SubId) -> %%-------------------------------------------------------------------- %% Hooks API %%-------------------------------------------------------------------- - --spec hook(emqx_hooks:hookpoint(), emqx_hooks:action()) -> ok | {error, already_exists}. -hook(HookPoint, Action) -> - emqx_hooks:add(HookPoint, Action). - --spec hook( - emqx_hooks:hookpoint(), - emqx_hooks:action(), - emqx_hooks:filter() | integer() | list() -) -> - ok | {error, already_exists}. -hook(HookPoint, Action, Priority) when is_integer(Priority) -> - emqx_hooks:add(HookPoint, Action, Priority); -hook(HookPoint, Action, {_M, _F, _A} = Filter) -> - emqx_hooks:add(HookPoint, Action, Filter). - --spec hook(emqx_hooks:hookpoint(), emqx_hooks:action(), emqx_hooks:filter(), integer()) -> - ok | {error, already_exists}. -hook(HookPoint, Action, Filter, Priority) -> - emqx_hooks:add(HookPoint, Action, Filter, Priority). - --spec unhook(emqx_hooks:hookpoint(), emqx_hooks:action() | {module(), atom()}) -> ok. -unhook(HookPoint, Action) -> - emqx_hooks:del(HookPoint, Action). - -spec run_hook(emqx_hooks:hookpoint(), list(any())) -> ok | stop. run_hook(HookPoint, Args) -> emqx_hooks:run(HookPoint, Args). diff --git a/apps/emqx/src/emqx_hooks.erl b/apps/emqx/src/emqx_hooks.erl index 632257c4f..b07353302 100644 --- a/apps/emqx/src/emqx_hooks.erl +++ b/apps/emqx/src/emqx_hooks.erl @@ -29,10 +29,8 @@ %% Hooks API -export([ - add/2, add/3, add/4, - put/2, put/3, put/4, del/2, @@ -121,44 +119,34 @@ callback_priority(#callback{priority = P}) -> P. %% Hooks API %%-------------------------------------------------------------------- -%% @doc `add/2,3,4` add a new hook, returns 'already_exists' if the hook exists. --spec add(hookpoint(), action() | callback()) -> ok_or_error(already_exists). -add(HookPoint, Callback) when is_record(Callback, callback) -> - gen_server:call(?SERVER, {add, HookPoint, Callback}, infinity); -add(HookPoint, Action) when is_function(Action); is_tuple(Action) -> - add(HookPoint, #callback{action = Action, priority = 0}). - --spec add(hookpoint(), action(), filter() | integer() | list()) -> +%% @doc `add/3,4` add a new hook, returns 'already_exists' if the hook exists. +-spec add(hookpoint(), action(), integer()) -> ok_or_error(already_exists). -add(HookPoint, Action, {_M, _F, _A} = Filter) -> - add(HookPoint, #callback{action = Action, filter = Filter, priority = 0}); add(HookPoint, Action, Priority) when is_integer(Priority) -> - add(HookPoint, #callback{action = Action, priority = Priority}). + do_add(HookPoint, #callback{action = Action, priority = Priority}). --spec add(hookpoint(), action(), filter(), integer()) -> +-spec add(hookpoint(), action(), integer(), filter()) -> ok_or_error(already_exists). -add(HookPoint, Action, Filter, Priority) when is_integer(Priority) -> - add(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}). +add(HookPoint, Action, Priority, Filter) when is_integer(Priority) -> + do_add(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}). -%% @doc `put/2,3,4` updates the existing hook, add it if not exists. --spec put(hookpoint(), action() | callback()) -> ok. -put(HookPoint, Callback) when is_record(Callback, callback) -> - case add(HookPoint, Callback) of +do_add(HookPoint, Callback) -> + gen_server:call(?SERVER, {add, HookPoint, Callback}, infinity). + +%% @doc `put/3,4` updates the existing hook, add it if not exists. +-spec put(hookpoint(), action(), integer()) -> ok. +put(HookPoint, Action, Priority) when is_integer(Priority) -> + do_put(HookPoint, #callback{action = Action, priority = Priority}). + +-spec put(hookpoint(), action(), integer(), filter()) -> ok. +put(HookPoint, Action, Priority, Filter) when is_integer(Priority) -> + do_put(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}). + +do_put(HookPoint, Callback) -> + case do_add(HookPoint, Callback) of ok -> ok; {error, already_exists} -> gen_server:call(?SERVER, {put, HookPoint, Callback}, infinity) - end; -put(HookPoint, Action) when is_function(Action); is_tuple(Action) -> - ?MODULE:put(HookPoint, #callback{action = Action, priority = 0}). - --spec put(hookpoint(), action(), filter() | integer() | list()) -> ok. -put(HookPoint, Action, {_M, _F, _A} = Filter) -> - ?MODULE:put(HookPoint, #callback{action = Action, filter = Filter, priority = 0}); -put(HookPoint, Action, Priority) when is_integer(Priority) -> - ?MODULE:put(HookPoint, #callback{action = Action, priority = Priority}). - --spec put(hookpoint(), action(), filter(), integer()) -> ok. -put(HookPoint, Action, Filter, Priority) when is_integer(Priority) -> - ?MODULE:put(HookPoint, #callback{action = Action, filter = Filter, priority = Priority}). + end. %% @doc Unregister a callback. -spec del(hookpoint(), action() | {module(), atom()}) -> ok. diff --git a/apps/emqx/test/emqx_SUITE.erl b/apps/emqx/test/emqx_SUITE.erl index 02e1c8f29..3eb4bf424 100644 --- a/apps/emqx/test/emqx_SUITE.erl +++ b/apps/emqx/test/emqx_SUITE.erl @@ -95,52 +95,54 @@ t_emqx_pubsub_api(_) -> ?assertEqual([], emqx:topics()). t_hook_unhook(_) -> - ok = emqx:hook(test_hook, {?MODULE, hook_fun1, []}), - ok = emqx:hook(test_hook, {?MODULE, hook_fun2, []}), + ok = emqx_hooks:add(test_hook, {?MODULE, hook_fun1, []}, 0), + ok = emqx_hooks:add(test_hook, {?MODULE, hook_fun2, []}, 0), ?assertEqual( {error, already_exists}, - emqx:hook(test_hook, {?MODULE, hook_fun2, []}) + emqx_hooks:add(test_hook, {?MODULE, hook_fun2, []}, 0) ), - ok = emqx:unhook(test_hook, {?MODULE, hook_fun1}), - ok = emqx:unhook(test_hook, {?MODULE, hook_fun2}), + ok = emqx_hooks:del(test_hook, {?MODULE, hook_fun1}), + ok = emqx_hooks:del(test_hook, {?MODULE, hook_fun2}), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun8, []}, 8), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun2, []}, 2), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun10, []}, 10), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun9, []}, 9), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun2, []}), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun8, []}), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun9, []}), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun10, []}). + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun8, []}, 8), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun2, []}, 2), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun10, []}, 10), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun9, []}, 9), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun2, []}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun8, []}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun9, []}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun10, []}). t_run_hook(_) -> - ok = emqx:hook(foldl_hook, {?MODULE, hook_fun3, [init]}), - ok = emqx:hook(foldl_hook, {?MODULE, hook_fun4, [init]}), - ok = emqx:hook(foldl_hook, {?MODULE, hook_fun5, [init]}), + ok = emqx_hooks:add(foldl_hook, {?MODULE, hook_fun3, [init]}, 0), + ok = emqx_hooks:add(foldl_hook, {?MODULE, hook_fun4, [init]}, 0), + ok = emqx_hooks:add(foldl_hook, {?MODULE, hook_fun5, [init]}, 0), [r5, r4] = emqx:run_fold_hook(foldl_hook, [arg1, arg2], []), [] = emqx:run_fold_hook(unknown_hook, [], []), - ok = emqx:hook(foldl_hook2, {?MODULE, hook_fun9, []}), - ok = emqx:hook(foldl_hook2, {?MODULE, hook_fun10, []}), + ok = emqx_hooks:add(foldl_hook2, {?MODULE, hook_fun9, []}, 0), + ok = emqx_hooks:add(foldl_hook2, {?MODULE, hook_fun10, []}, 0), [r10] = emqx:run_fold_hook(foldl_hook2, [arg], []), - ok = emqx:hook(foreach_hook, {?MODULE, hook_fun6, [initArg]}), - {error, already_exists} = emqx:hook(foreach_hook, {?MODULE, hook_fun6, [initArg]}), - ok = emqx:hook(foreach_hook, {?MODULE, hook_fun7, [initArg]}), - ok = emqx:hook(foreach_hook, {?MODULE, hook_fun8, [initArg]}), + ok = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun6, [initArg]}, 0), + {error, already_exists} = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun6, [initArg]}, 0), + ok = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun7, [initArg]}, 0), + ok = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun8, [initArg]}, 0), ok = emqx:run_hook(foreach_hook, [arg]), - ok = emqx:hook(foreach_filter1_hook, {?MODULE, hook_fun1, []}, {?MODULE, hook_filter1, []}, 0), + ok = emqx_hooks:add( + foreach_filter1_hook, {?MODULE, hook_fun1, []}, 0, {?MODULE, hook_filter1, []} + ), %% filter passed ?assertEqual(ok, emqx:run_hook(foreach_filter1_hook, [arg])), %% filter failed ?assertEqual(ok, emqx:run_hook(foreach_filter1_hook, [arg1])), - ok = emqx:hook( - foldl_filter2_hook, {?MODULE, hook_fun2, []}, {?MODULE, hook_filter2, [init_arg]} + ok = emqx_hooks:add( + foldl_filter2_hook, {?MODULE, hook_fun2, []}, 0, {?MODULE, hook_filter2, [init_arg]} ), - ok = emqx:hook( - foldl_filter2_hook, {?MODULE, hook_fun2_1, []}, {?MODULE, hook_filter2_1, [init_arg]} + ok = emqx_hooks:add( + foldl_filter2_hook, {?MODULE, hook_fun2_1, []}, 0, {?MODULE, hook_filter2_1, [init_arg]} ), ?assertEqual(3, emqx:run_fold_hook(foldl_filter2_hook, [arg], 1)), ?assertEqual(2, emqx:run_fold_hook(foldl_filter2_hook, [arg1], 1)). diff --git a/apps/emqx/test/emqx_hooks_SUITE.erl b/apps/emqx/test/emqx_hooks_SUITE.erl index e6c14d3d4..3c516fcfe 100644 --- a/apps/emqx/test/emqx_hooks_SUITE.erl +++ b/apps/emqx/test/emqx_hooks_SUITE.erl @@ -66,7 +66,7 @@ add_hook_order_prop() -> hooks(), try {ok, _} = emqx_hooks:start_link(), - [ok = emqx:hook(prop_hook, {M, F, []}, Prio) || {Prio, M, F} <- Hooks], + [ok = emqx_hooks:add(prop_hook, {M, F, []}, Prio) || {Prio, M, F} <- Hooks], Callbacks = emqx_hooks:lookup(prop_hook), Order = [{Prio, M, F} || {callback, {M, F, _}, _Filter, Prio} <- Callbacks], ?assertEqual( @@ -93,11 +93,11 @@ hooks() -> t_add_put_del_hook(_) -> {ok, _} = emqx_hooks:start_link(), - ok = emqx:hook(test_hook, {?MODULE, hook_fun1, []}), - ok = emqx:hook(test_hook, {?MODULE, hook_fun2, []}), + ok = emqx_hooks:add(test_hook, {?MODULE, hook_fun1, []}, 0), + ok = emqx_hooks:add(test_hook, {?MODULE, hook_fun2, []}, 0), ?assertEqual( {error, already_exists}, - emqx:hook(test_hook, {?MODULE, hook_fun2, []}) + emqx_hooks:add(test_hook, {?MODULE, hook_fun2, []}, 0) ), Callbacks0 = [ {callback, {?MODULE, hook_fun1, []}, undefined, 0}, @@ -105,23 +105,23 @@ t_add_put_del_hook(_) -> ], ?assertEqual(Callbacks0, emqx_hooks:lookup(test_hook)), - ok = emqx_hooks:put(test_hook, {?MODULE, hook_fun1, [test]}), - ok = emqx_hooks:put(test_hook, {?MODULE, hook_fun2, [test]}), + ok = emqx_hooks:put(test_hook, {?MODULE, hook_fun1, [test]}, 0), + ok = emqx_hooks:put(test_hook, {?MODULE, hook_fun2, [test]}, 0), Callbacks1 = [ {callback, {?MODULE, hook_fun1, [test]}, undefined, 0}, {callback, {?MODULE, hook_fun2, [test]}, undefined, 0} ], ?assertEqual(Callbacks1, emqx_hooks:lookup(test_hook)), - ok = emqx:unhook(test_hook, {?MODULE, hook_fun1}), - ok = emqx:unhook(test_hook, {?MODULE, hook_fun2}), + ok = emqx_hooks:del(test_hook, {?MODULE, hook_fun1}), + ok = emqx_hooks:del(test_hook, {?MODULE, hook_fun2}), timer:sleep(200), ?assertEqual([], emqx_hooks:lookup(test_hook)), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun8, []}, 8), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun2, []}, 2), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun10, []}, 10), - ok = emqx:hook(emqx_hook, {?MODULE, hook_fun9, []}, 9), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun8, []}, 8), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun2, []}, 2), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun10, []}, 10), + ok = emqx_hooks:add(emqx_hook, {?MODULE, hook_fun9, []}, 9), Callbacks2 = [ {callback, {?MODULE, hook_fun10, []}, undefined, 10}, {callback, {?MODULE, hook_fun9, []}, undefined, 9}, @@ -142,43 +142,45 @@ t_add_put_del_hook(_) -> ], ?assertEqual(Callbacks3, emqx_hooks:lookup(emqx_hook)), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun2, [test]}), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun8, [test]}), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun9, [test]}), - ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun10, [test]}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun2, [test]}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun8, [test]}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun9, [test]}), + ok = emqx_hooks:del(emqx_hook, {?MODULE, hook_fun10, [test]}), timer:sleep(200), ?assertEqual([], emqx_hooks:lookup(emqx_hook)). t_run_hooks(_) -> {ok, _} = emqx_hooks:start_link(), - ok = emqx:hook(foldl_hook, {?MODULE, hook_fun3, [init]}), - ok = emqx:hook(foldl_hook, {?MODULE, hook_fun4, [init]}), - ok = emqx:hook(foldl_hook, {?MODULE, hook_fun5, [init]}), + ok = emqx_hooks:add(foldl_hook, {?MODULE, hook_fun3, [init]}, 0), + ok = emqx_hooks:add(foldl_hook, {?MODULE, hook_fun4, [init]}, 0), + ok = emqx_hooks:add(foldl_hook, {?MODULE, hook_fun5, [init]}, 0), [r5, r4] = emqx:run_fold_hook(foldl_hook, [arg1, arg2], []), [] = emqx:run_fold_hook(unknown_hook, [], []), - ok = emqx:hook(foldl_hook2, {?MODULE, hook_fun9, []}), - ok = emqx:hook(foldl_hook2, {?MODULE, hook_fun10, []}), + ok = emqx_hooks:add(foldl_hook2, {?MODULE, hook_fun9, []}, 0), + ok = emqx_hooks:add(foldl_hook2, {?MODULE, hook_fun10, []}, 0), %% Note: 10 is _less_ than 9 per lexicographic order [r10] = emqx:run_fold_hook(foldl_hook2, [arg], []), - ok = emqx:hook(foreach_hook, {?MODULE, hook_fun6, [initArg]}), - {error, already_exists} = emqx:hook(foreach_hook, {?MODULE, hook_fun6, [initArg]}), - ok = emqx:hook(foreach_hook, {?MODULE, hook_fun7, [initArg]}), - ok = emqx:hook(foreach_hook, {?MODULE, hook_fun8, [initArg]}), + ok = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun6, [initArg]}, 0), + {error, already_exists} = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun6, [initArg]}, 0), + ok = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun7, [initArg]}, 0), + ok = emqx_hooks:add(foreach_hook, {?MODULE, hook_fun8, [initArg]}, 0), ok = emqx:run_hook(foreach_hook, [arg]), - ok = emqx:hook(foreach_filter1_hook, {?MODULE, hook_fun1, []}, {?MODULE, hook_filter1, []}, 0), + ok = emqx_hooks:add( + foreach_filter1_hook, {?MODULE, hook_fun1, []}, 0, {?MODULE, hook_filter1, []} + ), %% filter passed ?assertEqual(ok, emqx:run_hook(foreach_filter1_hook, [arg])), %% filter failed ?assertEqual(ok, emqx:run_hook(foreach_filter1_hook, [arg1])), - ok = emqx:hook( - foldl_filter2_hook, {?MODULE, hook_fun2, []}, {?MODULE, hook_filter2, [init_arg]} + ok = emqx_hooks:add( + foldl_filter2_hook, {?MODULE, hook_fun2, []}, 0, {?MODULE, hook_filter2, [init_arg]} ), - ok = emqx:hook( - foldl_filter2_hook, {?MODULE, hook_fun2_1, []}, {?MODULE, hook_filter2_1, [init_arg]} + ok = emqx_hooks:add( + foldl_filter2_hook, {?MODULE, hook_fun2_1, []}, 0, {?MODULE, hook_filter2_1, [init_arg]} ), ?assertEqual(3, emqx:run_fold_hook(foldl_filter2_hook, [arg], 1)), ?assertEqual(2, emqx:run_fold_hook(foldl_filter2_hook, [arg1], 1)). diff --git a/apps/emqx_gateway/test/emqx_coap_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_SUITE.erl index 672783865..127666c3a 100644 --- a/apps/emqx_gateway/test/emqx_coap_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_SUITE.erl @@ -330,8 +330,8 @@ t_clients_get_subscription_api(_) -> t_on_offline_event(_) -> Fun = fun(Channel) -> - emqx_hooks:add('client.connected', {emqx_sys, on_client_connected, []}), - emqx_hooks:add('client.disconnected', {emqx_sys, on_client_disconnected, []}), + emqx_hooks:add('client.connected', {emqx_sys, on_client_connected, []}, 1000), + emqx_hooks:add('client.disconnected', {emqx_sys, on_client_disconnected, []}, 1000), ConnectedSub = <<"$SYS/brokers/+/gateway/coap/clients/+/connected">>, emqx_broker:subscribe(ConnectedSub), diff --git a/apps/emqx_gateway/test/emqx_exproto_SUITE.erl b/apps/emqx_gateway/test/emqx_exproto_SUITE.erl index 9e1c3e6e9..8569a5567 100644 --- a/apps/emqx_gateway/test/emqx_exproto_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_exproto_SUITE.erl @@ -19,6 +19,8 @@ -compile(export_all). -compile(nowarn_export_all). +-include_lib("emqx/include/emqx_hooks.hrl"). + -import( emqx_exproto_echo_svr, [ @@ -263,8 +265,8 @@ t_hook_connected_disconnected(Cfg) -> ConnAckBin = frame_connack(0), Parent = self(), - emqx:hook('client.connected', {?MODULE, hook_fun1, [Parent]}), - emqx:hook('client.disconnected', {?MODULE, hook_fun2, [Parent]}), + emqx_hooks:add('client.connected', {?MODULE, hook_fun1, [Parent]}, 1000), + emqx_hooks:add('client.disconnected', {?MODULE, hook_fun2, [Parent]}, 1000), send(Sock, ConnBin), {ok, ConnAckBin} = recv(Sock, 5000), @@ -288,8 +290,8 @@ t_hook_connected_disconnected(Cfg) -> begin {error, closed} = recv(Sock, 5000) end, - emqx:unhook('client.connected', {?MODULE, hook_fun1}), - emqx:unhook('client.disconnected', {?MODULE, hook_fun2}). + emqx_hooks:del('client.connected', {?MODULE, hook_fun1}), + emqx_hooks:del('client.disconnected', {?MODULE, hook_fun2}). t_hook_session_subscribed_unsubscribed(Cfg) -> SockType = proplists:get_value(listener_type, Cfg), @@ -309,8 +311,8 @@ t_hook_session_subscribed_unsubscribed(Cfg) -> {ok, ConnAckBin} = recv(Sock, 5000), Parent = self(), - emqx:hook('session.subscribed', {?MODULE, hook_fun3, [Parent]}), - emqx:hook('session.unsubscribed', {?MODULE, hook_fun4, [Parent]}), + emqx_hooks:add('session.subscribed', {?MODULE, hook_fun3, [Parent]}, 1000), + emqx_hooks:add('session.unsubscribed', {?MODULE, hook_fun4, [Parent]}, 1000), SubBin = frame_subscribe(<<"t/#">>, 1), SubAckBin = frame_suback(0), @@ -337,8 +339,8 @@ t_hook_session_subscribed_unsubscribed(Cfg) -> end, close(Sock), - emqx:unhook('session.subscribed', {?MODULE, hook_fun3}), - emqx:unhook('session.unsubscribed', {?MODULE, hook_fun4}). + emqx_hooks:del('session.subscribed', {?MODULE, hook_fun3}), + emqx_hooks:del('session.unsubscribed', {?MODULE, hook_fun4}). t_hook_message_delivered(Cfg) -> SockType = proplists:get_value(listener_type, Cfg), @@ -363,14 +365,14 @@ t_hook_message_delivered(Cfg) -> send(Sock, SubBin), {ok, SubAckBin} = recv(Sock, 5000), - emqx:hook('message.delivered', {?MODULE, hook_fun5, []}), + emqx_hooks:add('message.delivered', {?MODULE, hook_fun5, []}, 1000), emqx:publish(emqx_message:make(<<"t/dn">>, <<"1">>)), PubBin1 = frame_publish(<<"t/dn">>, 0, <<"2">>), {ok, PubBin1} = recv(Sock, 5000), close(Sock), - emqx:unhook('message.delivered', {?MODULE, hook_fun5}). + emqx_hooks:del('message.delivered', {?MODULE, hook_fun5}). %%-------------------------------------------------------------------- %% Utils diff --git a/lib-ee/emqx_license/src/emqx_license.erl b/lib-ee/emqx_license/src/emqx_license.erl index c5fc5643e..787a8b283 100644 --- a/lib-ee/emqx_license/src/emqx_license.erl +++ b/lib-ee/emqx_license/src/emqx_license.erl @@ -27,6 +27,11 @@ -define(CONF_KEY_PATH, [license]). +%% Give the license app the highest priority. +%% We don't define it in the emqx_hooks.hrl becasue that is an opensource code +%% and can be changed by the communitiy. +-define(HP_LICENSE, 2000). + %%------------------------------------------------------------------------------ %% API %%------------------------------------------------------------------------------ @@ -114,7 +119,7 @@ post_config_update(_Path, _Cmd, NewConf, _Old, _AppEnvs) -> %%------------------------------------------------------------------------------ add_license_hook() -> - ok = emqx_hooks:put('client.connect', {?MODULE, check, []}). + ok = emqx_hooks:put('client.connect', {?MODULE, check, []}, ?HP_LICENSE). del_license_hook() -> _ = emqx_hooks:del('client.connect', {?MODULE, check, []}), From f59443eaee70a26c7ac36b95355a82ff66e43abe Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Thu, 16 Jun 2022 09:16:43 +0800 Subject: [PATCH 4/4] fix: add macro HP_HIGHEST=1000 --- apps/emqx/include/emqx_hooks.hrl | 34 ++++++++++--------- apps/emqx/test/props/prop_emqx_sys.erl | 2 +- apps/emqx_plugins/test/emqx_plugins_SUITE.erl | 2 +- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/apps/emqx/include/emqx_hooks.hrl b/apps/emqx/include/emqx_hooks.hrl index 08fa07bc8..3785a13de 100644 --- a/apps/emqx/include/emqx_hooks.hrl +++ b/apps/emqx/include/emqx_hooks.hrl @@ -16,23 +16,25 @@ %% Definitions for Hook Priorities -%% == Highest Priority --define(HP_PSK, 1000). --define(HP_REWRITE, 1000). --define(HP_AUTHN, 990). --define(HP_AUTHZ, 980). --define(HP_SYS_MSGS, 960). --define(HP_TOPIC_METRICS, 950). --define(HP_RETAINER, 940). --define(HP_AUTO_SUB, 930). +%% Highest Priority = 1000, don't change this value as the plugins may depend on it. +-define(HP_HIGHEST, 1000). +%% hooks used by the emqx core app +-define(HP_PSK, 990). +-define(HP_REWRITE, 980). +-define(HP_AUTHN, 970). +-define(HP_AUTHZ, 960). +-define(HP_SYS_MSGS, 950). +-define(HP_TOPIC_METRICS, 940). +-define(HP_RETAINER, 930). +-define(HP_AUTO_SUB, 920). -define(HP_RULE_ENGINE, 900). - %% apps that can work with the republish action --define(HP_SLOW_SUB, 980). --define(HP_BRIDGE, 970). --define(HP_DELAY_PUB, 960). - -%% apps that can stop the hooks +-define(HP_SLOW_SUB, 880). +-define(HP_BRIDGE, 870). +-define(HP_DELAY_PUB, 860). +%% apps that can stop the hooks chain from continuing -define(HP_EXHOOK, 100). -%% == Lowest Priority + +%% == Lowest Priority = 0, don't change this value as the plugins may depend on it. +-define(HP_LOWEST, 0). diff --git a/apps/emqx/test/props/prop_emqx_sys.erl b/apps/emqx/test/props/prop_emqx_sys.erl index c3a091ce3..c554cbcdf 100644 --- a/apps/emqx/test/props/prop_emqx_sys.erl +++ b/apps/emqx/test/props/prop_emqx_sys.erl @@ -114,7 +114,7 @@ do_mock(mria_mnesia) -> do_mock(emqx_metrics) -> meck:expect(emqx_metrics, all, fun() -> [{hello, 3}] end); do_mock(emqx_hooks) -> - meck:expect(emqx_hooks, put, fun(_HookPoint, _MFA) -> ok end), + meck:expect(emqx_hooks, put, fun(_HookPoint, _MFA, _) -> ok end), meck:expect(emqx_hooks, del, fun(_HookPoint, _MF) -> ok end); do_mock(emqx_config_handler) -> meck:expect(emqx_config_handler, add_handler, fun(_, _) -> ok end). diff --git a/apps/emqx_plugins/test/emqx_plugins_SUITE.erl b/apps/emqx_plugins/test/emqx_plugins_SUITE.erl index 317519124..39eea5fc2 100644 --- a/apps/emqx_plugins/test/emqx_plugins_SUITE.erl +++ b/apps/emqx_plugins/test/emqx_plugins_SUITE.erl @@ -22,7 +22,7 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("eunit/include/eunit.hrl"). --define(EMQX_PLUGIN_TEMPLATE_VSN, "5.0-rc.1"). +-define(EMQX_PLUGIN_TEMPLATE_VSN, "5.0.0-rc.3"). -define(EMQX_ELIXIR_PLUGIN_TEMPLATE_VSN, "0.1.0"). -define(PACKAGE_SUFFIX, ".tar.gz").