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:
parent
baf39fe080
commit
686bf8255b
|
|
@ -309,7 +309,7 @@ set_resource_status_connecting(ResId) ->
|
||||||
-spec get_instance(resource_id()) ->
|
-spec get_instance(resource_id()) ->
|
||||||
{ok, resource_group(), resource_data()} | {error, Reason :: term()}.
|
{ok, resource_group(), resource_data()} | {error, Reason :: term()}.
|
||||||
get_instance(ResId) ->
|
get_instance(ResId) ->
|
||||||
emqx_resource_manager:lookup(ResId).
|
emqx_resource_manager:ets_lookup(ResId, [metrics]).
|
||||||
|
|
||||||
-spec fetch_creation_opts(map()) -> creation_opts().
|
-spec fetch_creation_opts(map()) -> creation_opts().
|
||||||
fetch_creation_opts(Opts) ->
|
fetch_creation_opts(Opts) ->
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@
|
||||||
list_all/0,
|
list_all/0,
|
||||||
list_group/1,
|
list_group/1,
|
||||||
ets_lookup/1,
|
ets_lookup/1,
|
||||||
|
ets_lookup/2,
|
||||||
get_metrics/1,
|
get_metrics/1,
|
||||||
reset_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}.
|
-spec lookup(resource_id()) -> {ok, resource_group(), resource_data()} | {error, not_found}.
|
||||||
lookup(ResId) ->
|
lookup(ResId) ->
|
||||||
case safe_call(ResId, lookup, ?T_LOOKUP) of
|
case safe_call(ResId, lookup, ?T_LOOKUP) of
|
||||||
{error, timeout} -> ets_lookup(ResId);
|
{error, timeout} -> ets_lookup(ResId, [metrics]);
|
||||||
Result -> Result
|
Result -> Result
|
||||||
end.
|
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}.
|
-spec ets_lookup(resource_id()) -> {ok, resource_group(), resource_data()} | {error, not_found}.
|
||||||
ets_lookup(ResId) ->
|
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
|
case read_cache(ResId) of
|
||||||
|
{Group, Data} when NeedMetrics ->
|
||||||
|
{ok, Group, data_record_to_external_map_with_metrics(Data)};
|
||||||
{Group, Data} ->
|
{Group, Data} ->
|
||||||
{ok, Group, data_record_to_external_map(Data)};
|
{ok, Group, data_record_to_external_map(Data)};
|
||||||
not_found ->
|
not_found ->
|
||||||
|
|
@ -253,7 +265,7 @@ reset_metrics(ResId) ->
|
||||||
emqx_metrics_worker:reset_metrics(?RES_METRICS, ResId).
|
emqx_metrics_worker:reset_metrics(?RES_METRICS, ResId).
|
||||||
|
|
||||||
%% @doc Returns the data for all resources
|
%% @doc Returns the data for all resources
|
||||||
-spec list_all() -> [resource_data()] | [].
|
-spec list_all() -> [resource_data()].
|
||||||
list_all() ->
|
list_all() ->
|
||||||
try
|
try
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue