mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-22 16:20:52 +01:00
Use round-robin algorithm when selecting worker from DB pool
This commit is contained in:
parent
18433e289f
commit
8770fc98e1
@ -516,7 +516,7 @@ log_error(Cmd, Reason) ->
|
|||||||
|
|
||||||
-spec get_rnd_id() -> pos_integer().
|
-spec get_rnd_id() -> pos_integer().
|
||||||
get_rnd_id() ->
|
get_rnd_id() ->
|
||||||
randoms:uniform(2, ejabberd_redis_sup:get_pool_size()).
|
randoms:round_robin(ejabberd_redis_sup:get_pool_size() - 1) + 2.
|
||||||
|
|
||||||
-spec get_result([{error, atom() | binary()} | {ok, iodata()}]) ->
|
-spec get_result([{error, atom() | binary()} | {ok, iodata()}]) ->
|
||||||
{ok, [redis_reply()]} | {error, binary()}.
|
{ok, [redis_reply()]} | {error, binary()}.
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
-author('alexey@process-one.net').
|
-author('alexey@process-one.net').
|
||||||
|
|
||||||
-export([start_link/0, init/1, get_pids/0,
|
-export([start_link/0, init/1, get_pids/0,
|
||||||
transform_options/1, get_random_pid/0, get_random_pid/1,
|
transform_options/1, get_random_pid/0,
|
||||||
host_up/1, config_reloaded/0, opt_type/1]).
|
host_up/1, config_reloaded/0, opt_type/1]).
|
||||||
|
|
||||||
-include("ejabberd.hrl").
|
-include("ejabberd.hrl").
|
||||||
@ -199,10 +199,7 @@ get_pids() ->
|
|||||||
[ejabberd_riak:get_proc(I) || I <- lists:seq(1, get_pool_size())].
|
[ejabberd_riak:get_proc(I) || I <- lists:seq(1, get_pool_size())].
|
||||||
|
|
||||||
get_random_pid() ->
|
get_random_pid() ->
|
||||||
get_random_pid(p1_time_compat:system_time()).
|
I = randoms:round_robin(get_pool_size()) + 1,
|
||||||
|
|
||||||
get_random_pid(Term) ->
|
|
||||||
I = erlang:phash2(Term, get_pool_size()) + 1,
|
|
||||||
ejabberd_riak:get_proc(I).
|
ejabberd_riak:get_proc(I).
|
||||||
|
|
||||||
transform_options(Opts) ->
|
transform_options(Opts) ->
|
||||||
|
@ -98,7 +98,9 @@ get_pids(Host) ->
|
|||||||
get_random_pid(Host) ->
|
get_random_pid(Host) ->
|
||||||
case get_pids(Host) of
|
case get_pids(Host) of
|
||||||
[] -> none;
|
[] -> none;
|
||||||
Pids -> lists:nth(erlang:phash(p1_time_compat:unique_integer(), length(Pids)), Pids)
|
Pids ->
|
||||||
|
I = randoms:round_robin(length(Pids)) + 1,
|
||||||
|
lists:nth(I, Pids)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
add_pid(Host, Pid) ->
|
add_pid(Host, Pid) ->
|
||||||
|
@ -27,7 +27,8 @@
|
|||||||
|
|
||||||
-author('alexey@process-one.net').
|
-author('alexey@process-one.net').
|
||||||
|
|
||||||
-export([get_string/0, uniform/0, uniform/1, uniform/2, bytes/1]).
|
-export([get_string/0, uniform/0, uniform/1, uniform/2, bytes/1,
|
||||||
|
round_robin/1]).
|
||||||
|
|
||||||
-define(THRESHOLD, 16#10000000000000000).
|
-define(THRESHOLD, 16#10000000000000000).
|
||||||
|
|
||||||
@ -51,3 +52,7 @@ bytes(N) ->
|
|||||||
bytes(N) ->
|
bytes(N) ->
|
||||||
crypto:rand_bytes(N).
|
crypto:rand_bytes(N).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
|
-spec round_robin(pos_integer()) -> non_neg_integer().
|
||||||
|
round_robin(N) ->
|
||||||
|
erlang:unique_integer([monotonic, positive]) rem N.
|
||||||
|
Loading…
Reference in New Issue
Block a user