This commit is contained in:
Feng 2015-07-07 15:36:34 +08:00
parent ed81eb5a9d
commit fcc0bb98e2
1 changed files with 13 additions and 8 deletions

View File

@ -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), gen_server:cast(?MODULE, {setstats, Stat, MaxStat, Val}).
if
Val > MaxVal ->
ets:update_element(?STATS_TAB, MaxStat, {2, Val});
true -> ok
end,
ets:update_element(?STATS_TAB, Stat, {2, 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}.