diff options
author | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2010-07-02 20:31:42 +1000 |
---|---|---|
committer | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2010-07-02 20:31:42 +1000 |
commit | 4c2e7e38a1170bacad0ecbe0244d048d11fd355c (patch) | |
tree | 569e9bf83d25b8f6c8ec48151a463892f25f8213 /src/odbc | |
parent | Recompile the Guide and Configure (diff) |
Use ets insead of asking supervisor in ejabberd_odbc_sup:get_pids/1 (Thanks to Alexey Shchepin)
Diffstat (limited to 'src/odbc')
-rw-r--r-- | src/odbc/ejabberd_odbc.erl | 2 | ||||
-rw-r--r-- | src/odbc/ejabberd_odbc_sup.erl | 41 |
2 files changed, 34 insertions, 9 deletions
diff --git a/src/odbc/ejabberd_odbc.erl b/src/odbc/ejabberd_odbc.erl index d7cdd0371..1b07fd6d9 100644 --- a/src/odbc/ejabberd_odbc.erl +++ b/src/odbc/ejabberd_odbc.erl @@ -178,6 +178,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(), @@ -274,6 +275,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 diff --git a/src/odbc/ejabberd_odbc_sup.erl b/src/odbc/ejabberd_odbc_sup.erl index d828449ec..45ede1835 100644 --- a/src/odbc/ejabberd_odbc_sup.erl +++ b/src/odbc/ejabberd_odbc_sup.erl @@ -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). |