25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-30 16:36:29 +01:00

Use ets insead of asking supervisor in ejabberd_odbc_sup:get_pids/1 (Thanks to Alexey Shchepin)

This commit is contained in:
Evgeniy Khramtsov 2010-07-03 00:35:34 +10:00
parent 5161a9d4e8
commit d60ef4c212
2 changed files with 34 additions and 9 deletions

View File

@ -179,6 +179,7 @@ init([Host, StartInterval]) ->
end,
[DBType | _] = db_opts(Host),
?GEN_FSM:send_event(self(), connect),
ejabberd_odbc_sup:add_pid(Host, self()),
{ok, connecting, #state{db_type = DBType,
host = Host,
max_pending_requests_len = max_fsm_queue(),
@ -275,6 +276,7 @@ handle_info(Info, StateName, State) ->
{next_state, StateName, State}.
terminate(_Reason, _StateName, State) ->
ejabberd_odbc_sup:remove_pid(State#state.host, self()),
case State#state.db_type of
mysql ->
%% old versions of mysql driver don't have the stop function

View File

@ -30,6 +30,8 @@
%% API
-export([start_link/1,
init/1,
add_pid/2,
remove_pid/2,
get_pids/1,
get_random_pid/1
]).
@ -44,7 +46,19 @@
-define(CONNECT_TIMEOUT, 500). % milliseconds
-record(sql_pool, {host, pid}).
start_link(Host) ->
mnesia:create_table(sql_pool,
[{ram_copies, [node()]},
{type, bag},
{local_content, true},
{attributes, record_info(fields, sql_pool)}]),
mnesia:add_table_copy(local_config, node(), ram_copies),
F = fun() ->
mnesia:delete({sql_pool, Host})
end,
mnesia:ets(F),
supervisor:start_link({local, gen_mod:get_module_proc(Host, ?MODULE)},
?MODULE, [Host]).
@ -86,16 +100,25 @@ init([Host]) ->
end, lists:seq(1, PoolSize))}}.
get_pids(Host) ->
Proc = gen_mod:get_module_proc(Host, ?MODULE),
% throw an exception if supervisor is not ready (i.e. if it cannot
% start its children, if the database is down for example)
sys:get_status(Proc, ?CONNECT_TIMEOUT),
[Child ||
{_Id, Child, _Type, _Modules} <- supervisor:which_children(Proc),
Child /= undefined].
Rs = mnesia:dirty_read(sql_pool, Host),
[R#sql_pool.pid || R <- Rs].
get_random_pid(Host) ->
Pids = get_pids(Host),
lists:nth(erlang:phash(now(), length(Pids)), Pids).
add_pid(Host, Pid) ->
F = fun() ->
mnesia:write(
#sql_pool{host = Host,
pid = Pid})
end,
mnesia:ets(F).
remove_pid(Host, Pid) ->
F = fun() ->
mnesia:delete_object(
#sql_pool{host = Host,
pid = Pid})
end,
mnesia:ets(F).