Skip to content
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

Merged
merged 5 commits into from
Oct 4, 2024

Conversation

jferrant
Copy link
Collaborator

@jferrant jferrant commented Oct 3, 2024

No description provided.

@jferrant jferrant requested a review from a team as a code owner October 3, 2024 20:45
Signed-off-by: Jacinta Ferrant <[email protected]>
@hstove
Copy link
Contributor

hstove commented Oct 4, 2024

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;
        }
    }
}

@jferrant
Copy link
Collaborator Author

jferrant commented Oct 4, 2024

EDIT: Thanks @hstove . Nice clean option. All good :D

@jferrant jferrant requested a review from obycode October 4, 2024 20:51
Copy link
Contributor

@obycode obycode left a 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]>
@jferrant jferrant requested a review from obycode October 4, 2024 21:03
obycode
obycode previously approved these changes Oct 4, 2024
Copy link
Contributor

@obycode obycode left a 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!!

kantai
kantai previously approved these changes Oct 4, 2024
@jferrant jferrant dismissed stale reviews from kantai and obycode via 3690ad9 October 4, 2024 21:13
@jferrant jferrant added this pull request to the merge queue Oct 4, 2024
Merged via the queue into develop with commit 4ecfe28 Oct 4, 2024
1 check passed
@blockstack-devops
Copy link
Contributor

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.

@stacks-network stacks-network locked as resolved and limited conversation to collaborators Oct 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants