2004-12-30 00:10:14 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_odbc_sup.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-12-30 00:10:14 +01:00
|
|
|
%%% Purpose : ODBC connections supervisor
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 22 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:50:55 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2008-10-06 16:18:46 +02:00
|
|
|
%%%
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2004-12-30 00:10:14 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_odbc_sup).
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-30 00:10:14 +01:00
|
|
|
|
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,
|
2010-07-02 12:31:42 +02:00
|
|
|
add_pid/2,
|
|
|
|
remove_pid/2,
|
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).
|
2008-10-06 16:18:46 +02:00
|
|
|
-define(DEFAULT_ODBC_START_INTERVAL, 30). % 30 seconds
|
2007-08-16 18:37:00 +02:00
|
|
|
|
2009-08-06 17:06:08 +02:00
|
|
|
% time to wait for the supervisor to start its child before returning
|
|
|
|
% a timeout error to the request
|
|
|
|
-define(CONNECT_TIMEOUT, 500). % milliseconds
|
|
|
|
|
|
|
|
|
2010-07-02 12:31:42 +02:00
|
|
|
-record(sql_pool, {host, pid}).
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
start_link(Host) ->
|
2010-07-02 12:31:42 +02:00
|
|
|
mnesia:create_table(sql_pool,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, record_info(fields, sql_pool)}]),
|
2010-08-10 11:42:22 +02:00
|
|
|
mnesia:add_table_copy(sql_pool, node(), ram_copies),
|
2010-07-02 12:31:42 +02:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({sql_pool, Host})
|
|
|
|
end,
|
|
|
|
mnesia:ets(F),
|
2005-07-13 05:24:13 +02:00
|
|
|
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]) ->
|
2008-10-06 16:18:46 +02:00
|
|
|
PoolSize = case ejabberd_config:get_local_option({odbc_pool_size, Host}) of
|
|
|
|
I when is_integer(I) ->
|
|
|
|
I;
|
2007-08-16 18:37:00 +02:00
|
|
|
undefined ->
|
2008-10-06 16:18:46 +02:00
|
|
|
?DEFAULT_POOL_SIZE;
|
|
|
|
Other ->
|
2009-08-06 17:06:08 +02:00
|
|
|
?ERROR_MSG("Wrong odbc_pool_size definition '~p' "
|
|
|
|
"for host ~p, default to ~p~n",
|
2008-10-06 16:18:46 +02:00
|
|
|
[Other, Host, ?DEFAULT_POOL_SIZE]),
|
|
|
|
?DEFAULT_POOL_SIZE
|
|
|
|
end,
|
2009-08-06 17:06:08 +02:00
|
|
|
StartInterval = case ejabberd_config:get_local_option({odbc_start_interval,
|
|
|
|
Host}) of
|
2008-10-06 16:18:46 +02:00
|
|
|
Interval when is_integer(Interval) ->
|
|
|
|
Interval;
|
|
|
|
undefined ->
|
|
|
|
?DEFAULT_ODBC_START_INTERVAL;
|
|
|
|
_Other2 ->
|
2009-08-06 17:06:08 +02:00
|
|
|
?ERROR_MSG("Wrong odbc_start_interval "
|
|
|
|
"definition '~p' for host ~p, "
|
|
|
|
"defaulting to ~p~n",
|
|
|
|
[_Other2, Host,
|
|
|
|
?DEFAULT_ODBC_START_INTERVAL]),
|
2008-10-06 16:18:46 +02:00
|
|
|
?DEFAULT_ODBC_START_INTERVAL
|
|
|
|
end,
|
2010-12-01 06:24:51 +01:00
|
|
|
{ok, {{one_for_one, PoolSize*10, 1},
|
2004-12-30 00:10:14 +01:00
|
|
|
lists:map(
|
|
|
|
fun(I) ->
|
|
|
|
{I,
|
2008-10-06 16:18:46 +02:00
|
|
|
{ejabberd_odbc, start_link, [Host, StartInterval*1000]},
|
2004-12-30 00:10:14 +01:00
|
|
|
transient,
|
2010-12-01 06:24:51 +01:00
|
|
|
2000,
|
2004-12-30 00:10:14 +01:00
|
|
|
worker,
|
|
|
|
[?MODULE]}
|
2008-10-06 16:18:46 +02:00
|
|
|
end, lists:seq(1, PoolSize))}}.
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_pids(Host) ->
|
2010-07-02 12:31:42 +02:00
|
|
|
Rs = mnesia:dirty_read(sql_pool, Host),
|
|
|
|
[R#sql_pool.pid || R <- Rs].
|
2004-12-30 00:10:14 +01:00
|
|
|
|
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).
|
2010-07-02 12:31:42 +02:00
|
|
|
|
|
|
|
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).
|