Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Random Seeding optimization #2

Merged
merged 1 commit into from

2 participants

@ferd

Only seed when no good seed is present in the process calling.

@ferd ferd Optimization for random seeding
Only seed when no good seed is present in the process calling.
ca76585
@lpgauth lpgauth merged commit 6f78233 into lpgauth:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 19, 2012
  1. @ferd

    Optimization for random seeding

    ferd authored
    Only seed when no good seed is present in the process calling.
This page is out of date. Refresh to see the latest.
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.
Something went wrong with that request. Please try again.