Permalink
Browse files

Optimization for random seeding

Only seed when no good seed is present in the process calling.
  • Loading branch information...
1 parent 931ed2e commit ca76585e888eb9d6f5ed222e14a2f49151fa3cfe @ferd committed Jul 19, 2012
Showing with 16 additions and 1 deletion.
  1. +16 −1 src/statsderl.erl
View
17 src/statsderl.erl
@@ -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),
@@ -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.