Permalink
Browse files

Merge pull request #2 from ferd/master

Random Seeding optimization
  • Loading branch information...
2 parents 931ed2e + ca76585 commit 6f78233b52c7992483d646a085f34271f05380f4 @lpgauth committed Jul 19, 2012
Showing with 16 additions and 1 deletion.
  1. +16 −1 src/statsderl.erl
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.

0 comments on commit 6f78233

Please sign in to comment.