2017-07-21 01:07:36 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_push_keepalive.erl
|
|
|
|
%%% Author : Holger Weiss <holger@zedat.fu-berlin.de>
|
|
|
|
%%% Purpose : Keep pending XEP-0198 sessions alive with XEP-0357
|
|
|
|
%%% Created : 15 Jul 2017 by Holger Weiss <holger@zedat.fu-berlin.de>
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2017-2023 ProcessOne
|
2017-07-21 01:07:36 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_push_keepalive).
|
|
|
|
-author('holger@zedat.fu-berlin.de').
|
|
|
|
|
2018-01-31 14:57:43 +01:00
|
|
|
-behaviour(gen_mod).
|
2017-07-21 01:07:36 +02:00
|
|
|
|
|
|
|
%% gen_mod callbacks.
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_opt_type/1, mod_options/1, depends/2]).
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([mod_doc/0]).
|
2017-07-21 01:07:36 +02:00
|
|
|
%% 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]).
|
|
|
|
|
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2020-01-08 10:24:51 +01:00
|
|
|
-include("translate.hrl").
|
2017-07-21 01:07:36 +02:00
|
|
|
|
2019-07-23 00:15:40 +02:00
|
|
|
-define(PUSH_BEFORE_TIMEOUT_PERIOD, 120000). % 2 minutes.
|
2017-07-21 01:07:36 +02:00
|
|
|
|
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_mod callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec start(binary(), gen_mod:opts()) -> ok.
|
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_push_keepalive_opt:wake_on_start(Opts) of
|
2017-07-21 01:07:36 +02:00
|
|
|
true ->
|
|
|
|
wake_all(Host);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case {mod_push_keepalive_opt:wake_on_start(NewOpts),
|
|
|
|
mod_push_keepalive_opt:wake_on_start(OldOpts)} of
|
|
|
|
{true, false} ->
|
2017-07-21 01:07:36 +02:00
|
|
|
wake_all(Host);
|
|
|
|
_ ->
|
|
|
|
ok
|
2019-06-14 11:33:26 +02:00
|
|
|
end.
|
2017-07-21 01:07:36 +02:00
|
|
|
|
|
|
|
-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
|
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_push, hard},
|
|
|
|
{mod_client_state, soft},
|
|
|
|
{mod_stream_mgmt, soft}].
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec mod_opt_type(atom()) -> econf:validator().
|
2017-07-21 01:07:36 +02:00
|
|
|
mod_opt_type(resume_timeout) ->
|
2019-07-23 00:15:40 +02:00
|
|
|
econf:either(
|
|
|
|
econf:int(0, 0),
|
|
|
|
econf:timeout(second));
|
2017-07-21 01:07:36 +02:00
|
|
|
mod_opt_type(wake_on_start) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2017-07-21 01:07:36 +02:00
|
|
|
mod_opt_type(wake_on_timeout) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(_Host) ->
|
2021-06-25 01:30:10 +02:00
|
|
|
[{resume_timeout, timer:hours(72)},
|
2018-01-23 08:54:52 +01:00
|
|
|
{wake_on_start, false},
|
|
|
|
{wake_on_timeout, true}].
|
2017-07-21 01:07:36 +02:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module tries to keep the stream management "
|
2021-08-21 18:31:21 +02:00
|
|
|
"session (see _`mod_stream_mgmt`_) of a disconnected "
|
2020-01-08 10:24:51 +01:00
|
|
|
"mobile client alive if the client enabled push "
|
|
|
|
"notifications for that session. However, the normal "
|
|
|
|
"session resumption timeout is restored once a push "
|
|
|
|
"notification is issued, so the session will be closed "
|
|
|
|
"if the client doesn't respond to push notifications."), "",
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("The module depends on _`mod_push`_.")],
|
2020-01-08 10:24:51 +01:00
|
|
|
opts =>
|
|
|
|
[{resume_timeout,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
|
|
|
?T("This option specifies the period of time until "
|
|
|
|
"the session of a disconnected push client times out. "
|
|
|
|
"This timeout is only in effect as long as no push "
|
|
|
|
"notification is issued. Once that happened, the "
|
2021-08-21 18:31:21 +02:00
|
|
|
"resumption timeout configured for _`mod_stream_mgmt`_ "
|
|
|
|
"is restored. "
|
2021-06-25 01:30:10 +02:00
|
|
|
"The default value is '72' hours.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{wake_on_start,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("If this option is set to 'true', notifications "
|
|
|
|
"are generated for **all** registered push clients "
|
|
|
|
"during server startup. This option should not be "
|
|
|
|
"enabled on servers with many push clients as it "
|
|
|
|
"can generate significant load on the involved push "
|
|
|
|
"services and the server itself. "
|
|
|
|
"The default value is 'false'.")}},
|
|
|
|
{wake_on_timeout,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("If this option is set to 'true', a notification "
|
|
|
|
"is generated shortly before the session would time "
|
|
|
|
"out as per the 'resume_timeout' option. "
|
|
|
|
"The default value is 'true'.")}}]}.
|
|
|
|
|
2017-07-21 01:07:36 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Register/unregister hooks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec register_hooks(binary()) -> ok.
|
|
|
|
register_hooks(Host) ->
|
|
|
|
ejabberd_hooks:add(c2s_session_pending, Host, ?MODULE,
|
|
|
|
c2s_session_pending, 50),
|
|
|
|
ejabberd_hooks:add(c2s_session_resumed, Host, ?MODULE,
|
|
|
|
c2s_session_resumed, 50),
|
|
|
|
ejabberd_hooks:add(c2s_copy_session, Host, ?MODULE,
|
|
|
|
c2s_copy_session, 50),
|
|
|
|
ejabberd_hooks:add(c2s_handle_cast, Host, ?MODULE,
|
|
|
|
c2s_handle_cast, 40),
|
|
|
|
ejabberd_hooks:add(c2s_handle_info, Host, ?MODULE,
|
|
|
|
c2s_handle_info, 50),
|
|
|
|
ejabberd_hooks:add(c2s_handle_send, Host, ?MODULE,
|
|
|
|
c2s_stanza, 50).
|
|
|
|
|
|
|
|
-spec unregister_hooks(binary()) -> ok.
|
|
|
|
unregister_hooks(Host) ->
|
|
|
|
ejabberd_hooks:delete(c2s_session_pending, Host, ?MODULE,
|
|
|
|
c2s_session_pending, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_session_resumed, Host, ?MODULE,
|
|
|
|
c2s_session_resumed, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_copy_session, Host, ?MODULE,
|
|
|
|
c2s_copy_session, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_handle_cast, Host, ?MODULE,
|
|
|
|
c2s_handle_cast, 40),
|
|
|
|
ejabberd_hooks:delete(c2s_handle_info, Host, ?MODULE,
|
|
|
|
c2s_handle_info, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_handle_send, Host, ?MODULE,
|
|
|
|
c2s_stanza, 50).
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Hook callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec c2s_stanza(c2s_state(), xmpp_element() | xmlel(), term()) -> c2s_state().
|
|
|
|
c2s_stanza(#{push_enabled := true, mgmt_state := pending} = State,
|
2018-04-17 00:27:07 +02:00
|
|
|
Pkt, _SendResult) ->
|
2018-06-13 21:43:40 +02:00
|
|
|
case mod_push:is_incoming_chat_msg(Pkt) of
|
2018-04-17 00:27:07 +02:00
|
|
|
true ->
|
|
|
|
maybe_restore_resume_timeout(State);
|
|
|
|
false ->
|
|
|
|
State
|
|
|
|
end;
|
2017-07-21 01:07:36 +02:00
|
|
|
c2s_stanza(State, _Pkt, _SendResult) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_session_pending(c2s_state()) -> c2s_state().
|
|
|
|
c2s_session_pending(#{push_enabled := true, mgmt_queue := Queue} = State) ->
|
2018-06-13 21:43:40 +02:00
|
|
|
case mod_stream_mgmt:queue_find(fun mod_push:is_incoming_chat_msg/1,
|
2018-04-17 00:27:07 +02:00
|
|
|
Queue) of
|
|
|
|
none ->
|
2017-07-21 01:07:36 +02:00
|
|
|
State1 = maybe_adjust_resume_timeout(State),
|
|
|
|
maybe_start_wakeup_timer(State1);
|
2018-04-17 00:27:07 +02:00
|
|
|
_Msg ->
|
2017-07-21 01:07:36 +02:00
|
|
|
State
|
|
|
|
end;
|
|
|
|
c2s_session_pending(State) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_session_resumed(c2s_state()) -> c2s_state().
|
|
|
|
c2s_session_resumed(#{push_enabled := true} = State) ->
|
|
|
|
maybe_restore_resume_timeout(State);
|
|
|
|
c2s_session_resumed(State) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_copy_session(c2s_state(), c2s_state()) -> c2s_state().
|
|
|
|
c2s_copy_session(State, #{push_enabled := true,
|
|
|
|
push_resume_timeout := ResumeTimeout,
|
2018-03-05 01:10:12 +01:00
|
|
|
push_wake_on_timeout := WakeOnTimeout} = OldState) ->
|
|
|
|
State1 = case maps:find(push_resume_timeout_orig, OldState) of
|
|
|
|
{ok, Val} ->
|
|
|
|
State#{push_resume_timeout_orig => Val};
|
|
|
|
error ->
|
|
|
|
State
|
|
|
|
end,
|
|
|
|
State1#{push_resume_timeout => ResumeTimeout,
|
|
|
|
push_wake_on_timeout => WakeOnTimeout};
|
2017-07-21 01:07:36 +02:00
|
|
|
c2s_copy_session(State, _) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_handle_cast(c2s_state(), any()) -> c2s_state().
|
2022-05-01 17:51:07 +02:00
|
|
|
c2s_handle_cast(#{lserver := LServer} = State, {push_enable, _ID}) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ResumeTimeout = mod_push_keepalive_opt:resume_timeout(LServer),
|
|
|
|
WakeOnTimeout = mod_push_keepalive_opt:wake_on_timeout(LServer),
|
2017-07-21 01:07:36 +02:00
|
|
|
State#{push_resume_timeout => ResumeTimeout,
|
|
|
|
push_wake_on_timeout => WakeOnTimeout};
|
|
|
|
c2s_handle_cast(State, push_disable) ->
|
|
|
|
State1 = maps:remove(push_resume_timeout, State),
|
|
|
|
maps:remove(push_wake_on_timeout, State1);
|
|
|
|
c2s_handle_cast(State, _Msg) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_handle_info(c2s_state(), any()) -> c2s_state() | {stop, c2s_state()}.
|
|
|
|
c2s_handle_info(#{push_enabled := true, mgmt_state := pending,
|
|
|
|
jid := JID} = State, {timeout, _, push_keepalive}) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Waking ~ts before session times out", [jid:encode(JID)]),
|
2018-06-13 21:43:40 +02:00
|
|
|
mod_push:notify(State, none, undefined),
|
2017-07-21 01:07:36 +02:00
|
|
|
{stop, State};
|
|
|
|
c2s_handle_info(State, _) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec maybe_adjust_resume_timeout(c2s_state()) -> c2s_state().
|
|
|
|
maybe_adjust_resume_timeout(#{push_resume_timeout := undefined} = State) ->
|
|
|
|
State;
|
|
|
|
maybe_adjust_resume_timeout(#{push_resume_timeout := Timeout} = State) ->
|
|
|
|
OrigTimeout = mod_stream_mgmt:get_resume_timeout(State),
|
2019-07-30 11:50:31 +02:00
|
|
|
?DEBUG("Adjusting resume timeout to ~B seconds", [Timeout div 1000]),
|
2017-07-21 01:07:36 +02:00
|
|
|
State1 = mod_stream_mgmt:set_resume_timeout(State, Timeout),
|
|
|
|
State1#{push_resume_timeout_orig => OrigTimeout}.
|
|
|
|
|
|
|
|
-spec maybe_restore_resume_timeout(c2s_state()) -> c2s_state().
|
|
|
|
maybe_restore_resume_timeout(#{push_resume_timeout_orig := Timeout} = State) ->
|
2019-07-30 11:50:31 +02:00
|
|
|
?DEBUG("Restoring resume timeout to ~B seconds", [Timeout div 1000]),
|
2017-07-21 01:07:36 +02:00
|
|
|
State1 = mod_stream_mgmt:set_resume_timeout(State, Timeout),
|
|
|
|
maps:remove(push_resume_timeout_orig, State1);
|
|
|
|
maybe_restore_resume_timeout(State) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec maybe_start_wakeup_timer(c2s_state()) -> c2s_state().
|
|
|
|
maybe_start_wakeup_timer(#{push_wake_on_timeout := true,
|
|
|
|
push_resume_timeout := ResumeTimeout} = State)
|
2019-07-23 00:15:40 +02:00
|
|
|
when is_integer(ResumeTimeout), ResumeTimeout > ?PUSH_BEFORE_TIMEOUT_PERIOD ->
|
|
|
|
WakeTimeout = ResumeTimeout - ?PUSH_BEFORE_TIMEOUT_PERIOD,
|
2019-07-30 11:50:31 +02:00
|
|
|
?DEBUG("Scheduling wake-up timer to fire in ~B seconds", [WakeTimeout div 1000]),
|
2019-07-23 00:15:40 +02:00
|
|
|
erlang:start_timer(WakeTimeout, self(), push_keepalive),
|
2017-07-21 01:07:36 +02:00
|
|
|
State;
|
|
|
|
maybe_start_wakeup_timer(State) ->
|
|
|
|
State.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec wake_all(binary()) -> ok.
|
2017-07-21 01:07:36 +02:00
|
|
|
wake_all(LServer) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Waking all push clients on ~ts", [LServer]),
|
2017-07-21 01:07:36 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, mod_push),
|
|
|
|
case Mod:lookup_sessions(LServer) of
|
|
|
|
{ok, Sessions} ->
|
|
|
|
IgnoreResponse = fun(_) -> ok end,
|
|
|
|
lists:foreach(fun({_, PushLJID, Node, XData}) ->
|
|
|
|
mod_push:notify(LServer, PushLJID, Node,
|
2018-06-13 21:43:40 +02:00
|
|
|
XData, none, undefined,
|
|
|
|
IgnoreResponse)
|
2017-07-21 01:07:36 +02:00
|
|
|
end, Sessions);
|
|
|
|
error ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ok
|
2017-07-21 01:07:36 +02:00
|
|
|
end.
|