Merge pull request #179 from hamano/added_get_random_pid_error_handling

added get_random_pid/1 error handling
This commit is contained in:
Evgeny Khramtsov 2014-04-29 13:05:58 +04:00
commit 29aead19d9
2 changed files with 10 additions and 5 deletions

View File

@ -140,9 +140,12 @@ sql_bloc(Host, F) -> sql_call(Host, {sql_bloc, F}).
sql_call(Host, Msg) ->
case get(?STATE_KEY) of
undefined ->
(?GEN_FSM):sync_send_event(ejabberd_odbc_sup:get_random_pid(Host),
{sql_cmd, Msg, now()},
?TRANSACTION_TIMEOUT);
case ejabberd_odbc_sup:get_random_pid(Host) of
none -> ?WARNING_MSG("SQL calling unknown host: ~p~n", [Host]);
Pid ->
(?GEN_FSM):sync_send_event(Pid,{sql_cmd, Msg, now()},
?TRANSACTION_TIMEOUT)
end;
_State -> nested_op(Msg)
end.

View File

@ -82,8 +82,10 @@ get_pids(Host) ->
[R#sql_pool.pid || R <- Rs].
get_random_pid(Host) ->
Pids = get_pids(Host),
lists:nth(erlang:phash(now(), length(Pids)), Pids).
case get_pids(Host) of
[] -> none;
Pids -> lists:nth(erlang:phash(now(), length(Pids)), Pids)
end.
add_pid(Host, Pid) ->
F = fun () ->