Skip to content

Commit

Permalink
Optimization for random seeding
Browse files Browse the repository at this point in the history
Only seed when no good seed is present in the process calling.
  • Loading branch information
ferd committed Jul 19, 2012
1 parent 931ed2e commit ca76585
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/statsderl.erl
Expand Up @@ -90,7 +90,7 @@ code_change(_OldVsn, State, _Extra) ->
%% ------------------------------------------------------------------

send(Method, Key, Value, SampleRate) ->
random:seed(erlang:now()),
maybe_seed(),
case random:uniform() =< SampleRate of
true ->
Packet = generate_packet(Method, Key, Value, SampleRate),
Expand Down Expand Up @@ -121,3 +121,18 @@ generate_packet(Method, Key, Value, SampleRate) ->
gauge ->
[Key, <<":">>, BinValue, <<"|g">>, BinSampleRate]
end.

%% this check verifies whether a seed is already placed
%% in the process dictionary for the random module -- if
%% it is, we don't re-seed for any reason, except if the
%% seed is bad (say, {X,X,X} -- usually {0,0,0} and {1,1,1}
%% for the default seed
maybe_seed() ->
case get(random_seed) of
undefined ->
random:seed(erlang:now());
{X,X,X} ->
random:seed(erlang:now());
_ ->
ok
end.

0 comments on commit ca76585

Please sign in to comment.