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

* src/odbc/ejabberd_odbc_sup.erl: Add an odbc_pool_size config file option to choose the number of SQL

connection in each pool (EJAB-58).
* src/odbc/ejabberd_odbc.erl: Add an odbc_keepalive_interval config file option to perform a keep
alive query at given interval (EJAB-206).

SVN Revision: 879
This commit is contained in:
Jérôme Sautret 2007-08-16 16:37:00 +00:00
parent 731457a8bb
commit 6fcef6f5c4
2 changed files with 32 additions and 8 deletions

View File

@ -18,7 +18,8 @@
sql_query_t/1, sql_query_t/1,
sql_transaction/2, sql_transaction/2,
escape/1, escape/1,
escape_like/1]). escape_like/1,
keep_alive/1]).
%% gen_server callbacks %% gen_server callbacks
-export([init/1, -export([init/1,
@ -37,6 +38,8 @@
-define(PGSQL_PORT, 5432). -define(PGSQL_PORT, 5432).
-define(MYSQL_PORT, 3306). -define(MYSQL_PORT, 3306).
-define(KEEPALIVE_QUERY, "SELECT 1;").
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
%%% API %%% API
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
@ -112,6 +115,14 @@ escape_like(C) -> odbc_queries:escape(C).
%% {stop, Reason} %% {stop, Reason}
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
init([Host]) -> init([Host]) ->
case ejabberd_config:get_local_option({odbc_keepalive_interval, Host}) of
Interval when is_integer(Interval) ->
timer:apply_interval(Interval*1000, ?MODULE, keep_alive, [self()]);
undefined ->
ok;
_Other ->
?ERROR_MSG("Wrong odbc_keepalive_interval definition '~p' for host ~p.~n", [_Other, Host])
end,
SQLServer = ejabberd_config:get_local_option({odbc_server, Host}), SQLServer = ejabberd_config:get_local_option({odbc_server, Host}),
case SQLServer of case SQLServer of
%% Default pgsql port %% Default pgsql port
@ -128,7 +139,6 @@ init([Host]) ->
odbc_connect(SQLServer) odbc_connect(SQLServer)
end. end.
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
%% Func: handle_call/3 %% Func: handle_call/3
%% Returns: {reply, Reply, State} | %% Returns: {reply, Reply, State} |
@ -306,3 +316,7 @@ mysql_item_to_odbc(Columns, Recs) ->
{selected, {selected,
[element(2, Column) || Column <- Columns], [element(2, Column) || Column <- Columns],
[list_to_tuple(Rec) || Rec <- Recs]}. [list_to_tuple(Rec) || Rec <- Recs]}.
% perform a harmless query on all opened connexions to avoid connexion close.
keep_alive(PID) ->
gen_server:call(PID, {sql_query, ?KEEPALIVE_QUERY}, 60000).

View File

@ -10,6 +10,7 @@
-author('alexey@sevcom.net'). -author('alexey@sevcom.net').
-vsn('$Revision$ '). -vsn('$Revision$ ').
%% API
-export([start_link/1, -export([start_link/1,
init/1, init/1,
get_pids/1, get_pids/1,
@ -18,13 +19,23 @@
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-define(DEFAULT_POOL_SIZE, 10).
start_link(Host) -> start_link(Host) ->
supervisor:start_link({local, gen_mod:get_module_proc(Host, ?MODULE)}, supervisor:start_link({local, gen_mod:get_module_proc(Host, ?MODULE)},
?MODULE, [Host]). ?MODULE, [Host]).
init([Host]) -> init([Host]) ->
% TODO N = case ejabberd_config:get_local_option({odbc_pool_size, Host}) of
N = 10, 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,
{ok, {{one_for_one, 10, 6}, {ok, {{one_for_one, 10, 6},
lists:map( lists:map(
fun(I) -> fun(I) ->
@ -45,4 +56,3 @@ get_pids(Host) ->
get_random_pid(Host) -> get_random_pid(Host) ->
Pids = get_pids(Host), Pids = get_pids(Host),
lists:nth(erlang:phash(now(), length(Pids)), Pids). lists:nth(erlang:phash(now(), length(Pids)), Pids).