Skip to content

Commit

Permalink
Optimise gen_servers
Browse files Browse the repository at this point in the history
- introduce hibernation when appropriate,
- remove dead `code_change/3` callbacks,
- remove dead comments,
- improve specs for start_link/ and init/1 callbacks
  • Loading branch information
NelsonVides committed Feb 5, 2025
1 parent 9320a70 commit 88952f6
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 148 deletions.
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 @@ route_error_reply(From, To, Acc, Error) ->
%% gen_server callbacks
%%====================================================================

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

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

handle_cast(_Msg, State) ->
{noreply, State}.
Expand All @@ -151,9 +150,6 @@ terminate(_Reason, _State) ->
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
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}, []}}.
3 changes: 1 addition & 2 deletions src/mod_caps.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@
start_link(HostType, Opts) ->
mod_caps_backend:init(HostType, Opts),
Proc = gen_mod:get_module_proc(HostType, ?PROCNAME),
gen_server:start_link({local, Proc}, ?MODULE,
[HostType, Opts], []).
gen_server:start_link({local, Proc}, ?MODULE, [HostType, Opts], [{hibernate_after, 0}]).

-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> any().
start(HostType, Opts) ->
Expand Down
14 changes: 5 additions & 9 deletions src/mongoose_cleaner.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
terminate/2]).

-ignore_xref([start_link/0]).

Expand All @@ -26,14 +25,15 @@
%%% API
%%%===================================================================

-spec start_link() -> gen_server:start_ret().
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
gen_server:start_link({local, ?SERVER}, ?MODULE, noargs, []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

init([]) ->
init(noargs) ->
case net_kernel:monitor_nodes(true) of
ok ->
{ok, #state{}};
Expand All @@ -45,8 +45,7 @@ init([]) ->
end.

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

handle_cast(_Msg, State) ->
{noreply, State}.
Expand All @@ -63,9 +62,6 @@ handle_info(_Info, State) ->
terminate(_Reason, _State) ->
ok.

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

%%%===================================================================
%%% Internal functions
%%%===================================================================
Expand Down
Loading

0 comments on commit 88952f6

Please sign in to comment.