Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAM shaper touches #3641

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions src/ejabberd_shaper_sup.erl

This file was deleted.

6 changes: 3 additions & 3 deletions src/ejabberd_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ init([]) ->
worker,
[mod_muc_iq]},
ShaperSup =
{ejabberd_shaper_sup,
{ejabberd_shaper_sup, start_link, []},
permanent, infinity, supervisor, [ejabberd_shaper_sup]},
{mongoose_shaper_sup,
{mongoose_shaper_sup, start_link, []},
permanent, infinity, supervisor, [mongoose_shaper_sup]},
PG =
{pg,
{pg, start_link, [mim_scope]},
Expand Down
17 changes: 4 additions & 13 deletions src/mam/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@

-ifdef(MAM_INLINE_UTILS).
-compile({inline, [
rsm_ns_binary/0,
mam_ns_binary/0,
is_archived_elem_for/2,
is_valid_message/4,
is_valid_message_type/3,
is_valid_message_children/3,
encode_compact_uuid/2,
get_one_of_path/3,
delay/2,
Expand All @@ -135,10 +131,6 @@

-export_type([direction/0, retraction_id/0, retraction_info/0]).

%% Constants
rsm_ns_binary() -> <<"http://jabber.org/protocol/rsm">>.


%% ----------------------------------------------------------------------
%% Datetime types
-type ne_binary() :: <<_:8, _:_*8>>.
Expand Down Expand Up @@ -510,7 +502,7 @@ result_set(FirstId, LastId, undefined, undefined)
|| LastId =/= undefined],
#xmlel{
name = <<"set">>,
attrs = [{<<"xmlns">>, rsm_ns_binary()}],
attrs = [{<<"xmlns">>, ?NS_RSM}],
children = FirstEl ++ LastEl};
result_set(FirstId, LastId, FirstIndexI, CountI)
when ?MAYBE_BIN(FirstId), ?MAYBE_BIN(LastId) ->
Expand All @@ -528,7 +520,7 @@ result_set(FirstId, LastId, FirstIndexI, CountI)
children = [#xmlcdata{content = integer_to_binary(CountI)}]},
#xmlel{
name = <<"set">>,
attrs = [{<<"xmlns">>, rsm_ns_binary()}],
attrs = [{<<"xmlns">>, ?NS_RSM}],
children = FirstEl ++ LastEl ++ [CountEl]}.


Expand Down Expand Up @@ -1111,14 +1103,13 @@ maybe_set_client_xmlns(true, Packet) ->
maybe_set_client_xmlns(false, Packet) ->
Packet.


-spec action_to_shaper_name(mam_iq:action()) -> atom().
action_to_shaper_name(Action) ->
list_to_atom(atom_to_list(Action) ++ "_shaper").

-spec action_to_global_shaper_name(mam_iq:action()) -> atom().
action_to_global_shaper_name(Action) -> list_to_atom(atom_to_list(Action) ++ "_global_shaper").

action_to_global_shaper_name(Action) ->
list_to_atom(atom_to_list(Action) ++ "_global_shaper").

-spec wait_shaper(mongooseim:host_type(), jid:server(), mam_iq:action(), jid:jid()) ->
'ok' | {'error', 'max_delay_reached'}.
Expand Down
58 changes: 58 additions & 0 deletions src/mongoose_shaper_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-module(mongoose_shaper_sup).

-behaviour(supervisor).

%% API
-export([start_link/0, get_workers/0, select_worker/1]).
-ignore_xref([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

-spec start_link() -> {ok, pid()} | ignore | {error, term()}.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

-spec init([]) -> {ok, {#{strategy => one_for_one, intensity => 100, period => 5},
[supervisor:child_spec()]}}.
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 100, period => 5},
WorkerNames = worker_names(),
TupleNames = list_to_tuple(WorkerNames),
persistent_term:put(?MODULE, TupleNames),
Shapers = [child_spec(Name) || Name <- WorkerNames],
{ok, { SupFlags, Shapers }}.

-spec child_spec(atom()) -> supervisor:child_spec().
child_spec(ProcName) ->
#{id => ProcName,
start => {shaper_srv, start_link, [ProcName]},
restart => permanent,
shutdown => 5000,
type => worker,
modules => [shaper_srv]}.

-spec get_workers() -> [atom()].
get_workers() ->
tuple_to_list(persistent_term:get(?MODULE)).

-spec select_worker(term()) -> atom().
select_worker(Key) ->
N = 1 + erlang:phash2(Key, worker_count()),
Workers = persistent_term:get(?MODULE),
element(N, Workers).

-spec worker_names() -> [atom()].
worker_names() ->
[build_worker_name(N) || N <- lists:seq(1, worker_count())].

-spec build_worker_name(integer()) -> atom().
build_worker_name(N) ->
list_to_atom(worker_prefix() ++ integer_to_list(N)).

-spec worker_prefix() -> string().
worker_prefix() ->
"mongoose_shaper_".

worker_count() ->
10.
Loading