From 55d4e54bef2612669fc6dfac734e23eda8486161 Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Tue, 19 Apr 2022 10:54:23 +0200 Subject: [PATCH] Use mongoose_cleaner for mod_stream_management_cets --- src/ejabberd_sm_cets.erl | 2 +- .../mod_stream_management_cets.erl | 60 ++++--------------- 2 files changed, 14 insertions(+), 48 deletions(-) diff --git a/src/ejabberd_sm_cets.erl b/src/ejabberd_sm_cets.erl index 91faaa25b2..a307699a66 100644 --- a/src/ejabberd_sm_cets.erl +++ b/src/ejabberd_sm_cets.erl @@ -19,7 +19,7 @@ -define(TABLE, cets_session). --spec init(list()) -> any(). +-spec init(map()) -> any(). init(_Opts) -> cets:start(?TABLE, #{}), cets_discovery:add_table(mongoose_cets_discovery, ?TABLE). diff --git a/src/stream_management/mod_stream_management_cets.erl b/src/stream_management/mod_stream_management_cets.erl index e7d105b2cb..49fe98f7ef 100644 --- a/src/stream_management/mod_stream_management_cets.erl +++ b/src/stream_management/mod_stream_management_cets.erl @@ -1,6 +1,5 @@ -module(mod_stream_management_cets). -behaviour(mod_stream_management_backend). --behaviour(gen_server). -include("mongoose.hrl"). -include("jlib.hrl"). @@ -15,32 +14,24 @@ write_stale_h/3, delete_stale_h/2]). -%% Internal exports --export([start_link/1]). - -%% gen_server callbacks --export([init/1, handle_call/3, handle_cast/2, handle_info/2]). +-export([clear_table/2]). -ignore_xref([start_link/1]). --record(smgc_state, - {gc_repeat_after :: non_neg_integer(), - gc_geriatric :: non_neg_integer() }). - -define(TABLE, cets_strm_man). -define(TABLE_H, cets_strm_man_h). -init(_HostType, Opts = #{stale_h := StaleOpts}) -> +init(HostType, #{stale_h := StaleOpts}) -> cets:start(?TABLE, #{}), cets_discovery:add_table(mongoose_cets_discovery, ?TABLE), - maybe_init_stale_h(StaleOpts), + maybe_init_stale_h(HostType, StaleOpts), ok. -maybe_init_stale_h(StaleOpts = #{enabled := true}) -> +maybe_init_stale_h(HostType, StaleOpts = #{enabled := true}) -> cets:start(?TABLE_H, #{}), cets_discovery:add_table(mongoose_cets_discovery, ?TABLE_H), - start_cleaner(StaleOpts); -maybe_init_stale_h(_) -> ok. + start_cleaner(HostType, StaleOpts); +maybe_init_stale_h(_, _) -> ok. -spec register_smid(HostType, SMID, SID) -> ok | {error, term()} when @@ -101,41 +92,16 @@ delete_stale_h(_HostType, SMID) -> %% stale_h cleaning logic -start_cleaner(Opts) -> - MFA = {?MODULE, start_link, [Opts]}, - ChildSpec = {stream_management_stale_h, MFA, permanent, 5000, worker, [?MODULE]}, +start_cleaner(HostType, #{repeat_after := Interval, geriatric := TTL}) -> + Name = gen_mod:get_module_proc(HostType, stream_management_stale_h), + WOpts = #{host_type => HostType, action => fun ?MODULE:clear_table/2, + opts => TTL, interval => Interval}, + MFA = {mongoose_collector, start_link, [Name, WOpts]}, + ChildSpec = {Name, MFA, permanent, 5000, worker, [?MODULE]}, %% TODO cleaner should be a service ejabberd_sup:start_child(ChildSpec). -start_link(Opts) -> - gen_server:start_link({local, stream_management_stale_h}, ?MODULE, Opts, []). - -init(#{repeat_after := RepeatAfter, geriatric := GeriatricAge}) -> - State = #smgc_state{gc_repeat_after = RepeatAfter, - gc_geriatric = GeriatricAge}, - schedule_check(State), - {ok, State}. - -handle_call(Msg, From, State) -> - ?UNEXPECTED_CALL(Msg, From), - {reply, ok, State}. - -handle_cast(Msg, State) -> - ?UNEXPECTED_CAST(Msg), - {noreply, State}. - -handle_info(check, #smgc_state{gc_geriatric = GeriatricAge} = State) -> - clear_table(GeriatricAge), - schedule_check(State), - {noreply, State}; -handle_info(Info, State) -> - ?UNEXPECTED_INFO(Info), - {noreply, State}. - -schedule_check(#smgc_state{gc_repeat_after = RepeatAfter}) -> - erlang:send_after(RepeatAfter * 1000, self(), check). - -clear_table(GeriatricAge) -> +clear_table(_HostType, GeriatricAge) -> TimeToDie = erlang:monotonic_time(second) - GeriatricAge, MS = ets:fun2ms(fun({_, _, S}) when S < TimeToDie -> true end), ets:select_delete(?TABLE_H, MS).