Skip to content

Commit

Permalink
Merge pull request #3784 from esl/hooks/mod_global_distrib
Browse files Browse the repository at this point in the history
Refactored hook handler in mod_global_distrib module
  • Loading branch information
NelsonVides authored Oct 4, 2022
2 parents b17712a + d7a4438 commit 49f9b07
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/global_distrib/mod_global_distrib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

-export([deps/2, start/2, stop/1, config_spec/0]).
-export([find_metadata/2, get_metadata/3, remove_metadata/2, put_metadata/3]).
-export([maybe_reroute/1]).
-export([maybe_reroute/3]).
-export([process_opts/1, process_endpoint/1]).

-ignore_xref([maybe_reroute/1, remove_metadata/2]).
-ignore_xref([remove_metadata/2]).

%%--------------------------------------------------------------------
%% gen_mod API
Expand Down Expand Up @@ -59,19 +59,19 @@ bounce_modules(#{enabled := false}) -> [].
start(HostType, #{global_host := HostType}) ->
mongoose_metrics:ensure_metric(global, ?GLOBAL_DISTRIB_DELIVERED_WITH_TTL, histogram),
mongoose_metrics:ensure_metric(global, ?GLOBAL_DISTRIB_STOP_TTL_ZERO, spiral),
ejabberd_hooks:add(hooks());
gen_hook:add_handlers(hooks());
start(_HostType, #{}) ->
ok.

-spec stop(mongooseim:host_type()) -> any().
stop(HostType) ->
case gen_mod:get_module_opt(HostType, ?MODULE, global_host) of
HostType -> ejabberd_hooks:delete(hooks());
HostType -> gen_hook:delete_handlers(hooks());
_ -> ok
end.

hooks() ->
[{filter_packet, global, ?MODULE, maybe_reroute, 99}].
[{filter_packet, global, fun ?MODULE:maybe_reroute/3, #{}, 99}].

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
Expand Down Expand Up @@ -224,30 +224,32 @@ remove_metadata(Acc, Key) ->
%% Hooks implementation
%%--------------------------------------------------------------------

-spec maybe_reroute(drop) -> drop;
({jid:jid(), jid:jid(), mongoose_acc:t(), exml:element()}) ->
drop | {jid:jid(), jid:jid(), mongoose_acc:t(), exml:element()}.
maybe_reroute(drop) -> drop;
-spec maybe_reroute(drop, _, _) -> drop;
(FPacket, Params, Extra) -> {ok, drop} | {ok, FPacket} when
FPacket :: {jid:jid(), jid:jid(), mongoose_acc:t(), exml:element()},
Params :: map(),
Extra :: map().
maybe_reroute(drop, _, _) -> {ok, drop};
maybe_reroute({#jid{ luser = SameUser, lserver = SameServer } = _From,
#jid{ luser = SameUser, lserver = SameServer } = _To,
_Acc, _Packet} = FPacket) ->
_Acc, _Packet} = FPacket, _, _) ->
%% GD is not designed to support two user sessions existing in distinct clusters
%% and here we explicitly block routing stanzas between them.
%% Without this clause, test_pm_with_ungraceful_reconnection_to_different_server test
%% was randomly failing because sometimes 'unavailable' presence from a dead session
%% was poisoning reg1 cache. In such case, reg1 tried to route locally stanzas
%% from unacked SM buffer, leading to an error, while a brand new, shiny Eve
%% on mim1 was waiting.
FPacket;
maybe_reroute({From, To, _, Packet} = FPacket) ->
{ok, FPacket};
maybe_reroute({From, To, _, Packet} = FPacket, _, _) ->
Acc = maybe_initialize_metadata(FPacket),
{ok, ID} = find_metadata(Acc, id),
LocalHost = opt(local_host),
GlobalHost = opt(global_host),
%% If target_host_override is set (typically when routed out of bounce storage),
%% host lookup is skipped and messages are routed to target_host_override value.
TargetHostOverride = get_metadata(Acc, target_host_override, undefined),
case lookup_recipients_host(TargetHostOverride, To, LocalHost, GlobalHost) of
ResultFPacket = case lookup_recipients_host(TargetHostOverride, To, LocalHost, GlobalHost) of
{ok, LocalHost} ->
%% Continue routing with initialized metadata
mongoose_hooks:mod_global_distrib_known_recipient(GlobalHost,
Expand Down Expand Up @@ -288,7 +290,8 @@ maybe_reroute({From, To, _, Packet} = FPacket) ->
?LOG_DEBUG(#{what => gd_route_failed, gd_id => ID, acc => Acc,
text => <<"Unable to route global: user not found in the routing table">>}),
mongoose_hooks:mod_global_distrib_unknown_recipient(GlobalHost, {From, To, Acc, Packet})
end.
end,
{ok, ResultFPacket}.

%%--------------------------------------------------------------------
%% Helpers
Expand Down

0 comments on commit 49f9b07

Please sign in to comment.