-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace pkg/rand by standard library math/rand/v2 #32542
Conversation
/test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks and lgtm ✔️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes overall look good to me, thanks!
Just a couple of comments inline. I wonder if it wouldn't be better to convert the few final math/rand
imports over to v2, and then add a linter to forbid using math/rand
altogether, to prevent possible regressions.
Good suggestion! I'll do that in an additional set of commits on this PR given the list of |
Since Go 1.22 the Go standard library provides the math/rand/v2 package[^1]. Its global generator accessed by top-level functions is unconditionally seeded using a true random value. Moreover, the new package provides a way to construct PRNG sources which can be accessed concurrently. This allows to drop SafeRand and use the global source again where applicable. SafeRand was originally introduced in commit fac5dde ("rand: add and use concurrency-safe PRNG source") to fix #10988 by providing a concurrency-safe PRNG source other than the global math/rand source which could be used at the time because it can't be interfered with from unrelated packages by means of calling rand.Seed, see #10575. In some cases the global math/rand/v2 source cannot be used because unit tests rely on seeding the PRNG source to a fixed value for deterministic test runs. In these cases a math/rand/v2 source using fixed values is used, like with SafeRand before. [^1]: https://go.dev/doc/go1.22#math_rand_v2 Signed-off-by: Tobias Klauser <tobias@cilium.io>
The previous commit changed all users of pkg/rand to use math/rand/v2 from the Go standard library. As a result this package is no longer needed, should not be used going forward and can thus be removed. Signed-off-by: Tobias Klauser <tobias@cilium.io>
pkg/rand was removed in the previous commit and we'll rely on math/rand/v2 from the Go standard library going forward. The script checking for pkg/rand usage is thus no longer needed. Signed-off-by: Tobias Klauser <tobias@cilium.io>
Replace use of the standard library math/rand package by math/rand/v2 which provides top-level functions backed by a global generator unconditionally seeded using a true random value. This avoids having to create generators manually. Suggested-by: Marco Iorio <marco.iorio@isovalent.com> Signed-off-by: Tobias Klauser <tobias@cilium.io>
Use crypto/rand.Read instead as suggested in https://pkg.go.dev/math/rand#Read Signed-off-by: Tobias Klauser <tobias@cilium.io>
Use the depguard linter to disallow using the standard library math/rand package and use v2 of the package instead. Suggested-by: Marco Iorio <marco.iorio@isovalent.com> Signed-off-by: Tobias Klauser <tobias@cilium.io>
5ba4edd
to
35587ff
Compare
/test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FQDN changes LGTM, thanks! 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for ci-structure
Since Go 1.22 the Go standard library provides the
math/rand/v2
package1. Its global generator accessed by top-level functions is unconditionally seeded using a true random value. Moreover, the new package provides a way to construct PRNG sources which can be accessed concurrently.This allows to drop
SafeRand
and use the global source again where applicable.SafeRand
was originally introduced in commit fac5dde ("rand: add and use concurrency-safe PRNG source") to fix #10988 by providing a concurrency-safe PRNG source other than the globalmath/rand
source which could be used at the time because it can't be interfered with from unrelated packages by means of calling rand.Seed, see #10575.In some cases the global
math/rand/v2
source cannot be used because unit tests rely on seeding the PRNG source to a fixed value for deterministic test runs. In these cases amath/rand/v2
source using fixed values is used, like withSafeRand
before.See commits for details.
Supersedes #29731
Footnotes
https://go.dev/doc/go1.22#math_rand_v2 ↩