24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-14 22:00:16 +02:00

open up to 3 s2s outgoing connection per domain pair

SVN Revision: 928
This commit is contained in:
Mickaël Rémond 2007-09-14 14:15:44 +00:00
parent e809c6c322
commit 9fc203ee6d
3 changed files with 93 additions and 72 deletions

View File

@ -3,12 +3,12 @@
%%% Author : Alexey Shchepin <alexey@sevcom.net> %%% Author : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : S2S connections manager %%% Purpose : S2S connections manager
%%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@sevcom.net> %%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id : $Id$ %%% Id : $Id: ejabberd_s2s.erl 820 2007-07-19 21:17:13Z mremond $
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
-module(ejabberd_s2s). -module(ejabberd_s2s).
-author('alexey@sevcom.net'). -author('alexey@sevcom.net').
-vsn('$Revision$ '). -vsn('$Revision: 820 $ ').
-behaviour(gen_server). -behaviour(gen_server).
@ -16,9 +16,8 @@
-export([start_link/0, -export([start_link/0,
route/3, route/3,
have_connection/1, have_connection/1,
get_key/1, has_key/2,
try_register/1, try_register/1,
remove_connection/1,
remove_connection/3, remove_connection/3,
dirty_get_connections/0, dirty_get_connections/0,
allow_host/2, allow_host/2,
@ -55,14 +54,9 @@ route(From, To, Packet) ->
ok ok
end. end.
remove_connection(FromTo) ->
F = fun() ->
mnesia:delete({s2s, FromTo})
end,
mnesia:transaction(F).
remove_connection(FromTo, Pid, Key) -> remove_connection(FromTo, Pid, Key) ->
case catch mnesia:dirty_read(s2s, FromTo) of ?ERROR_MSG("XXXXXXXXXXX ~p~n", [{FromTo, Pid, Key}]),
case catch mnesia:dirty_match_object(s2s, {s2s, FromTo, Pid, '_'}) of
[#s2s{pid = Pid, key = Key}] -> [#s2s{pid = Pid, key = Key}] ->
F = fun() -> F = fun() ->
mnesia:delete_object(#s2s{fromto = FromTo, mnesia:delete_object(#s2s{fromto = FromTo,
@ -82,23 +76,27 @@ have_connection(FromTo) ->
false false
end. end.
get_key(FromTo) -> has_key(FromTo, Key) ->
case catch mnesia:dirty_read(s2s, FromTo) of case mnesia:dirty_select(s2s,
[E] -> [{#s2s{fromto = FromTo, key = Key, _ = '_'},
E#s2s.key; [],
['$_']}]) of
[] ->
false;
_ -> _ ->
error true
end. end.
try_register(FromTo) -> try_register(FromTo) ->
Key = randoms:get_string(), Key = randoms:get_string(),
Max_S2S_Connexions_Number = 3,
F = fun() -> F = fun() ->
case mnesia:read({s2s, FromTo}) of case mnesia:read({s2s, FromTo}) of
[] -> L when length(L) < Max_S2S_Connexions_Number ->
mnesia:write(#s2s{fromto = FromTo, mnesia:write(#s2s{fromto = FromTo,
pid = self(), pid = self(),
key = Key}), key = Key}),
{key, Key}; {key, Key};
_ -> _ ->
false false
end end
@ -126,9 +124,10 @@ dirty_get_connections() ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
init([]) -> init([]) ->
update_tables(), update_tables(),
mnesia:create_table(s2s, [{ram_copies, [node()]}, mnesia:create_table(s2s, [{ram_copies, [node()]}, {type, bag},
{attributes, record_info(fields, s2s)}]), {attributes, record_info(fields, s2s)}]),
mnesia:add_table_copy(s2s, node(), ram_copies), mnesia:add_table_copy(s2s, node(), ram_copies),
mnesia:add_table_index(s2s, key),
mnesia:subscribe(system), mnesia:subscribe(system),
ejabberd_ctl:register_commands( ejabberd_ctl:register_commands(
[{"incoming-s2s-number", "print number of incoming s2s connections on the node"}, [{"incoming-s2s-number", "print number of incoming s2s connections on the node"},
@ -224,7 +223,7 @@ do_route(From, To, Packet) ->
Attrs), Attrs),
send_element(Pid, {xmlelement, Name, NewAttrs, Els}), send_element(Pid, {xmlelement, Name, NewAttrs, Els}),
ok; ok;
{aborted, Reason} -> {aborted, _Reason} ->
case xml:get_tag_attr_s("type", Packet) of case xml:get_tag_attr_s("type", Packet) of
"error" -> ok; "error" -> ok;
"result" -> ok; "result" -> ok;
@ -240,6 +239,8 @@ find_connection(From, To) ->
#jid{lserver = MyServer} = From, #jid{lserver = MyServer} = From,
#jid{lserver = Server} = To, #jid{lserver = Server} = To,
FromTo = {MyServer, Server}, FromTo = {MyServer, Server},
Max_S2S_Connexions_Number = 3,
?ERROR_MSG("XXX Finding connection for ~p~n", [FromTo]),
case catch mnesia:dirty_read(s2s, FromTo) of case catch mnesia:dirty_read(s2s, FromTo) of
{'EXIT', Reason} -> {'EXIT', Reason} ->
{aborted, Reason}; {aborted, Reason};
@ -250,36 +251,49 @@ find_connection(From, To) ->
case {is_service(From, To), case {is_service(From, To),
allow_host(MyServer, Server)} of allow_host(MyServer, Server)} of
{false, true} -> {false, true} ->
?DEBUG("starting new s2s connection~n", []), new_connection(MyServer, Server, From, FromTo, Max_S2S_Connexions_Number);
Key = randoms:get_string(),
{ok, Pid} = ejabberd_s2s_out:start(
MyServer, Server, {new, Key}),
F = fun() ->
case mnesia:read({s2s, FromTo}) of
[El] ->
El#s2s.pid;
[] ->
mnesia:write(#s2s{fromto = FromTo,
pid = Pid,
key = Key}),
Pid
end
end,
TRes = mnesia:transaction(F),
case TRes of
{atomic, Pid} ->
ejabberd_s2s_out:start_connection(Pid);
_ ->
ejabberd_s2s_out:stop_connection(Pid)
end,
TRes;
_ -> _ ->
{aborted, error} {aborted, error}
end; end;
[El] -> L when is_list(L) , length(L) < Max_S2S_Connexions_Number ->
{atomic, El#s2s.pid} %% We establish another connection for this pair.
new_connection(MyServer, Server, From, FromTo, Max_S2S_Connexions_Number);
L when is_list(L) ->
%% We choose a connexion from the pool of opened ones.
{atomic, choose_connection(From, L)}
end. end.
choose_connection(From, Connections) ->
El = lists:nth(erlang:phash(From, length(Connections)), Connections),
%El = lists:nth(random:uniform(length(Connections)), Connections),
?ERROR_MSG("XXX using ejabberd_s2s_out ~p~n", [El#s2s.pid]),
El#s2s.pid.
new_connection(MyServer, Server, From, FromTo, Max_S2S_Connexions_Number) ->
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_Connexions_Number ->
mnesia:write(#s2s{fromto = FromTo,
pid = Pid,
key = Key}),
?ERROR_MSG("XXX new s2s connection started ~p~n", [Pid]),
Pid;
L ->
choose_connection(From, L)
end
end,
TRes = mnesia:transaction(F),
case TRes of
{atomic, Pid} ->
ejabberd_s2s_out:start_connection(Pid);
_ ->
ejabberd_s2s_out:stop_connection(Pid)
end,
TRes.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: is_service(From, To) -> true | false %% Function: is_service(From, To) -> true | false
@ -321,6 +335,15 @@ ctl_process(Val, _Args) ->
Val. Val.
update_tables() -> update_tables() ->
case catch mnesia:table_info(s2s, type) of
bag ->
ok;
{'EXIT', _} ->
ok;
_ ->
% XXX TODO convert it ?
mnesia:delete_table(s2s)
end,
case catch mnesia:table_info(s2s, attributes) of case catch mnesia:table_info(s2s, attributes) of
[fromto, node, key] -> [fromto, node, key] ->
mnesia:transform_table(s2s, ignore, [fromto, pid, key]), mnesia:transform_table(s2s, ignore, [fromto, pid, key]),
@ -349,5 +372,3 @@ allow_host(MyServer, S2SHost) ->
_ -> true %% The default s2s policy is allow _ -> true %% The default s2s policy is allow
end end
end. end.

View File

@ -3,7 +3,7 @@
%%% Author : Alexey Shchepin <alexey@sevcom.net> %%% Author : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : Serve incoming s2s connection %%% Purpose : Serve incoming s2s connection
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@sevcom.net> %%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id : $Id$ %%% Id : $Id: ejabberd_s2s_in.erl 820 2007-07-19 21:17:13Z mremond $
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
-module(ejabberd_s2s_in). -module(ejabberd_s2s_in).
@ -74,7 +74,7 @@
-define(HOST_UNKNOWN_ERR, -define(HOST_UNKNOWN_ERR,
xml:element_to_string(?SERR_HOST_UNKNOWN)). xml:element_to_string(?SERR_HOST_UNKNOWN)).
-define(INVALID_FROM_ERR, -define(INVALID_FROM_ERR,
xml:element_to_string(?SERR_INVALID_FROM)). xml:element_to_string(?SERR_INVALID_FROM)).
-define(INVALID_XML_ERR, -define(INVALID_XML_ERR,
@ -101,7 +101,7 @@ socket_type() ->
%% Returns: {ok, StateName, StateData} | %% Returns: {ok, StateName, StateData} |
%% {ok, StateName, StateData, Timeout} | %% {ok, StateName, StateData, Timeout} |
%% ignore | %% ignore |
%% {stop, StopReason} %% {stop, StopReason}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
init([{SockMod, Socket}, Opts]) -> init([{SockMod, Socket}, Opts]) ->
?INFO_MSG("started: ~p", [Socket]), ?INFO_MSG("started: ~p", [Socket]),
@ -136,7 +136,7 @@ init([{SockMod, Socket}, Opts]) ->
%% Func: StateName/2 %% Func: StateName/2
%% Returns: {next_state, NextStateName, NextStateData} | %% Returns: {next_state, NextStateName, NextStateData} |
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) -> wait_for_stream({xmlstreamstart, _Name, Attrs}, StateData) ->
@ -312,8 +312,8 @@ stream_established({xmlstreamelement, El}, StateData) ->
?INFO_MSG("GET KEY: ~p", [{To, From, Id, Key}]), ?INFO_MSG("GET KEY: ~p", [{To, From, Id, Key}]),
LTo = jlib:nameprep(To), LTo = jlib:nameprep(To),
LFrom = jlib:nameprep(From), LFrom = jlib:nameprep(From),
%% Checks if the from domain is allowed and if the to %% Checks if the from domain is allowed and if the to
%% domain is handled by this server: %% domain is handled by this server:
case {ejabberd_s2s:allow_host(To, From), case {ejabberd_s2s:allow_host(To, From),
lists:member(LTo, ejabberd_router:dirty_get_all_domains())} of lists:member(LTo, ejabberd_router:dirty_get_all_domains())} of
{true, true} -> {true, true} ->
@ -338,10 +338,13 @@ stream_established({xmlstreamelement, El}, StateData) ->
?INFO_MSG("VERIFY KEY: ~p", [{To, From, Id, Key}]), ?INFO_MSG("VERIFY KEY: ~p", [{To, From, Id, Key}]),
LTo = jlib:nameprep(To), LTo = jlib:nameprep(To),
LFrom = jlib:nameprep(From), LFrom = jlib:nameprep(From),
Key1 = ejabberd_s2s:get_key({LTo, LFrom}), Type = case ejabberd_s2s:has_key({LTo, LFrom}, Key) of
Type = if Key == Key1 -> "valid"; true -> "valid";
true -> "invalid" _ -> "invalid"
end, end,
%Type = if Key == Key1 -> "valid";
% true -> "invalid"
% end,
send_element(StateData, send_element(StateData,
{xmlelement, {xmlelement,
"db:verify", "db:verify",
@ -456,7 +459,7 @@ stream_established(closed, StateData) ->
%% {reply, Reply, NextStateName, NextStateData} | %% {reply, Reply, NextStateName, NextStateData} |
%% {reply, Reply, NextStateName, NextStateData, Timeout} | %% {reply, Reply, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} | %% {stop, Reason, NewStateData} |
%% {stop, Reason, Reply, NewStateData} %% {stop, Reason, Reply, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
%state_name(Event, From, StateData) -> %state_name(Event, From, StateData) ->
% Reply = ok, % Reply = ok,
@ -466,7 +469,7 @@ stream_established(closed, StateData) ->
%% Func: handle_event/3 %% Func: handle_event/3
%% Returns: {next_state, NextStateName, NextStateData} | %% Returns: {next_state, NextStateName, NextStateData} |
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_event(_Event, StateName, StateData) -> handle_event(_Event, StateName, StateData) ->
{next_state, StateName, StateData}. {next_state, StateName, StateData}.
@ -478,7 +481,7 @@ handle_event(_Event, StateName, StateData) ->
%% {reply, Reply, NextStateName, NextStateData} | %% {reply, Reply, NextStateName, NextStateData} |
%% {reply, Reply, NextStateName, NextStateData, Timeout} | %% {reply, Reply, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} | %% {stop, Reason, NewStateData} |
%% {stop, Reason, Reply, NewStateData} %% {stop, Reason, Reply, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_sync_event(_Event, _From, StateName, StateData) -> handle_sync_event(_Event, _From, StateName, StateData) ->
Reply = ok, Reply = ok,
@ -491,7 +494,7 @@ code_change(_OldVsn, StateName, StateData, _Extra) ->
%% Func: handle_info/3 %% Func: handle_info/3
%% Returns: {next_state, NextStateName, NextStateData} | %% Returns: {next_state, NextStateName, NextStateData} |
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_info({send_text, Text}, StateName, StateData) -> handle_info({send_text, Text}, StateName, StateData) ->
send_text(StateData, Text), send_text(StateData, Text),
@ -677,5 +680,3 @@ match_labels([DL | DLabels], [PL | PLabels]) ->
false -> false ->
false false
end. end.

View File

@ -107,7 +107,7 @@ stop_connection(Pid) ->
%% Returns: {ok, StateName, StateData} | %% Returns: {ok, StateName, StateData} |
%% {ok, StateName, StateData, Timeout} | %% {ok, StateName, StateData, Timeout} |
%% ignore | %% ignore |
%% {stop, StopReason} %% {stop, StopReason}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
init([From, Server, Type]) -> init([From, Server, Type]) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
@ -146,7 +146,7 @@ init([From, Server, Type]) ->
%% Func: StateName/2 %% Func: StateName/2
%% Returns: {next_state, NextStateName, NextStateData} | %% Returns: {next_state, NextStateName, NextStateData} |
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
open_socket(init, StateData) -> open_socket(init, StateData) ->
?INFO_MSG("open_socket: ~p", [{StateData#state.myname, ?INFO_MSG("open_socket: ~p", [{StateData#state.myname,
@ -583,7 +583,7 @@ stream_established(closed, StateData) ->
%% {reply, Reply, NextStateName, NextStateData} | %% {reply, Reply, NextStateName, NextStateData} |
%% {reply, Reply, NextStateName, NextStateData, Timeout} | %% {reply, Reply, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} | %% {stop, Reason, NewStateData} |
%% {stop, Reason, Reply, NewStateData} %% {stop, Reason, Reply, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
%state_name(Event, From, StateData) -> %state_name(Event, From, StateData) ->
% Reply = ok, % Reply = ok,
@ -593,7 +593,7 @@ stream_established(closed, StateData) ->
%% Func: handle_event/3 %% Func: handle_event/3
%% Returns: {next_state, NextStateName, NextStateData} | %% Returns: {next_state, NextStateName, NextStateData} |
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_event(Event, StateName, StateData) -> handle_event(Event, StateName, StateData) ->
{next_state, StateName, StateData}. {next_state, StateName, StateData}.
@ -605,7 +605,7 @@ handle_event(Event, StateName, StateData) ->
%% {reply, Reply, NextStateName, NextStateData} | %% {reply, Reply, NextStateName, NextStateData} |
%% {reply, Reply, NextStateName, NextStateData, Timeout} | %% {reply, Reply, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} | %% {stop, Reason, NewStateData} |
%% {stop, Reason, Reply, NewStateData} %% {stop, Reason, Reply, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_sync_event(Event, From, StateName, StateData) -> handle_sync_event(Event, From, StateName, StateData) ->
Reply = ok, Reply = ok,
@ -618,7 +618,7 @@ code_change(OldVsn, StateName, StateData, Extra) ->
%% Func: handle_info/3 %% Func: handle_info/3
%% Returns: {next_state, NextStateName, NextStateData} | %% Returns: {next_state, NextStateName, NextStateData} |
%% {next_state, NextStateName, NextStateData, Timeout} | %% {next_state, NextStateName, NextStateData, Timeout} |
%% {stop, Reason, NewStateData} %% {stop, Reason, NewStateData}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_info({send_text, Text}, StateName, StateData) -> handle_info({send_text, Text}, StateName, StateData) ->
send_text(StateData, Text), send_text(StateData, Text),
@ -848,4 +848,3 @@ test_get_addr_port(Server) ->
lists:keyreplace(HostPort, 1, Acc, {HostPort, Num + 1}) lists:keyreplace(HostPort, 1, Acc, {HostPort, Num + 1})
end end
end, [], lists:seq(1, 100000)). end, [], lists:seq(1, 100000)).