fix(bridge): reply `emqx_resource:get_instance/1` from cache

The resource manager may be busy at times, so this change ensures that
getting resource instance state will not block. Currently, no users of
`emqx_resource:get_instance/1` do seem to be relying on state being
"as-actual-as-possible" guarantee it was providing.
This commit is contained in:
Andrew Mayorov 2023-03-13 14:35:08 +03:00
parent baf39fe080
commit 686bf8255b
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
2 changed files with 16 additions and 4 deletions

View File

@ -309,7 +309,7 @@ set_resource_status_connecting(ResId) ->
-spec get_instance(resource_id()) ->
{ok, resource_group(), resource_data()} | {error, Reason :: term()}.
get_instance(ResId) ->
emqx_resource_manager:lookup(ResId).
emqx_resource_manager:ets_lookup(ResId, [metrics]).
-spec fetch_creation_opts(map()) -> creation_opts().
fetch_creation_opts(Opts) ->

View File

@ -36,6 +36,7 @@
list_all/0,
list_group/1,
ets_lookup/1,
ets_lookup/2,
get_metrics/1,
reset_metrics/1
]).
@ -229,14 +230,25 @@ set_resource_status_connecting(ResId) ->
-spec lookup(resource_id()) -> {ok, resource_group(), resource_data()} | {error, not_found}.
lookup(ResId) ->
case safe_call(ResId, lookup, ?T_LOOKUP) of
{error, timeout} -> ets_lookup(ResId);
{error, timeout} -> ets_lookup(ResId, [metrics]);
Result -> Result
end.
%% @doc Lookup the group and data of a resource
%% @doc Lookup the group and data of a resource from the cache
-spec ets_lookup(resource_id()) -> {ok, resource_group(), resource_data()} | {error, not_found}.
ets_lookup(ResId) ->
ets_lookup(ResId, []).
%% @doc Lookup the group and data of a resource from the cache
-spec ets_lookup(resource_id(), [Option]) ->
{ok, resource_group(), resource_data()} | {error, not_found}
when
Option :: metrics.
ets_lookup(ResId, Options) ->
NeedMetrics = lists:member(metrics, Options),
case read_cache(ResId) of
{Group, Data} when NeedMetrics ->
{ok, Group, data_record_to_external_map_with_metrics(Data)};
{Group, Data} ->
{ok, Group, data_record_to_external_map(Data)};
not_found ->
@ -253,7 +265,7 @@ reset_metrics(ResId) ->
emqx_metrics_worker:reset_metrics(?RES_METRICS, ResId).
%% @doc Returns the data for all resources
-spec list_all() -> [resource_data()] | [].
-spec list_all() -> [resource_data()].
list_all() ->
try
[