Conversation
This adds new PRNG utilities that only use libstd and not the external `rand` crate. This change's motivation are that in tower middleware that need PRNG don't need the complexity and vast utilities of the `rand` crate. This adds a `Rng` trait which abstracts the simple PRNG features tower needs. This also provides a `HasherRng` which uses the `RandomState` type from libstd to generate random `u64` values. In addition, there is an internal only `sample_inplace` which is used within the balance p2c middleware to randomly pick a ready service. This implementation is crate private since its quite specific to the balance implementation. The goal of this in addition to the balance middlware getting `rand` removed is for the upcoming `Retry` changes. The `next_f64` will be used in the jitter portion of the backoff utilities in #685.
seanmonstar
reviewed
Aug 23, 2022
| H: BuildHasher, | ||
| { | ||
| fn next_u64(&mut self) -> u64 { | ||
| let mut hasher = self.hasher.build_hasher(); |
Collaborator
There was a problem hiding this comment.
I don't think you want to build_hasher for each number. That will restart the hasher back to the beginning. I'd expect you'd build a hasher once, and then feed the counter into it over and over. Or am I wrong?
Member
Author
There was a problem hiding this comment.
I thought finished consumed it, but I guess finish just takes a &self. Let me see if that makes any meaningful difference.
Collaborator
There was a problem hiding this comment.
Yea, thinking more, I could be much more wrong than right.
Member
Author
There was a problem hiding this comment.
Ok I've updated it seems to work....
Member
Author
There was a problem hiding this comment.
Follow up here HashMap creates a hasher for each hash so we are reverting back to that setup.
hawkw
reviewed
Aug 24, 2022
Co-authored-by: Eliza Weisman <[email protected]>
This reverts commit ebe9b79.
seanmonstar
approved these changes
Aug 25, 2022
19 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds new PRNG utilities that only use libstd and not the external
randcrate. This change's motivation are that in tower middleware thatneed PRNG don't need the complexity and vast utilities of the
randcrate.
This adds a
Rngtrait which abstracts the simple PRNG features towerneeds. This also provides a
HasherRngwhich uses theRandomStatetype from libstd to generate random
u64values. In addition, there isan internal only
sample_inplacewhich is used within the balance p2cmiddleware to randomly pick a ready service. This implementation is
crate private since its quite specific to the balance implementation.
The goal of this in addition to the balance middlware getting
randremoved is for the upcoming
Retrychanges. Thenext_f64will be usedin the jitter portion of the backoff utilities in #685.