mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-22 17:28:25 +01:00
* src/ejabberd_s2s.erl: Max number of connections and max number
of connections per node now can be specified via max_s2s_connections and max_s2s_connections_per_node acl rules SVN Revision: 950
This commit is contained in:
parent
4b2bd22533
commit
f82b84158e
@ -1,10 +1,16 @@
|
||||
2007-09-25 Alexey Shchepin <alexey@process-one.net>
|
||||
|
||||
* src/ejabberd_s2s.erl: Max number of connections and max number
|
||||
of connections per node now can be specified via
|
||||
max_s2s_connections and max_s2s_connections_per_node acl rules
|
||||
|
||||
2007-09-14 Mickael Remond <mremond@process-one.net>
|
||||
|
||||
* src/ejabberd_s2s_out.erl: Changed to actual p1_fsm behaviour. It
|
||||
was working correctly as the API of p1_fsm and gen_fsm is strictly
|
||||
the same.
|
||||
|
||||
* src/ejabberd_s2_in.erl: Added debug hook for s2s loop (EJAB-358).
|
||||
* src/ejabberd_s2s_in.erl: Added debug hook for s2s loop (EJAB-358).
|
||||
|
||||
* src/ejabberd_c2s.erl: Added debug hook for c2s loop (EJAB-358).
|
||||
|
||||
|
@ -3,12 +3,12 @@
|
||||
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
||||
%%% Purpose : S2S connections manager
|
||||
%%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
||||
%%% Id : $Id: ejabberd_s2s.erl 820 2007-07-19 21:17:13Z mremond $
|
||||
%%% Id : $Id$
|
||||
%%%----------------------------------------------------------------------
|
||||
|
||||
-module(ejabberd_s2s).
|
||||
-author('alexey@sevcom.net').
|
||||
-vsn('$Revision: 820 $ ').
|
||||
-vsn('$Revision$ ').
|
||||
|
||||
-behaviour(gen_server).
|
||||
|
||||
@ -32,7 +32,8 @@
|
||||
-include("jlib.hrl").
|
||||
-include("ejabberd_ctl.hrl").
|
||||
|
||||
-define(DEFAULT_MAX_S2S_CONNEXIONS_NUMBER, 3).
|
||||
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER, 1).
|
||||
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE, 1).
|
||||
|
||||
-record(s2s, {fromto, pid, key}).
|
||||
-record(state, {}).
|
||||
@ -57,7 +58,9 @@ route(From, To, Packet) ->
|
||||
end.
|
||||
|
||||
remove_connection(FromTo, Pid, Key) ->
|
||||
case catch mnesia:dirty_match_object(s2s, {s2s, FromTo, Pid, '_'}) of
|
||||
case catch mnesia:dirty_match_object(s2s, #s2s{fromto = FromTo,
|
||||
pid = Pid,
|
||||
_ = '_'}) of
|
||||
[#s2s{pid = Pid, key = Key}] ->
|
||||
F = fun() ->
|
||||
mnesia:delete_object(#s2s{fromto = FromTo,
|
||||
@ -90,15 +93,21 @@ has_key(FromTo, Key) ->
|
||||
|
||||
try_register(FromTo) ->
|
||||
Key = randoms:get_string(),
|
||||
Max_S2S_Connections_Number = max_s2s_connexions_number(element(1, FromTo)),
|
||||
MaxS2SConnectionsNumber = max_s2s_connections_number(FromTo),
|
||||
MaxS2SConnectionsNumberPerNode =
|
||||
max_s2s_connections_number_per_node(FromTo),
|
||||
F = fun() ->
|
||||
case mnesia:read({s2s, FromTo}) of
|
||||
L when length(L) < Max_S2S_Connections_Number ->
|
||||
L = mnesia:read({s2s, FromTo}),
|
||||
NeededConnections = needed_connections_number(
|
||||
L, MaxS2SConnectionsNumber,
|
||||
MaxS2SConnectionsNumberPerNode),
|
||||
if
|
||||
NeededConnections > 0 ->
|
||||
mnesia:write(#s2s{fromto = FromTo,
|
||||
pid = self(),
|
||||
key = Key}),
|
||||
{key, Key};
|
||||
_ ->
|
||||
true ->
|
||||
false
|
||||
end
|
||||
end,
|
||||
@ -128,7 +137,6 @@ init([]) ->
|
||||
mnesia:create_table(s2s, [{ram_copies, [node()]}, {type, bag},
|
||||
{attributes, record_info(fields, s2s)}]),
|
||||
mnesia:add_table_copy(s2s, node(), ram_copies),
|
||||
mnesia:add_table_index(s2s, key),
|
||||
mnesia:subscribe(system),
|
||||
ejabberd_ctl:register_commands(
|
||||
[{"incoming-s2s-number", "print number of incoming s2s connections on the node"},
|
||||
@ -240,8 +248,10 @@ find_connection(From, To) ->
|
||||
#jid{lserver = MyServer} = From,
|
||||
#jid{lserver = Server} = To,
|
||||
FromTo = {MyServer, Server},
|
||||
Max_S2S_Connections_Number = max_s2s_connexions_number(MyServer),
|
||||
?INFO_MSG("Finding connection for ~p~n", [FromTo]),
|
||||
MaxS2SConnectionsNumber = max_s2s_connections_number(FromTo),
|
||||
MaxS2SConnectionsNumberPerNode =
|
||||
max_s2s_connections_number_per_node(FromTo),
|
||||
?DEBUG("Finding connection for ~p~n", [FromTo]),
|
||||
case catch mnesia:dirty_read(s2s, FromTo) of
|
||||
{'EXIT', Reason} ->
|
||||
{aborted, Reason};
|
||||
@ -251,55 +261,78 @@ find_connection(From, To) ->
|
||||
%% is in whitelist:
|
||||
case not is_service(From, To) andalso allow_host(MyServer, Server) of
|
||||
true ->
|
||||
open_several_connections(Max_S2S_Connections_Number, MyServer,
|
||||
Server, From, FromTo, Max_S2S_Connections_Number);
|
||||
NeededConnections = needed_connections_number(
|
||||
[], MaxS2SConnectionsNumber,
|
||||
MaxS2SConnectionsNumberPerNode),
|
||||
open_several_connections(
|
||||
NeededConnections, MyServer,
|
||||
Server, From, FromTo,
|
||||
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode);
|
||||
false ->
|
||||
{aborted, error}
|
||||
end;
|
||||
L when is_list(L) , length(L) < Max_S2S_Connections_Number ->
|
||||
%% We establish the missing connections for this pair.
|
||||
open_several_connections(Max_S2S_Connections_Number-length(L), MyServer,
|
||||
Server, From, FromTo, Max_S2S_Connections_Number);
|
||||
L when is_list(L) ->
|
||||
NeededConnections = needed_connections_number(
|
||||
L, MaxS2SConnectionsNumber,
|
||||
MaxS2SConnectionsNumberPerNode),
|
||||
if
|
||||
NeededConnections > 0 ->
|
||||
%% We establish the missing connections for this pair.
|
||||
open_several_connections(
|
||||
NeededConnections, MyServer,
|
||||
Server, From, FromTo,
|
||||
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode);
|
||||
true ->
|
||||
%% We choose a connexion from the pool of opened ones.
|
||||
{atomic, choose_connection(From, L)}
|
||||
end
|
||||
end.
|
||||
|
||||
choose_connection(From, Connections) ->
|
||||
%El = lists:nth(random:uniform(length(Connections)), Connections),
|
||||
% use sticky connections based on the full JID of the sender
|
||||
Pid = case lists:nth(erlang:phash(From, length(Connections)), Connections) of
|
||||
El when is_record(El, s2s) ->
|
||||
El#s2s.pid;
|
||||
P when is_pid(P) ->
|
||||
P
|
||||
choose_pid(From, [C#s2s.pid || C <- Connections]).
|
||||
|
||||
choose_pid(From, Pids) ->
|
||||
Pids1 = case [P || P <- Pids, node(P) == node()] of
|
||||
[] -> Pids;
|
||||
Ps -> Ps
|
||||
end,
|
||||
?INFO_MSG("Using ejabberd_s2s_out ~p~n", [Pid]),
|
||||
%% use sticky connections based on the full JID of the sender
|
||||
Pid = lists:nth(erlang:phash(From, length(Pids1)), Pids1),
|
||||
?DEBUG("Using ejabberd_s2s_out ~p~n", [Pid]),
|
||||
Pid.
|
||||
|
||||
open_several_connections(N, MyServer, Server, From, FromTo, Max_S2S_Connections_Number) ->
|
||||
Connections_Result = [new_connection(MyServer, Server, From, FromTo, Max_S2S_Connections_Number)
|
||||
open_several_connections(N, MyServer, Server, From, FromTo,
|
||||
MaxS2SConnectionsNumber,
|
||||
MaxS2SConnectionsNumberPerNode) ->
|
||||
ConnectionsResult =
|
||||
[new_connection(MyServer, Server, From, FromTo,
|
||||
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode)
|
||||
|| _N <- lists:seq(1, N)],
|
||||
case [PID || {atomic, PID} <- Connections_Result] of
|
||||
case [PID || {atomic, PID} <- ConnectionsResult] of
|
||||
[] ->
|
||||
hd(Connections_Result);
|
||||
hd(ConnectionsResult);
|
||||
PIDs ->
|
||||
{atomic, choose_connection(From, PIDs)}
|
||||
{atomic, choose_pid(From, PIDs)}
|
||||
end.
|
||||
|
||||
new_connection(MyServer, Server, From, FromTo, Max_S2S_Connections_Number) ->
|
||||
new_connection(MyServer, Server, From, FromTo,
|
||||
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode) ->
|
||||
Key = randoms:get_string(),
|
||||
{ok, Pid} = ejabberd_s2s_out:start(
|
||||
MyServer, Server, {new, Key}),
|
||||
F = fun() ->
|
||||
case mnesia:read({s2s, FromTo}) of
|
||||
L when length(L) < Max_S2S_Connections_Number ->
|
||||
L = mnesia:read({s2s, FromTo}),
|
||||
NeededConnections = needed_connections_number(
|
||||
L, MaxS2SConnectionsNumber,
|
||||
MaxS2SConnectionsNumberPerNode),
|
||||
if
|
||||
NeededConnections > 0 ->
|
||||
mnesia:write(#s2s{fromto = FromTo,
|
||||
pid = Pid,
|
||||
key = Key}),
|
||||
?INFO_MSG("New s2s connection started ~p~n", [Pid]),
|
||||
?INFO_MSG("New s2s connection started ~p", [Pid]),
|
||||
Pid;
|
||||
L ->
|
||||
true ->
|
||||
choose_connection(From, L)
|
||||
end
|
||||
end,
|
||||
@ -312,14 +345,26 @@ new_connection(MyServer, Server, From, FromTo, Max_S2S_Connections_Number) ->
|
||||
end,
|
||||
TRes.
|
||||
|
||||
max_s2s_connexions_number(Host) ->
|
||||
case ejabberd_config:get_local_option({max_s2s_connexions_number, Host}) of
|
||||
N when is_integer(N) ->
|
||||
N;
|
||||
_ ->
|
||||
?DEFAULT_MAX_S2S_CONNEXIONS_NUMBER
|
||||
max_s2s_connections_number({From, To}) ->
|
||||
case acl:match_rule(
|
||||
From, max_s2s_connections, jlib:make_jid("", To, "")) of
|
||||
Max when is_integer(Max) -> Max;
|
||||
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER
|
||||
end.
|
||||
|
||||
max_s2s_connections_number_per_node({From, To}) ->
|
||||
case acl:match_rule(
|
||||
From, max_s2s_connections_per_node, jlib:make_jid("", To, "")) of
|
||||
Max when is_integer(Max) -> Max;
|
||||
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE
|
||||
end.
|
||||
|
||||
needed_connections_number(Ls, MaxS2SConnectionsNumber,
|
||||
MaxS2SConnectionsNumberPerNode) ->
|
||||
LocalLs = [L || L <- Ls, node(L#s2s.pid) == node()],
|
||||
lists:min([MaxS2SConnectionsNumber - length(Ls),
|
||||
MaxS2SConnectionsNumberPerNode - length(LocalLs)]).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Function: is_service(From, To) -> true | false
|
||||
%% Description: Return true if the destination must be considered as a
|
||||
@ -397,3 +442,4 @@ allow_host(MyServer, S2SHost) ->
|
||||
_ -> true %% The default s2s policy is allow
|
||||
end
|
||||
end.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user