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

adding dynamic domains support to mod_ping #3136

Merged
merged 8 commits into from
May 31, 2021
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
{skip_cases, "tests", carboncopy_SUITE, [discovering_support],
"at the moment mod_disco doesn't support dynamic domains"}.

{suites, "tests", mod_ping_SUITE}.

{config, ["dynamic_domains.config", "test.config"]}.

{logdir, "ct_report"}.
Expand Down
1 change: 1 addition & 0 deletions big_tests/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
%% so that we rein the "bag of things" approach
{hosts, [{mim, [{node, mongooseim@localhost},
{domain, <<"localhost">>},
{host_type, <<"localhost">>},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary, see below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have it defined like that, not as rpc-s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's better to have one helper that consistently reads it from mongoose_domain_core instead of defining it multiple times for multiple hosts. Anyway, as the helper is already there, let's use it consistently. This way we can change the implementation later when needed.

{vars, "mim1"},
{cluster, mim},
{secondary_domain, <<"localhost.bis">>},
Expand Down
60 changes: 31 additions & 29 deletions big_tests/tests/mod_ping_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ suite() ->
escalus:suite().

ping_interval() ->
3.
timer:seconds(3).

ping_req_timeout() ->
2.
timer:seconds(2).

init_per_suite(Config) ->
mongoose_helper:inject_module(?MODULE),
Expand All @@ -83,7 +83,7 @@ init_per_group(server_ping_kill, Config) ->
[{timeout_action, kill} | Config].

end_per_group(_GroupName, Config) ->
Domain = ct:get_config({hosts, mim, domain}),
Domain = ct:get_config({hosts, mim, host_type}),
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
dynamic_modules:stop(Domain, mod_ping),
Config.

Expand All @@ -100,39 +100,40 @@ end_per_testcase(CaseName, Config) ->
escalus:end_per_testcase(CaseName, Config).

start_mod_ping(Opts) ->
Domain = ct:get_config({hosts, mim, domain}),
Domain = ct:get_config({hosts, mim, host_type}),
dynamic_modules:start(Domain, mod_ping, Opts).

setup_pong_hook(Config) ->
Pid = self(),
Host = escalus_users:get_host(Config, alice),
Handler = mongoose_helper:successful_rpc(?MODULE, setup_pong_hook, [Host, Pid]),
HostType = ct:get_config({hosts, mim, host_type}),
Handler = mongoose_helper:successful_rpc(?MODULE, setup_pong_hook, [HostType, Pid]),
[{pong_handler, Handler} | Config].

setup_pong_hook(Host, Pid) ->
Handler = fun(_Acc, JID, _Response, _TDelta) ->
setup_pong_hook(HostType, Pid) ->
Handler = fun(_Acc, _HostType, JID, _Response, _TDelta) ->
Pid ! {pong, jid:to_binary(jid:to_lower(JID))}
end,
ejabberd_hooks:add(user_ping_response, Host, Handler, 50),
ejabberd_hooks:add(user_ping_response, HostType, Handler, 50),
Handler.

clear_pong_hook(Config) ->
{value, {_, Handler}, NConfig} = lists:keytake(pong_handler, 1, Config),
Host = escalus_users:get_host(Config, alice),
mongoose_helper:successful_rpc(?MODULE, clear_pong_hook, [Host, Handler]),
HostType = ct:get_config({hosts, mim, host_type}),
mongoose_helper:successful_rpc(?MODULE, clear_pong_hook, [HostType, Handler]),
NConfig.

clear_pong_hook(Host, Handler) ->
ejabberd_hooks:delete(user_ping_response, Host, Handler, 50).
clear_pong_hook(HostType, Handler) ->
ejabberd_hooks:delete(user_ping_response, HostType, Handler, 50).

%%--------------------------------------------------------------------
%% Ping tests
%%--------------------------------------------------------------------
ping(ConfigIn) ->
Domain = ct:get_config({hosts, mim, domain}),
HostType = ct:get_config({hosts, mim, host_type}),
Metrics = [
{[Domain, mod_ping, ping_response],0},
{[Domain, mod_ping, ping_response_timeout],0}
{[HostType, mod_ping, ping_response],0},
{[HostType, mod_ping, ping_response_timeout],0}
],
Config = [{mongoose_metrics, Metrics} | ConfigIn],
escalus:fresh_story(Config, [{alice, 1}],
Expand Down Expand Up @@ -160,9 +161,10 @@ wrong_ping(Config) ->

active(ConfigIn) ->
Domain = ct:get_config({hosts, mim, domain}),
HostType = ct:get_config({hosts, mim, host_type}),
Metrics = [
{[Domain, mod_ping, ping_response],0},
{[Domain, mod_ping, ping_response_timeout],0}
{[HostType, mod_ping, ping_response],0},
{[HostType, mod_ping, ping_response_timeout],0}
],
Config = [{mongoose_metrics, Metrics} | ConfigIn],
escalus:fresh_story(Config, [{alice, 1}],
Expand All @@ -177,10 +179,10 @@ active(ConfigIn) ->
end).

active_keep_alive(ConfigIn) ->
Domain = ct:get_config({hosts, mim, domain}),
HostType = ct:get_config({hosts, mim, host_type}),
Metrics = [
{[Domain, mod_ping, ping_response],0},
{[Domain, mod_ping, ping_response_timeout],0}
{[HostType, mod_ping, ping_response],0},
{[HostType, mod_ping, ping_response_timeout],0}
],
Config = [{mongoose_metrics, Metrics} | ConfigIn],
escalus:fresh_story(Config, [{alice, 1}],
Expand All @@ -193,11 +195,11 @@ active_keep_alive(ConfigIn) ->
end).

server_ping_pong(ConfigIn) ->
Domain = ct:get_config({hosts, mim, domain}),
HostType = ct:get_config({hosts, mim, host_type}),
Metrics = [
{[Domain, mod_ping, ping_response], 5},
{[Domain, mod_ping, ping_response_timeout], 0},
{[Domain, mod_ping, ping_response_time], changed}
{[HostType, mod_ping, ping_response], 5},
{[HostType, mod_ping, ping_response_timeout], 0},
{[HostType, mod_ping, ping_response_time], changed}
],
Config = [{mongoose_metrics, Metrics} | ConfigIn],
%% We use 5 Alices because with just 1 sample the histogram may look like it hasn't changed
Expand All @@ -213,24 +215,24 @@ server_ping_pong(ConfigIn) ->
end).

server_ping_pang(ConfigIn) ->
Domain = ct:get_config({hosts, mim, domain}),
HostType = ct:get_config({hosts, mim, host_type}),
Metrics = [
{[Domain, mod_ping, ping_response], 0},
{[Domain, mod_ping, ping_response_timeout], 1}
{[HostType, mod_ping, ping_response], 0},
{[HostType, mod_ping, ping_response_timeout], 1}
],
Config = [{mongoose_metrics, Metrics} | ConfigIn],
escalus:fresh_story(Config, [{alice, 1}],
fun(Alice) ->
wait_for_ping_req(Alice),
%% do not resp to ping req
ct:sleep(timer:seconds(ping_req_timeout() + 0.5)),
ct:sleep(ping_req_timeout() + timer:seconds(1)/2),
TimeoutAction = ?config(timeout_action, Config),
check_connection(TimeoutAction, Alice),
escalus_client:kill_connection(Config, Alice)
end).

wait_ping_interval(Ration) ->
WaitTime = timer:seconds(ping_interval()) * Ration,
WaitTime = ping_interval() * Ration,
ct:sleep(WaitTime).

check_connection(kill, Client) ->
Expand Down
11 changes: 4 additions & 7 deletions src/ejabberd_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -893,11 +893,10 @@ process_outgoing_stanza(Acc, StateData) ->

process_outgoing_stanza(Acc, ToJID, <<"presence">>, StateData) ->
#jid{user = User, server = Server} = FromJID = mongoose_acc:from_jid(Acc),
%% TODO: recheck that Acc has sender's host type here.
HostType = mongoose_acc:host_type(Acc),
Res = mongoose_hooks:c2s_update_presence(HostType, Acc),
El = mongoose_acc:element(Res),
Res1 = mongoose_hooks:user_send_packet(HostType, Res, FromJID, ToJID, El),
Res1 = mongoose_hooks:user_send_packet(Res, FromJID, ToJID, El),
{_Acc1, NState} = case ToJID of
#jid{user = User,
server = Server,
Expand All @@ -910,25 +909,23 @@ process_outgoing_stanza(Acc, ToJID, <<"presence">>, StateData) ->
process_outgoing_stanza(Acc0, ToJID, <<"iq">>, StateData) ->
{XMLNS, Acc} = mongoose_iq:xmlns(Acc0),
FromJID = mongoose_acc:from_jid(Acc),
HostType = mongoose_acc:host_type(Acc),
El = mongoose_acc:element(Acc),
{_Acc, NState} = case XMLNS of
?NS_PRIVACY ->
process_privacy_iq(Acc, ToJID, StateData);
?NS_BLOCKING ->
process_privacy_iq(Acc, ToJID, StateData);
_ ->
Acc2 = mongoose_hooks:user_send_packet(HostType, Acc,
FromJID, ToJID, El),
Acc2 = mongoose_hooks:user_send_packet(Acc, FromJID,
ToJID, El),
Acc3 = check_privacy_and_route(Acc2, StateData),
{Acc3, StateData}
end,
NState;
process_outgoing_stanza(Acc, ToJID, <<"message">>, StateData) ->
FromJID = mongoose_acc:from_jid(Acc),
HostType = mongoose_acc:host_type(Acc),
El = mongoose_acc:element(Acc),
Acc1 = mongoose_hooks:user_send_packet(HostType, Acc, FromJID, ToJID, El),
Acc1 = mongoose_hooks:user_send_packet(Acc, FromJID, ToJID, El),
_Acc2 = check_privacy_and_route(Acc1, StateData),
StateData;
process_outgoing_stanza(_Acc, _ToJID, _Name, StateData) ->
Expand Down
6 changes: 2 additions & 4 deletions src/mod_commands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,11 @@ send_stanza(BinStanza) ->
do_send_packet(From, To, Packet) ->
case mongoose_domain_api:get_domain_host_type(From#jid.lserver) of
{ok, HostType} ->
Acc0 = mongoose_acc:new(#{location => ?LOCATION,
Acc = mongoose_acc:new(#{location => ?LOCATION,
host_type => HostType,
lserver => From#jid.lserver,
element => Packet}),
mongoose_hooks:user_send_packet(From#jid.lserver,
Acc0,
From, To, Packet),
mongoose_hooks:user_send_packet(Acc, From, To, Packet),
ejabberd_router:route(From, To, Packet),
ok;
{error, not_found} ->
Expand Down
Loading