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

Minor optimisations and cleanups #4480

Merged
merged 3 commits into from
Feb 7, 2025
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
6 changes: 2 additions & 4 deletions src/component/mongoose_component_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,8 @@ handle_route(StateData = #component_data{}, _, Acc) ->
case acl:match_rule(global, Access, From) of
allow ->
mongoose_hooks:packet_to_component(Acc, From, To),
Attrs2 = jlib:replace_from_to_attrs(jid:to_binary(From),
jid:to_binary(To),
Packet#xmlel.attrs),
send_xml(StateData, Packet#xmlel{attrs = Attrs2});
Packet2 = jlib:replace_from_to(From, To, Packet),
send_xml(StateData, Packet2);
deny ->
ejabberd_router:route_error_reply(To, From, Acc, mongoose_xmpp_errors:not_allowed())
end,
Expand Down
2 changes: 2 additions & 0 deletions src/domain/mongoose_domain_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-export([start_link/2, restart_core/1]).
-ignore_xref([start_link/2, restart_core/1]).

-spec start_link() -> supervisor:startlink_ret().
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

Expand All @@ -21,6 +22,7 @@ restart_core(Args) ->
supervisor:delete_child(?MODULE, mongoose_domain_core),
supervisor:start_child(?MODULE, worker_spec(mongoose_domain_core, fill_args(Args))).

-spec init(list()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(Args) ->
DomainCore = worker_spec(mongoose_domain_core, fill_args(Args)),
SubdomainCore = worker_spec(mongoose_subdomain_core, []),
Expand Down
41 changes: 4 additions & 37 deletions src/ejabberd_local.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
-export([disco_local_features/3]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).

-export([do_route/4]).

Expand Down Expand Up @@ -89,13 +88,9 @@
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok, Pid} | ignore | {error, Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
-spec start_link() -> 'ignore' | {'error', _} | {'ok', pid()}.
-spec start_link() -> gen_server:start_ret().
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
gen_server:start_link({local, ?MODULE}, ?MODULE, noargs, []).

-spec process_iq(Acc :: mongoose_acc:t(),
From :: jid:jid(),
Expand Down Expand Up @@ -261,22 +256,13 @@ disco_local_features(Acc, _, _) ->
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
init(noargs) ->
catch ets:new(?IQTABLE, [named_table, protected, {read_concurrency, true}]),
catch ets:new(?NSTABLE, [named_table, bag, protected, {read_concurrency, true}]),
catch ets:new(?IQRESPONSE, [named_table, public]),
gen_hook:add_handlers(hooks()),
{ok, #state{}}.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call({unregister_host, Host}, _From, State) ->
Node = node(),
[mongoose_c2s:stop(Pid, host_was_unregistered)
Expand All @@ -293,21 +279,9 @@ handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.

%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info({route, Acc, From, To, El}, State) ->
spawn(fun() -> process_packet(Acc, From, To, El, #{}) end),
{noreply, State};
Expand Down Expand Up @@ -341,13 +315,6 @@ handle_info(_Info, State) ->
terminate(_Reason, _State) ->
gen_hook:delete_handlers(hooks()).

%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
Expand Down
14 changes: 5 additions & 9 deletions src/ejabberd_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
-export([start_link/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).

-ignore_xref([route_error/4, start_link/0]).

Expand All @@ -49,9 +49,9 @@
%% API
%%====================================================================

-spec start_link() -> 'ignore' | {'error', _} | {'ok', pid()}.
-spec start_link() -> gen_server:start_ret().
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
gen_server:start_link({local, ?MODULE}, ?MODULE, noargs, [{hibernate_after, 0}]).

%% @doc The main routing function. It puts the message through a chain
%% of filtering/routing modules, as defined in config 'routing_modules'
Expand Down Expand Up @@ -133,13 +133,12 @@
%% gen_server callbacks
%%====================================================================

init([]) ->
init(noargs) ->
mongoose_component:start(),
{ok, #state{}}.

handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
{reply, ok, State}.

Check warning on line 141 in src/ejabberd_router.erl

View check run for this annotation

Codecov / codecov/patch

src/ejabberd_router.erl#L141

Added line #L141 was not covered by tests

handle_cast(_Msg, State) ->
{noreply, State}.
Expand All @@ -151,9 +150,6 @@
mongoose_component:stop(),
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ start() ->
Spec = {?MODULE, {?MODULE, start_link, []}, permanent, brutal_kill, worker, [?MODULE]},
{ok, _} = ejabberd_sup:start_child(Spec).

-spec start_link() -> 'ignore' | {'error', _} | {'ok', pid()}.
-spec start_link() -> gen_server:start_ret().
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

Expand Down
32 changes: 4 additions & 28 deletions src/ejabberd_sm_backend_sup.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
%%%-------------------------------------------------------------------
%%% @author Konrad Kaplita
%%% @copyright (C) 2011, Konrad Kaplita
%%% @doc
%%%
%%% @end
%%% Created : 18 Nov 2011 by Konrad Kaplita
%%%-------------------------------------------------------------------
-module(ejabberd_sm_backend_sup).
Expand All @@ -20,32 +17,11 @@

-define(SERVER, ?MODULE).

%%%===================================================================
%%% API functions
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the supervisor
%% @end
%%--------------------------------------------------------------------
-spec start_link() -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}.
-spec start_link() -> supervisor:startlink_ret().
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
supervisor:start_link({local, ?SERVER}, ?MODULE, noargs).

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2, 3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%% @end
%%--------------------------------------------------------------------
-spec init([]) -> {ok, {{one_for_one, 1000, 3600}, []}}.
init([]) ->
-spec init(noargs) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(noargs) ->
{ok, {{one_for_one, 1000, 3600}, []}}.
6 changes: 4 additions & 2 deletions src/ejabberd_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@

-include("mongoose_logger.hrl").

-spec start_link() -> supervisor:startlink_err().
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
supervisor:start_link({local, ?MODULE}, ?MODULE, noargs).

init([]) ->
-spec init(noargs) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(noargs) ->
Hooks = worker_spec(gen_hook),
Instrument = worker_spec(mongoose_instrument),
Cleaner = worker_spec(mongoose_cleaner),
Expand Down
7 changes: 2 additions & 5 deletions src/gen_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
-export([init/1,
handle_call/3,
handle_cast/2,
code_change/3,
handle_info/2,
terminate/2]).

Expand Down Expand Up @@ -76,6 +75,7 @@
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
-spec start_link() -> gen_server:start_ret().
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

Expand Down Expand Up @@ -200,7 +200,7 @@ handle_call({delete_handler, Key = {Name, Tag}, #hook_handler{} = HookHandler},
{reply, ok, NewState};
handle_call(reload_hooks, _From, State) ->
persistent_term:put(?MODULE, State),
{reply, ok, State};
{reply, ok, State, hibernate};
handle_call(Request, From, State) ->
?UNEXPECTED_CALL(Request, From),
{reply, bad_request, State}.
Expand All @@ -217,9 +217,6 @@ terminate(_Reason, _State) ->
persistent_term:erase(?MODULE),
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------
Expand Down
11 changes: 4 additions & 7 deletions src/instrument/mongoose_instrument.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
-export([add_handler/2, remove_handler/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, code_change/3, handle_info/2, terminate/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).

-ignore_xref([start_link/0, set_up/3, tear_down/2, span/4, add_handler/2, remove_handler/1]).

Expand Down Expand Up @@ -180,7 +180,8 @@ init([]) ->
{ok, #{events => #{}, probe_timers => #{}}}.

-spec handle_call(any(), gen_server:from(), state()) ->
{reply, ok | {ok, handlers()} | {error, map()}, state()}.
{reply, ok | {ok, handlers()} | {error, map()}, state()} |
{reply, ok | {ok, handlers()} | {error, map()}, state(), hibernate}.
handle_call({set_up, EventName, Labels, Config}, _From,
#{events := Events, probe_timers := ProbeTimers} = State) ->
case set_up_and_register_event(EventName, Labels, Config, Events) of
Expand Down Expand Up @@ -226,7 +227,7 @@ handle_call({remove_handler, Key}, _From, State = #{events := Events}) ->
end;
handle_call(persist, _From, State = #{events := Events}) ->
persistent_term:put(?MODULE, Events),
{reply, ok, State};
{reply, ok, State, hibernate};
handle_call({lookup, EventName, Labels}, _From, State = #{events := Events}) ->
{reply, lookup(EventName, Labels, Events), State};
handle_call(Request, From, State) ->
Expand All @@ -250,10 +251,6 @@ terminate(_Reason, _State) ->
|| {Key, Opts = #{}} <- maps:to_list(mongoose_config:get_opt(instrumentation))],
ok.

-spec code_change(any(), state(), any()) -> {ok, state()}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%% Internal functions

-spec update_if_persisted(event_map(), event_map()) -> ok.
Expand Down
21 changes: 11 additions & 10 deletions src/jlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
make_error_reply/3,
make_invitation/3,
make_config_change_message/1,
replace_from_to_attrs/3,
replace_from_to/3,
remove_attr/2,
iq_query_info/1,
Expand Down Expand Up @@ -160,13 +159,11 @@ make_invitation(From, Password, Reason) ->
-spec replace_from_to_attrs(From :: binary(),
To :: binary() | undefined,
exml:attrs()) -> exml:attrs().
replace_from_to_attrs(From, undefined, Attrs) ->
Attrs1 = maps:remove(<<"to">>, Attrs),
Attrs1#{<<"from">> => From};
replace_from_to_attrs(From, To, Attrs) ->
Attrs1 = maps:without([<<"from">>, <<"to">>], Attrs),
Attrs2 = case To of
undefined -> Attrs1;
_ -> Attrs1#{<<"to">> => To}
end,
Attrs2#{<<"from">> => From}.
Attrs#{<<"from">> => From, <<"to">> => To}.

-spec swap_from_to_attrs(exml:attrs()) -> exml:attrs().
swap_from_to_attrs(#{<<"from">> := From, <<"to">> := To} = Attrs) ->
Expand All @@ -180,12 +177,16 @@ swap_from_to_attrs(#{<<"to">> := To} = Attrs0) ->
swap_from_to_attrs(Attrs) ->
Attrs.

%% Replaces from and to, or ensures they are defined to begin with.
-spec replace_from_to(From :: jid:simple_jid() | jid:jid(),
To :: jid:simple_jid() | jid:jid(),
To :: undefined | jid:simple_jid() | jid:jid(),
XE :: exml:element()) -> exml:element().
replace_from_to(From, To, XE = #xmlel{attrs = Attrs}) ->
replace_from_to(From, undefined, #xmlel{attrs = Attrs} = Packet) ->
NewAttrs = replace_from_to_attrs(jid:to_binary(From), undefined, Attrs),
Packet#xmlel{attrs = NewAttrs};
replace_from_to(From, To, #xmlel{attrs = Attrs} = Packet) ->
NewAttrs = replace_from_to_attrs(jid:to_binary(From), jid:to_binary(To), Attrs),
XE#xmlel{attrs = NewAttrs}.
Packet#xmlel{attrs = NewAttrs}.

-spec remove_attr(binary(), exml:element()) -> exml:element().
remove_attr(Attr, XE = #xmlel{attrs = Attrs}) ->
Expand Down
11 changes: 4 additions & 7 deletions src/listeners/mongoose_listener_sup.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%% @doc Supervisor for the socket listeners

-module(mongoose_listener_sup).

-behaviour(supervisor).
Expand All @@ -8,13 +7,11 @@

-ignore_xref([start_link/0, init/1]).

-include("mongoose_logger.hrl").

%% API

-spec start_link() -> {ok, pid()}.
-spec start_link() -> supervisor:startlink_ret().
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
supervisor:start_link({local, ?MODULE}, ?MODULE, noargs).

-spec start_child(supervisor:child_spec()) -> ok.
start_child(ChildSpec) ->
Expand All @@ -24,8 +21,8 @@ start_child(ChildSpec) ->

%% Supervisor callbacks

-spec init([]) -> {ok, {supervisor:sup_flags(), []}}.
init([]) ->
-spec init(noargs) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(noargs) ->
{ok, {#{strategy => one_for_one,
intensity => 10,
period => 1}, []}}.
6 changes: 3 additions & 3 deletions src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,9 @@ maybe_delete_x_user_element(false, _ReceiverJID, Packet) ->
%% When sending out the archives to a requesting client,
%% the forwarded stanza MUST NOT have a 'to' attribute, and
%% the 'from' MUST be the occupant JID of the sender of the archived message.
replace_from_to_attributes(SrcJID, Packet = #xmlel{attrs = Attrs}) ->
NewAttrs = jlib:replace_from_to_attrs(jid:to_binary(SrcJID), undefined, Attrs),
Packet#xmlel{attrs = NewAttrs}.
-spec replace_from_to_attributes(jid:jid(), exml:element()) -> exml:element().
replace_from_to_attributes(SrcJID, Packet) ->
jlib:replace_from_to(SrcJID, undefined, Packet).

-spec message_row_to_ext_id(row()) -> binary().
message_row_to_ext_id(#{id := MessID}) ->
Expand Down
Loading