Skip to content

Commit 0d54edb

Browse files
committed
decollision ports better
1 parent b165c18 commit 0d54edb

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

gossip/src/cluster_info.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -2417,9 +2417,7 @@ impl Node {
24172417
pub fn new_localhost_with_pubkey(pubkey: &Pubkey) -> Self {
24182418
let addr = IpAddr::V4(Ipv4Addr::LOCALHOST);
24192419
let port_range = localhost_port_range_for_tests();
2420-
let gossip_port = find_available_port_in_range(addr, port_range)
2421-
.expect("At least one open port should be available");
2422-
let gossip_addr = SocketAddr::new(addr, gossip_port);
2420+
let gossip_addr = SocketAddr::new(addr, port_range.0);
24232421
#[allow(deprecated)] // new_single_bind will be merged in this function
24242422
Self::new_single_bind(pubkey, &gossip_addr, port_range, addr)
24252423
}
@@ -2485,19 +2483,18 @@ impl Node {
24852483
num_tvu_receive_sockets.get(),
24862484
)
24872485
.expect("tvu multi_bind");
2488-
24892486
let (tvu_quic_port, tvu_quic) =
24902487
Self::bind_with_config(bind_ip_addr, port_range, socket_config);
2491-
24922488
let (tpu_port, tpu_sockets) =
24932489
multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 32)
24942490
.expect("tpu multi_bind");
24952491

2496-
let (_tpu_port_quic, tpu_quic) = Self::bind_with_config(
2492+
let (_tpu_port_quic, tpu_quic) = bind_in_range_with_config(
24972493
bind_ip_addr,
24982494
(tpu_port + QUIC_PORT_OFFSET, tpu_port + QUIC_PORT_OFFSET + 1),
24992495
socket_config_reuseport,
2500-
);
2496+
)
2497+
.expect("TPU quic port bind should succeed");
25012498
let tpu_quic =
25022499
bind_more_with_config(tpu_quic, num_quic_endpoints.get(), socket_config_reuseport)
25032500
.unwrap();
@@ -2506,14 +2503,15 @@ impl Node {
25062503
multi_bind_in_range_with_config(bind_ip_addr, port_range, socket_config_reuseport, 8)
25072504
.expect("tpu_forwards multi_bind");
25082505

2509-
let (_tpu_forwards_port_quic, tpu_forwards_quic) = Self::bind_with_config(
2506+
let (_tpu_forwards_port_quic, tpu_forwards_quic) = bind_in_range_with_config(
25102507
bind_ip_addr,
25112508
(
25122509
tpu_forwards_port + QUIC_PORT_OFFSET,
25132510
tpu_forwards_port + QUIC_PORT_OFFSET + 1,
25142511
),
25152512
socket_config_reuseport,
2516-
);
2513+
)
2514+
.expect("TPU QUIC binding should succeed");
25172515
let tpu_forwards_quic = bind_more_with_config(
25182516
tpu_forwards_quic,
25192517
num_quic_endpoints.get(),

net-utils/src/sockets.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,35 @@ use {
66
},
77
};
88

9-
const BASE_PORT: u16 = 20000;
9+
const BASE_PORT: u16 = 5000;
1010

1111
/// Retrieve a free 100-port slice for unit tests
1212
///
13-
/// When running under nextest, this is guaranteed to provide
13+
/// When running under nextest, this will try to provide
1414
/// a unique slice of port numbers (assuming no other nextest processes
1515
/// are running on the same host) based on NEXTEST_TEST_GLOBAL_SLOT variable
1616
/// The port ranges will be reused following nextest logic.
1717
///
18-
/// When running without nextest, this will bump an atomic and eventually
18+
/// When running without nextest, this will only bump an atomic and eventually
1919
/// panic when it runs out of port numbers to assign.
2020
pub fn localhost_port_range_for_tests() -> (u16, u16) {
21-
static SLICE: AtomicU16 = AtomicU16::new(BASE_PORT);
22-
let start = match std::env::var("NEXTEST_TEST_GLOBAL_SLOT") {
23-
Ok(slot) => {
24-
let slot: u16 = slot.parse().unwrap();
25-
BASE_PORT + slot * 100
26-
}
27-
Err(_) => SLICE.fetch_add(100, Ordering::Relaxed),
28-
};
21+
static SLICE: AtomicU16 = AtomicU16::new(0);
22+
let offset = SLICE.fetch_add(20, Ordering::Relaxed);
23+
let start = offset
24+
+ match std::env::var("NEXTEST_TEST_GLOBAL_SLOT") {
25+
Ok(slot) => {
26+
let slot: u16 = slot.parse().unwrap();
27+
assert!(
28+
offset < 1000,
29+
"Overrunning into the port range of another test!"
30+
);
31+
32+
BASE_PORT + slot * 1000
33+
}
34+
Err(_) => BASE_PORT,
35+
};
2936
assert!(start < 65500, "ran out of port numbers!");
30-
(start, start + 100)
37+
(start, start + 20)
3138
}
3239

3340
pub fn get_gossip_port(

0 commit comments

Comments
 (0)