Skip to content

Commit

Permalink
Use mongoose_cleaner for mod_stream_management_cets
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Apr 27, 2022
1 parent 3302296 commit 55d4e54
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/ejabberd_sm_cets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
60 changes: 13 additions & 47 deletions src/stream_management/mod_stream_management_cets.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-module(mod_stream_management_cets).
-behaviour(mod_stream_management_backend).
-behaviour(gen_server).

-include("mongoose.hrl").
-include("jlib.hrl").
Expand All @@ -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
Expand Down Expand Up @@ -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).

0 comments on commit 55d4e54

Please sign in to comment.