Feng 10 سال پیش
والد
کامیت
fcc0bb98e2
1فایلهای تغییر یافته به همراه13 افزوده شده و 8 حذف شده
  1. 13 8
      src/emqttd_stats.erl

+ 13 - 8
src/emqttd_stats.erl

@@ -24,6 +24,7 @@
 %%%
 %%%
 %%% @end
 %%% @end
 %%%-----------------------------------------------------------------------------
 %%%-----------------------------------------------------------------------------
+
 -module(emqttd_stats).
 -module(emqttd_stats).
 
 
 -author("Feng Lee <feng@emqtt.io>").
 -author("Feng Lee <feng@emqtt.io>").
@@ -124,18 +125,11 @@ setstat(Stat, Val) ->
 
 
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 %% @doc Set stats with max
 %% @doc Set stats with max
-%% TODO: this is wrong...
 %% @end
 %% @end
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 -spec setstats(Stat :: atom(), MaxStat :: atom(), Val :: pos_integer()) -> boolean().
 -spec setstats(Stat :: atom(), MaxStat :: atom(), Val :: pos_integer()) -> boolean().
 setstats(Stat, MaxStat, Val) ->
 setstats(Stat, MaxStat, Val) ->
-    MaxVal = ets:lookup_element(?STATS_TAB, MaxStat, 2),
-    if
-        Val > MaxVal -> 
-            ets:update_element(?STATS_TAB, MaxStat, {2, Val});
-        true -> ok
-    end,
-    ets:update_element(?STATS_TAB, Stat, {2, Val}).
+    gen_server:cast(?MODULE, {setstats, Stat, MaxStat, Val}).
 
 
 %%%=============================================================================
 %%%=============================================================================
 %%% gen_server callbacks
 %%% gen_server callbacks
@@ -154,6 +148,17 @@ init([]) ->
 handle_call(_Request, _From, State) ->
 handle_call(_Request, _From, State) ->
     {reply, error, State}.
     {reply, error, State}.
 
 
+%% atomic
+handle_cast({setstats, Stat, MaxStat, Val}, State) ->
+    MaxVal = ets:lookup_element(?STATS_TAB, MaxStat, 2),
+    if
+        Val > MaxVal ->
+            ets:update_element(?STATS_TAB, MaxStat, {2, Val});
+        true -> ok
+    end,
+    ets:update_element(?STATS_TAB, Stat, {2, Val}),
+    {noreply, State};
+
 handle_cast(_Msg, State) ->
 handle_cast(_Msg, State) ->
     {noreply, State}.
     {noreply, State}.