Jelajahi Sumber

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

Zaiming (Stone) Shi 3 tahun lalu
induk
melakukan
fb61c2b266

+ 1 - 1
apps/emqx_resource/include/emqx_resource.hrl

@@ -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() :: #{

+ 9 - 5
apps/emqx_resource/src/emqx_resource_manager.erl

@@ -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).