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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2014-03-13 12:29:21 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2014 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
|
|
|
%%%
|
2014-02-22 11:27:40 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2004-12-30 00:10:14 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_odbc_sup).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
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
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start_link/1, init/1, add_pid/2, remove_pid/2,
|
2013-08-12 14:25:05 +02:00
|
|
|
get_pids/1, get_random_pid/1, transform_options/1]).
|
2004-12-30 00:10:14 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-define(PGSQL_PORT, 5432).
|
|
|
|
|
|
|
|
-define(MYSQL_PORT, 3306).
|
|
|
|
|
2007-08-16 18:37:00 +02:00
|
|
|
-define(DEFAULT_POOL_SIZE, 10).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(DEFAULT_ODBC_START_INTERVAL, 30).
|
2009-08-06 17:06:08 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(CONNECT_TIMEOUT, 500).
|
2009-08-06 17:06:08 +02:00
|
|
|
|
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,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{ram_copies, [node()]}, {type, bag},
|
2010-07-02 12:31:42 +02:00
|
|
|
{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),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () -> mnesia:delete({sql_pool, Host}) end,
|
2010-07-02 12:31:42 +02:00
|
|
|
mnesia:ets(F),
|
2013-03-14 10:33:02 +01:00
|
|
|
supervisor:start_link({local,
|
|
|
|
gen_mod:get_module_proc(Host, ?MODULE)},
|
2005-07-13 05:24:13 +02:00
|
|
|
?MODULE, [Host]).
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
init([Host]) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
PoolSize = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{odbc_pool_size, Host},
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
?DEFAULT_POOL_SIZE),
|
2013-08-12 14:25:05 +02:00
|
|
|
StartInterval = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{odbc_start_interval, Host},
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
?DEFAULT_ODBC_START_INTERVAL),
|
|
|
|
{ok,
|
|
|
|
{{one_for_one, PoolSize * 10, 1},
|
|
|
|
lists:map(fun (I) ->
|
|
|
|
{I,
|
|
|
|
{ejabberd_odbc, start_link,
|
|
|
|
[Host, StartInterval * 1000]},
|
|
|
|
transient, 2000, worker, [?MODULE]}
|
|
|
|
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) ->
|
2014-04-24 08:34:41 +02:00
|
|
|
case get_pids(Host) of
|
|
|
|
[] -> none;
|
|
|
|
Pids -> lists:nth(erlang:phash(now(), length(Pids)), Pids)
|
|
|
|
end.
|
2010-07-02 12:31:42 +02:00
|
|
|
|
|
|
|
add_pid(Host, Pid) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
mnesia:write(#sql_pool{host = Host, pid = Pid})
|
2010-07-02 12:31:42 +02:00
|
|
|
end,
|
|
|
|
mnesia:ets(F).
|
|
|
|
|
|
|
|
remove_pid(Host, Pid) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
mnesia:delete_object(#sql_pool{host = Host, pid = Pid})
|
2010-07-02 12:31:42 +02:00
|
|
|
end,
|
|
|
|
mnesia:ets(F).
|
2013-08-12 14:25:05 +02:00
|
|
|
|
|
|
|
transform_options(Opts) ->
|
|
|
|
lists:foldl(fun transform_options/2, [], Opts).
|
|
|
|
|
|
|
|
transform_options({odbc_server, {Type, Server, Port, DB, User, Pass}}, Opts) ->
|
|
|
|
[{odbc_type, Type},
|
|
|
|
{odbc_server, Server},
|
|
|
|
{odbc_port, Port},
|
|
|
|
{odbc_database, DB},
|
|
|
|
{odbc_username, User},
|
|
|
|
{odbc_password, Pass}|Opts];
|
|
|
|
transform_options({odbc_server, {mysql, Server, DB, User, Pass}}, Opts) ->
|
|
|
|
transform_options({odbc_server, {mysql, Server, ?MYSQL_PORT, DB, User, Pass}}, Opts);
|
|
|
|
transform_options({odbc_server, {pgsql, Server, DB, User, Pass}}, Opts) ->
|
|
|
|
transform_options({odbc_server, {pgsql, Server, ?PGSQL_PORT, DB, User, Pass}}, Opts);
|
|
|
|
transform_options(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|