From 62ecf6f545652bcff3914852f22cac5f66e3aeb0 Mon Sep 17 00:00:00 2001 From: JimMoen Date: Mon, 22 Aug 2022 02:34:25 +0800 Subject: [PATCH] fix(resource): keep `auto_retry` in `disconnected` state Automatic retries should be maintained even in `disconnected` state without any state transition. --- .../src/emqx_resource_manager.erl | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/apps/emqx_resource/src/emqx_resource_manager.erl b/apps/emqx_resource/src/emqx_resource_manager.erl index 382cf29d9..226f9e927 100644 --- a/apps/emqx_resource/src/emqx_resource_manager.erl +++ b/apps/emqx_resource/src/emqx_resource_manager.erl @@ -384,9 +384,33 @@ handle_event(EventType, EventData, State, Data) -> %%------------------------------------------------------------------------------ insert_cache(ResId, Group, Data = #data{manager_id = MgrId}) -> case get_owner(ResId) of - not_found -> ets:insert(?ETS_TABLE, {ResId, Group, Data}); - MgrId -> ets:insert(?ETS_TABLE, {ResId, Group, Data}); - _ -> self() ! quit + not_found -> + ?SLOG( + debug, + #{ + msg => resource_owner_not_found, + resource_id => ResId, + action => auto_insert_cache + } + ), + ets:insert(?ETS_TABLE, {ResId, Group, Data}); + MgrId -> + ?SLOG( + debug, + #{ + msg => resource_owner_matched, + resource_id => ResId, + action => reinsert_cache + } + ), + ets:insert(?ETS_TABLE, {ResId, Group, Data}); + _ -> + ?SLOG(error, #{ + msg => get_resource_owner_failed, + resource_id => ResId, + action => quit_rusource + }), + self() ! quit end. read_cache(ResId) -> @@ -425,12 +449,14 @@ get_owner(ResId) -> end. handle_disconnected_state_enter(Data) -> + {next_state, disconnected, Data, retry_actions(Data)}. + +retry_actions(Data) -> case maps:get(auto_restart_interval, Data#data.opts, ?AUTO_RESTART_INTERVAL) of undefined -> - {next_state, disconnected, Data}; + []; RetryInterval -> - Actions = [{state_timeout, RetryInterval, auto_retry}], - {next_state, disconnected, Data, Actions} + [{state_timeout, RetryInterval, auto_retry}] end. handle_remove_event(From, ClearMetrics, Data) -> @@ -461,7 +487,7 @@ start_resource(Data, From) -> %% Keep track of the error reason why the connection did not work %% so that the Reason can be returned when the verification call is made. UpdatedData = Data#data{error = Reason}, - Actions = maybe_reply([], From, Err), + Actions = maybe_reply(retry_actions(UpdatedData), From, Err), {next_state, disconnected, UpdatedData, Actions} end.