Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimization for random seeding

Only seed when no good seed is present in the process calling.
  • Loading branch information...
commit ca76585e888eb9d6f5ed222e14a2f49151fa3cfe 1 parent 931ed2e
Fred Hebert authored
Showing with 16 additions and 1 deletion.
  1. +16 −1 src/statsderl.erl
17 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.
Please sign in to comment.
Something went wrong with that request. Please try again.