-
Notifications
You must be signed in to change notification settings - Fork 672
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
Bind ports should not use the same port numbers #5272
Conversation
Signed-off-by: Jacinta Ferrant <[email protected]>
Signed-off-by: Jacinta Ferrant <[email protected]>
What about something like this, which doesn't require tracking used ports within tests: use std::collections::HashSet;
use std::sync::Mutex;
use rand::Rng;
use lazy_static::lazy_static;
lazy_static! {
static ref USED_PORTS: Mutex<HashSet<u16>> = Mutex::new(HashSet::new());
}
pub fn gen_random_port() -> u16 {
let mut rng = rand::thread_rng();
loop {
let port = rng.gen_range(1024..=65534);
let mut ports = USED_PORTS.lock().unwrap();
if !ports.contains(&port) {
ports.insert(port);
return port;
}
}
} |
EDIT: Thanks @hstove . Nice clean option. All good :D |
Signed-off-by: Jacinta Ferrant <[email protected]>
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.
Looks good, thanks! I just pointed out some cleanup to avoid warnings.
Signed-off-by: Jacinta Ferrant <[email protected]>
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.
🎉 Now it's beautiful! 🤣 Thanks @jferrant!!
Signed-off-by: Jacinta Ferrant <[email protected]>
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
No description provided.