Skip to content

Commit

Permalink
mod_push_keepalive: Delay 'wake_on_start'
Browse files Browse the repository at this point in the history
Delay the 'wake_on_start' notifications until ejabberd is fully
initialized.  This makes sure no s2s connections are initiated before
certificates are loaded.

Many thanks to Friedrich Altheide for reporting the issue.
  • Loading branch information
weiss committed Aug 8, 2023
1 parent a9347cd commit 26ed653
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/mod_push_keepalive.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
-export([start/2, stop/1, reload/3, mod_opt_type/1, mod_options/1, depends/2]).
-export([mod_doc/0]).
%% ejabberd_hooks callbacks.
-export([c2s_session_pending/1, c2s_session_resumed/1, c2s_copy_session/2,
c2s_handle_cast/2, c2s_handle_info/2, c2s_stanza/3]).
-export([ejabberd_started/0, c2s_session_pending/1, c2s_session_resumed/1,
c2s_copy_session/2, c2s_handle_cast/2, c2s_handle_info/2,
c2s_stanza/3]).

-include("logger.hrl").
-include_lib("xmpp/include/xmpp.hrl").
Expand All @@ -47,28 +48,16 @@
%% gen_mod callbacks.
%%--------------------------------------------------------------------
-spec start(binary(), gen_mod:opts()) -> ok.
start(Host, Opts) ->
case mod_push_keepalive_opt:wake_on_start(Opts) of
true ->
wake_all(Host);
false ->
ok
end,
start(Host, _Opts) ->
register_hooks(Host).

-spec stop(binary()) -> ok.
stop(Host) ->
unregister_hooks(Host).

-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
reload(Host, NewOpts, OldOpts) ->
case {mod_push_keepalive_opt:wake_on_start(NewOpts),
mod_push_keepalive_opt:wake_on_start(OldOpts)} of
{true, false} ->
wake_all(Host);
_ ->
ok
end.
reload(_Host, _NewOpts, _OldOpts) ->
ok.

-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
depends(_Host, _Opts) ->
Expand Down Expand Up @@ -146,7 +135,10 @@ register_hooks(Host) ->
ejabberd_hooks:add(c2s_handle_info, Host, ?MODULE,
c2s_handle_info, 50),
ejabberd_hooks:add(c2s_handle_send, Host, ?MODULE,
c2s_stanza, 50).
c2s_stanza, 50),
% Wait for ejabberd_pkix before running our ejabberd_started/0, so that we
% don't initiate s2s connections before certificates are loaded:
ejabberd_hooks:add(ejabberd_started, ?MODULE, ejabberd_started, 90).

-spec unregister_hooks(binary()) -> ok.
unregister_hooks(Host) ->
Expand All @@ -161,7 +153,14 @@ unregister_hooks(Host) ->
ejabberd_hooks:delete(c2s_handle_info, Host, ?MODULE,
c2s_handle_info, 50),
ejabberd_hooks:delete(c2s_handle_send, Host, ?MODULE,
c2s_stanza, 50).
c2s_stanza, 50),
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
false ->
ejabberd_hooks:delete(
ejabberd_started, ?MODULE, ejabberd_started, 90);
true ->
ok
end.

%%--------------------------------------------------------------------
%% Hook callbacks.
Expand Down Expand Up @@ -233,6 +232,12 @@ c2s_handle_info(#{push_enabled := true, mgmt_state := pending,
c2s_handle_info(State, _) ->
State.

-spec ejabberd_started() -> ok.
ejabberd_started() ->
[wake_all(Host) || Host <- ejabberd_config:get_option(hosts),
mod_push_keepalive_opt:wake_on_start(Host)],
ok.

%%--------------------------------------------------------------------
%% Internal functions.
%%--------------------------------------------------------------------
Expand Down

0 comments on commit 26ed653

Please sign in to comment.