25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Speed up ejabberd_s2s:is_service/2, allow_host/2 (thanks to Andreas Köhler)(EJAB-1319)

Iterating through the list of possible parent domains of a given domain
and comparing with the list of hosts or routes is almost always faster
than doing it the other way around. It naturally returns the shortest or
longest parent domain satisfying a predicate, whereas the possibly long
list compared with would need to be sorted by length first.
This commit is contained in:
Badlop 2010-10-14 21:14:46 +02:00
parent 0960637aa4
commit d1f5fb4aa9

View File

@ -491,22 +491,22 @@ needed_connections_number(Ls, MaxS2SConnectionsNumber,
is_service(From, To) -> is_service(From, To) ->
LFromDomain = exmpp_jid:prep_domain_as_list(From), LFromDomain = exmpp_jid:prep_domain_as_list(From),
case ejabberd_config:get_local_option({route_subdomains, LFromDomain}) of case ejabberd_config:get_local_option({route_subdomains, LFromDomain}) of
s2s -> % bypass RFC 3920 10.3 s2s -> % bypass RFC 3920 10.3
false; false;
_ -> _ ->
LDstDomain = exmpp_jid:prep_domain_as_list(To), LDstDomain = exmpp_jid:prep_domain_as_list(To),
P = fun(Domain) -> is_subdomain(LDstDomain, Domain) end, Hosts = ?MYHOSTS,
lists:any(P, ?MYHOSTS) P = fun(ParentDomain) -> lists:member(ParentDomain, Hosts) end,
lists:any(P, parent_domains(LDstDomain))
end. end.
%%-------------------------------------------------------------------- parent_domains(Domain) ->
%% Function: is_subdomain(Domain1, Domain2) -> true | false lists:foldl(
%% Description: Return true if Domain1 (a string representing an fun(Label, []) ->
%% internet domain name) is a subdomain (or the same domain) of [Label];
%% Domain2 (Label, [Head | Tail]) ->
%% -------------------------------------------------------------------- [Label ++ "." ++ Head, Head | Tail]
is_subdomain(Domain1, Domain2) -> end, [], lists:reverse(string:tokens(Domain, "."))).
lists:suffix(string:tokens(Domain2, "."), string:tokens(Domain1, ".")).
send_element(Pid, El) -> send_element(Pid, El) ->
Pid ! {send_element, El}. Pid ! {send_element, El}.
@ -575,10 +575,11 @@ update_tables() ->
%% Check if host is in blacklist or white list %% Check if host is in blacklist or white list
allow_host(MyServer, S2SHost) -> allow_host(MyServer, S2SHost) ->
case lists:filter( Hosts = ?MYHOSTS,
fun(Host) -> case lists:dropwhile(
is_subdomain(MyServer, Host) fun(ParentDomain) ->
end, ?MYHOSTS) of not lists:member(ParentDomain, Hosts)
end, parent_domains(MyServer)) of
[MyHost|_] -> [MyHost|_] ->
allow_host1(MyHost, S2SHost); allow_host1(MyHost, S2SHost);
[] -> [] ->