From ab76e7978b3d20adc844f49cb512b7e2114c920b Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Thu, 23 Feb 2017 16:53:09 +0800 Subject: [PATCH] Add emqttd_gc module --- src/emqttd_gc.erl | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/emqttd_gc.erl diff --git a/src/emqttd_gc.erl b/src/emqttd_gc.erl new file mode 100644 index 000000000..339e895f3 --- /dev/null +++ b/src/emqttd_gc.erl @@ -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 "). + +-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. +