2004-12-30 00:10:14 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_odbc_sup.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : ODBC connections supervisor
|
|
|
|
%%% Created : 22 Dec 2004 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_odbc_sup).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2007-08-16 18:37:00 +02:00
|
|
|
%% API
|
2005-07-13 05:24:13 +02:00
|
|
|
-export([start_link/1,
|
2004-12-30 00:10:14 +01:00
|
|
|
init/1,
|
2005-07-13 05:24:13 +02:00
|
|
|
get_pids/1,
|
|
|
|
get_random_pid/1
|
2004-12-30 00:10:14 +01:00
|
|
|
]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2007-08-16 18:37:00 +02:00
|
|
|
-define(DEFAULT_POOL_SIZE, 10).
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
start_link(Host) ->
|
|
|
|
supervisor:start_link({local, gen_mod:get_module_proc(Host, ?MODULE)},
|
|
|
|
?MODULE, [Host]).
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
init([Host]) ->
|
2007-08-16 18:37:00 +02:00
|
|
|
N = case ejabberd_config:get_local_option({odbc_pool_size, Host}) of
|
|
|
|
I when is_integer(I) ->
|
|
|
|
I;
|
|
|
|
undefined ->
|
|
|
|
?DEFAULT_POOL_SIZE;
|
|
|
|
Other ->
|
|
|
|
?ERROR_MSG("Wrong odbc_pool_size definition '~p' for host ~p, default to ~p~n",
|
|
|
|
[Other, Host, ?DEFAULT_POOL_SIZE]),
|
|
|
|
?DEFAULT_POOL_SIZE
|
|
|
|
end,
|
2006-01-13 19:05:10 +01:00
|
|
|
{ok, {{one_for_one, 10, 6},
|
2004-12-30 00:10:14 +01:00
|
|
|
lists:map(
|
|
|
|
fun(I) ->
|
|
|
|
{I,
|
2005-07-13 05:24:13 +02:00
|
|
|
{ejabberd_odbc, start_link, [Host]},
|
2004-12-30 00:10:14 +01:00
|
|
|
transient,
|
|
|
|
brutal_kill,
|
|
|
|
worker,
|
|
|
|
[?MODULE]}
|
|
|
|
end, lists:seq(1, N))}}.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_pids(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
2004-12-30 00:10:14 +01:00
|
|
|
[Child ||
|
2005-07-13 05:24:13 +02:00
|
|
|
{_Id, Child, _Type, _Modules} <- supervisor:which_children(Proc),
|
2004-12-30 00:10:14 +01:00
|
|
|
Child /= undefined].
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_random_pid(Host) ->
|
|
|
|
Pids = get_pids(Host),
|
2004-12-30 00:10:14 +01:00
|
|
|
lists:nth(erlang:phash(now(), length(Pids)), Pids).
|