Browse Source

Wrap `os:type` for cpu:sup call

GilbertWong 6 năm trước cách đây
mục cha
commit
6e0a269ff6
1 tập tin đã thay đổi với 11 bổ sung13 xóa
  1. 11 13
      src/emqx_vm.erl

+ 11 - 13
src/emqx_vm.erl

@@ -451,26 +451,24 @@ mapping([{Key, Value}|Entries], Acc) ->
     mapping(Entries, [{Key, Value}|Acc]).
 
 avg1() ->
-   case cpu_sup:avg1() of
-        V when is_integer(V) -> V;
-        {error, _Reason} -> 0.00
-    end.
+    compat_windows(fun cpu_sup:avg1/0).
 
 avg5() ->
-    case cpu_sup:avg5() of
-        V when is_integer(V) -> V;
-        {error, _Reason} -> 0.00
-    end.
+    compat_windows(fun cpu_sup:avg5/0).
 
 avg15() ->
-    case cpu_sup:avg15() of
-        V when is_integer(V) -> V;
-        {error, _Reason} -> 0.00
-    end.
+    compat_windows(fun cpu_sup:avg15/0).
 
 cpu_util() ->
+    compat_windows(fun cpu_sup:util/0).
+
+compat_windows(Fun) ->
     case os:type() of
         {win32, nt} -> 0;
-        _Other -> cpu_sup:util()
+        _Other -> handle_error(Fun())
     end.
 
+handle_error(Value) when is_number(Value) ->
+    Value;
+handle_error({error, _Reason}) ->
+    0.