Add emqttd_gc module

This commit is contained in:
Feng Lee 2017-02-23 16:53:09 +08:00
parent 2d9dbe4729
commit ab76e7978b
1 changed files with 46 additions and 0 deletions

46
src/emqttd_gc.erl Normal file
View File

@ -0,0 +1,46 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
%% GC Utility functions.
-module(emqttd_gc).
-author("Feng Lee <feng@emqtt.io>").
-export([conn_max_gc_count/0, reset_conn_gc_count/2, maybe_force_gc/2]).
-spec(conn_max_gc_count() -> integer()).
conn_max_gc_count() ->
case emqttd:env(conn_force_gc_count) of
undefined -> undefined;
I when I > 0 -> I + rand:uniform(I)
end.
-spec(reset_conn_gc_count(pos_integer(), tuple()) -> tuple()).
reset_conn_gc_count(Pos, State) ->
case element(Pos, State) of
undefined -> State;
_I -> setelement(Pos, State, conn_max_gc_count())
end.
maybe_force_gc(Pos, State) ->
case element(Pos, State) of
undefined -> State;
I when I =< 0 -> garbage_collect(),
reset_conn_gc_count(Pos, State);
I -> setelement(Pos, State, I - 1)
end.