perf: avoid getting metrics (gen_server:call) for each resource lookup

This commit is contained in:
Zaiming (Stone) Shi 2023-02-10 17:05:09 +01:00
parent fe450ca2d9
commit fb61c2b266
2 changed files with 10 additions and 6 deletions

View File

@ -43,7 +43,7 @@
config := resource_config(),
state := resource_state(),
status := resource_status(),
metrics := emqx_metrics_worker:metrics()
metrics => emqx_metrics_worker:metrics()
}.
-type resource_group() :: binary().
-type creation_opts() :: #{

View File

@ -238,7 +238,7 @@ lookup(ResId) ->
ets_lookup(ResId) ->
case read_cache(ResId) of
{Group, Data} ->
{ok, Group, data_record_to_external_map_with_metrics(Data)};
{ok, Group, data_record_to_external_map(Data)};
not_found ->
{error, not_found}
end.
@ -620,8 +620,8 @@ maybe_reply(Actions, undefined, _Reply) ->
maybe_reply(Actions, From, Reply) ->
[{reply, From, Reply} | Actions].
-spec data_record_to_external_map_with_metrics(data()) -> resource_data().
data_record_to_external_map_with_metrics(Data) ->
-spec data_record_to_external_map(data()) -> resource_data().
data_record_to_external_map(Data) ->
#{
id => Data#data.id,
mod => Data#data.mod,
@ -629,10 +629,14 @@ data_record_to_external_map_with_metrics(Data) ->
query_mode => Data#data.query_mode,
config => Data#data.config,
status => Data#data.status,
state => Data#data.state,
metrics => get_metrics(Data#data.id)
state => Data#data.state
}.
-spec data_record_to_external_map_with_metrics(data()) -> resource_data().
data_record_to_external_map_with_metrics(Data) ->
DataMap = data_record_to_external_map(Data),
DataMap#{metrics => get_metrics(Data#data.id)}.
-spec wait_for_ready(resource_id(), integer()) -> ok | timeout | {error, term()}.
wait_for_ready(ResId, WaitTime) ->
do_wait_for_ready(ResId, WaitTime div ?WAIT_FOR_RESOURCE_DELAY).