Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

Pass a host string to get_available_port. #979

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ pub struct PrometheusMetricsParameters {

impl Default for PrometheusMetricsParameters {
fn default() -> Self {
let host = "127.0.0.1";
Self {
socket_addr: format!("/ip4/127.0.0.1/tcp/{}/http", get_available_port())
socket_addr: format!("/ip4/{}/tcp/{}/http", host, get_available_port(host))
.parse()
.unwrap(),
}
Expand All @@ -167,8 +168,9 @@ pub struct ConsensusAPIGrpcParameters {

impl Default for ConsensusAPIGrpcParameters {
fn default() -> Self {
let host = "127.0.0.1";
Self {
socket_addr: format!("/ip4/127.0.0.1/tcp/{}/http", get_available_port())
socket_addr: format!("/ip4/{}/tcp/{}/http", host, get_available_port(host))
.parse()
.unwrap(),
get_collections_timeout: Duration::from_millis(5_000),
Expand Down
8 changes: 4 additions & 4 deletions config/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ use std::net::{TcpListener, TcpStream};
/// Return an ephemeral, available port. On unix systems, the port returned will be in the
/// TIME_WAIT state ensuring that the OS won't hand out this port for some grace period.
/// Callers should be able to bind to this port given they use SO_REUSEADDR.
pub fn get_available_port() -> u16 {
pub fn get_available_port(host: &str) -> u16 {
const MAX_PORT_RETRIES: u32 = 1000;

for _ in 0..MAX_PORT_RETRIES {
if let Ok(port) = get_ephemeral_port() {
if let Ok(port) = get_ephemeral_port(host) {
return port;
}
}

panic!("Error: could not find an available port");
}

fn get_ephemeral_port() -> ::std::io::Result<u16> {
fn get_ephemeral_port(host: &str) -> ::std::io::Result<u16> {
// Request a random available port from the OS
let listener = TcpListener::bind(("localhost", 0))?;
let listener = TcpListener::bind((host, 0))?;
let addr = listener.local_addr()?;

// Create and accept a connection (which we'll promptly drop) in order to force the port
Expand Down
30 changes: 16 additions & 14 deletions test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,16 @@ impl<R> Builder<R> {

impl<R: rand::RngCore + rand::CryptoRng> Builder<R> {
pub fn build(mut self) -> CommitteeFixture {
let get_port = || {
if self.randomize_ports {
get_available_port()
} else {
0
}
};

let authorities = (0..self.committee_size.get())
.map(|_| AuthorityFixture::generate(&mut self.rng, self.number_of_workers, get_port))
.map(|_| {
AuthorityFixture::generate(&mut self.rng, self.number_of_workers, |host| {
if self.randomize_ports {
get_available_port(host)
} else {
0
}
})
})
.collect();

CommitteeFixture {
Expand Down Expand Up @@ -842,11 +842,12 @@ impl AuthorityFixture {
fn generate<R, P>(mut rng: R, number_of_workers: NonZeroUsize, mut get_port: P) -> Self
where
R: rand::RngCore + rand::CryptoRng,
P: FnMut() -> u16,
P: FnMut(&str) -> u16,
{
let keypair = KeyPair::generate(&mut rng);
let network_keypair = NetworkKeyPair::generate(&mut rng);
let address: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}/http", get_port())
let host = "127.0.0.1";
let address: Multiaddr = format!("/ip4/{}/tcp/{}/http", host, get_port(host))
.parse()
.unwrap();

Expand Down Expand Up @@ -887,14 +888,15 @@ impl WorkerFixture {
fn generate<R, P>(mut rng: R, id: WorkerId, mut get_port: P) -> Self
where
R: rand::RngCore + rand::CryptoRng,
P: FnMut() -> u16,
P: FnMut(&str) -> u16,
{
let keypair = NetworkKeyPair::generate(&mut rng);
let worker_name = keypair.public().clone();
let worker_address = format!("/ip4/127.0.0.1/tcp/{}/http", get_port())
let host = "127.0.0.1";
let worker_address = format!("/ip4/{}/tcp/{}/http", host, get_port(host))
.parse()
.unwrap();
let transactions = format!("/ip4/127.0.0.1/tcp/{}/http", get_port())
let transactions = format!("/ip4/{}/tcp/{}/http", host, get_port(host))
.parse()
.unwrap();

Expand Down