Skip to content

Commit

Permalink
Merge pull request #27 from inaka/elbrujohalcon.27.properly_change_th…
Browse files Browse the repository at this point in the history
…e_random_imp

Properly change the random implementation for R18
  • Loading branch information
Brujo Benavides committed Apr 28, 2016
2 parents 234f2e0 + b0343c2 commit 407d109
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ include erlang.mk
ERLC_OPTS := +warn_unused_vars +warn_export_all +warn_shadow_vars +warn_unused_import +warn_unused_function
ERLC_OPTS += +warn_bif_clash +warn_unused_record +warn_deprecated_function +warn_obsolete_guard +strict_validation
ERLC_OPTS += +warn_export_vars +warn_exported_vars +warn_missing_spec +warn_untyped_record +debug_info
ERLC_OPTS += -Dr_18

# Commont Test Config
TEST_ERLC_OPTS += +debug_info
Expand Down
3 changes: 1 addition & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
warn_export_vars,
warn_exported_vars,
warn_missing_spec,
warn_untyped_record, debug_info,
{platform_define, "^18", r_18}]}.
warn_untyped_record, debug_info]}.
{xref_warnings, true}.
{xref_checks, [undefined_function_calls, undefined_functions, locals_not_used, deprecated_function_calls, deprecated_functions]}.
{cover_enabled, true}.
Expand Down
29 changes: 22 additions & 7 deletions src/wpool_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ random_worker(Sup) ->
case wpool_size(Sup) of
undefined -> throw(no_workers);
Wpool_Size ->
_ = seed_random(),
worker_name(Sup, random:uniform(Wpool_Size))
WorkerNumber = rnd(Wpool_Size),
worker_name(Sup, WorkerNumber)
end.

%% @doc Picks the next worker in a round robin fashion
Expand Down Expand Up @@ -460,8 +460,23 @@ build_wpool(Name) ->
next_wpool(Wpool) ->
Wpool#wpool{next = (Wpool#wpool.next rem Wpool#wpool.size) + 1}.

-ifdef(r_18).
seed_random() -> random:seed(erlang:timestamp()).
-else.
seed_random() -> random:seed(now()).
-endif.
rnd(Wpool_Size) ->
case application:get_env(worker_pool, random_fun) of
undefined ->
set_random_fun(),
rnd(Wpool_Size);
{ok, RndFun} ->
RndFun(Wpool_Size)
end.

set_random_fun() ->
RndFun =
case code:ensure_loaded(rand) of
{module, rand} -> fun rand:uniform/1;
{error, _} ->
fun(Size) ->
_ = random:seed(os:timestamp()),
random:uniform(Size)
end
end,
application:set_env(worker_pool, random_fun, RndFun).

0 comments on commit 407d109

Please sign in to comment.