You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The NewRelic::Agent::GuidGenerator module's generate_guid method uses a mixture of rand and rjust and has a source code comment that explains why SecureRandom is avoided.
With the Ruby versions supported by the current version of the agent, we can now consider the source code comment's cautions about running out of file descriptors to be outdated because of Bug #9569's resolution 7 years ago.
If we replace rand and rjust with SecureRandom.hex, we stand to gain a performance boost.
module_function
- MAX_RAND_16 = 16**16- MAX_RAND_32 = 16**32- # This method intentionally does not use SecureRandom, because it relies- # on urandom, which raises an exception in MRI when the interpreter runs- # out of allocated file descriptors.- # The guids generated by this method may not be _secure_, but they are- # random enough for our purposes.
def generate_guid(length = 16)
- guid = case length- when 16- rand(MAX_RAND_16)- when 32- rand(MAX_RAND_32)- else- rand(16**length)- end.to_s(16)- guid.length < length ? guid.rjust(length, '0') : guid+ SecureRandom.hex(length / 2)
end
The
NewRelic::Agent::GuidGenerator
module'sgenerate_guid
method uses a mixture ofrand
andrjust
and has a source code comment that explains whySecureRandom
is avoided.With the Ruby versions supported by the current version of the agent, we can now consider the source code comment's cautions about running out of file descriptors to be outdated because of Bug #9569's resolution 7 years ago.
If we replace
rand
andrjust
withSecureRandom.hex
, we stand to gain a performance boost.to test this change, here is a benchmark script:
which on my archaic machine produced the following numbers:
The text was updated successfully, but these errors were encountered: