| 1 | //! Fast pseudo-random numbers whose sequence differs across concurrently running processes. |
| 2 | //! |
| 3 | //! These numbers are *not* cryptographically secure. Unlike the global generators of the |
| 4 | //! [`fastrand`] crate, whose per-thread seed is derived only from values that can coincide between |
| 5 | //! separate processes (such as small thread IDs), the generator here is seeded from a high-entropy |
| 6 | //! source so that concurrent processes don't easily share a sequence. |
| 7 | //! |
| 8 | //! This matters wherever a collision causes a real problem, such as the jitter used for retry |
| 9 | //! backoff (to avoid a [thundering herd](https://en.wikipedia.org/wiki/Thundering_herd_problem)) or |
| 10 | //! the temporary file names used while probing filesystem capabilities. |
| 11 | |
| 12 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#, opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#}; |
| 13 | |
| 14 | thread_localrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/thread_local!! { |
| 15 | static RNGrust-analyzer cargo gix-utils 0.3.5 rng/RNG.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<fastrandrust-analyzer cargo fastrand 2.4.1 crate/::Rngrust-analyzer cargo fastrand 2.4.1 Rng#> = Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(new_rngrust-analyzer cargo gix-utils 0.3.5 rng/new_rng().()); |
| 16 | } |
| 17 | |
| 18 | /// Return an unsigned number in the given `range`, drawn from a per-thread generator that is seeded to differ |
| 19 | /// across concurrently running processes. |
| 20 | pub fn usizerust-analyzer cargo gix-utils 0.3.5 rng/usize().(range: impl RangeBoundsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeBounds#<usize>) -> usize { |
| 21 | RNGrust-analyzer cargo gix-utils 0.3.5 rng/RNG..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|cell| { |
| 22 | let mut rng = cell.replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]replace().(fastrandrust-analyzer cargo fastrand 2.4.1 crate/::Rngrust-analyzer cargo fastrand 2.4.1 Rng#::with_seedrust-analyzer cargo fastrand 2.4.1 impl#[Rng]with_seed().(0)); |
| 23 | let n = rng.usizerust-analyzer cargo fastrand 2.4.1 impl#[Rng]usize().(range); |
| 24 | cell.setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(rng); |
| 25 | n |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] |
| 30 | fn new_rngrust-analyzer cargo gix-utils 0.3.5 rng/new_rng().() -> fastrandrust-analyzer cargo fastrand 2.4.1 crate/::Rngrust-analyzer cargo fastrand 2.4.1 Rng# { |
| 31 | fastrandrust-analyzer cargo fastrand 2.4.1 crate/::Rngrust-analyzer cargo fastrand 2.4.1 Rng#::with_seedrust-analyzer cargo fastrand 2.4.1 impl#[Rng]with_seed().(seedrust-analyzer cargo gix-utils 0.3.5 rng/seed().()) |
| 32 | } |
| 33 | |
| 34 | /// `wasm32-unknown-unknown` has no OS entropy source and traps at runtime on APIs like |
| 35 | /// `std::process::id()` and `Instant::now()`; it also runs a single process, so the cross-process |
| 36 | /// concern doesn't apply. Use a fixed seed there. |
| 37 | #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 38 | fn new_rng() -> fastrandrust-analyzer cargo fastrand 2.4.1 crate/::Rngrust-analyzer cargo fastrand 2.4.1 Rng# { |
| 39 | fastrandrust-analyzer cargo fastrand 2.4.1 crate/::Rngrust-analyzer cargo fastrand 2.4.1 Rng#::with_seed(0x9e37_79b9_7f4a_7c15) |
| 40 | } |
| 41 | |
| 42 | /// Seed a generator from a high-entropy OS source, so that generators in separate processes don't |
| 43 | /// share a sequence. |
| 44 | #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] |
| 45 | fn seedrust-analyzer cargo gix-utils 0.3.5 rng/seed().() -> u64 { |
| 46 | getrandomrust-analyzer cargo getrandom 0.4.3 crate/::u64rust-analyzer cargo getrandom 0.4.3 u64().().unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| fallback_seedrust-analyzer cargo gix-utils 0.3.5 rng/fallback_seed().()) |
| 47 | } |
| 48 | |
| 49 | /// A best-effort seed used only when the OS entropy source is unavailable. `std::process::id()` is |
| 50 | /// the reliable differentiator across concurrent processes. |
| 51 | #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] |
| 52 | fn fallback_seedrust-analyzer cargo gix-utils 0.3.5 rng/fallback_seed().() -> u64 { |
| 53 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::{BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#}; |
| 54 | |
| 55 | let mut hasher = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[RandomState]new().().build_hasherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[RandomState][BuildHasher]build_hasher().(); |
| 56 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/id().().hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/impls/impl#[u32][Hash]hash().(&mut hasher); |
| 57 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/current/current().().idrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/thread/impl#[Thread]id().().hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/id/impl#[ThreadId][Hash]hash().(&mut hasher); |
| 58 | hasher.finishrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[DefaultHasher][Hasher]finish().() |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod testsrust-analyzer cargo gix-utils 0.3.5 rng/tests/ { |
| 63 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 64 | fn values_are_in_range_and_not_constantrust-analyzer cargo gix-utils 0.3.5 rng/tests/values_are_in_range_and_not_constant().() { |
| 65 | let mut seen = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`]new().(); |
| 66 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#256 { |
| 67 | let n = superrust-analyzer cargo gix-utils 0.3.5 rng/::usizerust-analyzer cargo gix-utils 0.3.5 rng/usize().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#1_000); |
| 68 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(n <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). 1_000, "value {n} must be within the requested range"); |
| 69 | seen.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]insert().(n); |
| 70 | } |
| 71 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 72 | seen.lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]len().() >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 1, |
| 73 | "the generator wasn't made for the Playstation 3 (imprecise, but still funny?)" |
| 74 | ); |
| 75 | } |
| 76 | } |