Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Random Seeding optimization #2

Merged
merged 1 commit into from Jul 19, 2012
Jump to file or symbol
Failed to load files and symbols.
+16 −1
Split
View
@@ -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.