Skip to content

Commit 957f9a4

Browse files
committed
rpc port alloc fix
1 parent 0d54edb commit 957f9a4

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

gossip/src/cluster_info.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use {
5252
solana_ledger::shred::Shred,
5353
solana_net_utils::{
5454
bind_in_range, bind_in_range_with_config, bind_more_with_config, bind_to_unspecified,
55-
bind_to_with_config, find_available_port_in_range, multi_bind_in_range_with_config,
55+
bind_to_with_config, find_available_ports_in_range, multi_bind_in_range_with_config,
5656
sockets::{get_gossip_port, localhost_port_range_for_tests},
5757
PortRange, SocketConfig, VALIDATOR_PORT_RANGE,
5858
},
@@ -2449,8 +2449,9 @@ impl Node {
24492449
.expect("Number of QUIC endpoints can not be zero"),
24502450
};
24512451
let mut node = Self::new_with_external_ip(pubkey, config);
2452-
let rpc_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
2453-
let rpc_pubsub_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
2452+
let rpc_ports = find_available_ports_in_range(bind_ip_addr, port_range, 2).unwrap();
2453+
let rpc_port = rpc_ports[0];
2454+
let rpc_pubsub_port = rpc_ports[1];
24542455
node.info.set_rpc((bind_ip_addr, rpc_port)).unwrap();
24552456
node.info
24562457
.set_rpc_pubsub((bind_ip_addr, rpc_pubsub_port))

net-utils/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -607,18 +607,28 @@ pub fn bind_two_in_range_with_offset_and_config(
607607
/// This will start at a random point in the range provided, and search sequenctially.
608608
/// If it can not find anything, an Error is returned.
609609
pub fn find_available_port_in_range(ip_addr: IpAddr, range: PortRange) -> io::Result<u16> {
610+
find_available_ports_in_range(ip_addr, range, 1).map(|v| *v.first().unwrap())
611+
}
612+
613+
pub fn find_available_ports_in_range(
614+
ip_addr: IpAddr,
615+
range: PortRange,
616+
mut num: usize,
617+
) -> io::Result<Vec<u16>> {
618+
let mut result = vec![];
610619
let range = range.0..range.1;
611620
let mut next_port_to_try = range
612621
.clone()
613622
.cycle() // loop over the end of the range
614623
.skip(thread_rng().gen_range(range.clone()) as usize) // skip to random position
615624
.take(range.len()) // never take the same value twice
616625
.peekable();
617-
loop {
626+
while num > 0 {
618627
let port_to_try = next_port_to_try.next().unwrap(); // this unwrap never fails since we exit earlier
619628
match bind_common(ip_addr, port_to_try) {
620629
Ok(_) => {
621-
return Ok(port_to_try);
630+
num -= 1;
631+
result.push(port_to_try);
622632
}
623633
Err(err) => {
624634
if next_port_to_try.peek().is_none() {
@@ -627,6 +637,7 @@ pub fn find_available_port_in_range(ip_addr: IpAddr, range: PortRange) -> io::Re
627637
}
628638
}
629639
}
640+
return Ok(result);
630641
}
631642

632643
pub fn bind_more_with_config(

test-validator/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -967,8 +967,9 @@ impl TestValidator {
967967
node.info.set_rpc((addr, rpc)).unwrap();
968968
node.info.set_rpc_pubsub((addr, rpc_pubsub)).unwrap();
969969
} else {
970-
let rpc_port = find_available_port_in_range(addr, port_range).unwrap();
971-
let rpc_pubsub_port = find_available_port_in_range(addr, port_range).unwrap();
970+
let rpc_ports = find_available_ports_in_range(bind_ip_addr, port_range, 2).unwrap();
971+
let rpc_port = rpc_ports[0];
972+
let rpc_pubsub_port = rpc_ports[1];
972973
node.info.set_rpc((addr, rpc_port)).unwrap();
973974
node.info.set_rpc_pubsub((addr, rpc_pubsub_port)).unwrap();
974975
}

0 commit comments

Comments
 (0)