Skip to content

Commit 8ef51ee

Browse files
fix: allow gateways to accept connections when below minimum threshold
Gateways should be able to accept connections directly not just when they have 0 connections (bootstrap mode) but also when they're below their minimum connection threshold. This fixes the issue where peer2 couldn't connect in the test because the gateway had 1 connection and wasn't accepting new ones directly. Co-authored-by: nacho.d.g <iduartgomez@users.noreply.github.com>
1 parent c96b14a commit 8ef51ee

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

crates/core/src/operations/connect.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,33 +1029,46 @@ where
10291029
return Ok(None);
10301030
}
10311031

1032-
if connection_manager.num_connections() == 0 {
1033-
// Check if this node is a gateway that needs to bootstrap
1034-
// Gateways should accept their first connection to bootstrap the network
1035-
// Non-gateways maintain the strict requirement for existing connections
1036-
if is_gateway && accepted {
1032+
let num_connections = connection_manager.num_connections();
1033+
1034+
// Check if gateway needs to accept connections directly
1035+
// Gateways can accept connections when:
1036+
// 1. They have 0 connections (bootstrap mode)
1037+
// 2. They are below their minimum connection threshold
1038+
if is_gateway && accepted && num_connections < connection_manager.min_connections {
1039+
if num_connections == 0 {
10371040
tracing::info!(
10381041
tx = %id,
10391042
joiner = %joiner.peer,
10401043
"Gateway bootstrap: accepting first connection to bootstrap network",
10411044
);
1042-
// Return a state that will lead to accepting this connection
1043-
// Create ConnectivityInfo for gateway bootstrap
1044-
let connectivity_info = ConnectivityInfo::new(
1045-
req_peer.clone(),
1046-
1, // Single check for bootstrap connection
1047-
);
1048-
return Ok(Some(ConnectState::AwaitingConnectivity(connectivity_info)));
10491045
} else {
1050-
tracing::warn!(
1046+
tracing::info!(
10511047
tx = %id,
10521048
joiner = %joiner.peer,
1053-
is_gateway,
1054-
accepted,
1055-
"Couldn't forward connect petition, not enough connections",
1049+
current_connections = num_connections,
1050+
min_connections = connection_manager.min_connections,
1051+
"Gateway accepting connection directly (below minimum threshold)",
10561052
);
1057-
return Ok(None);
10581053
}
1054+
// Return a state that will lead to accepting this connection
1055+
let connectivity_info = ConnectivityInfo::new(
1056+
req_peer.clone(),
1057+
1, // Single check for direct connection
1058+
);
1059+
return Ok(Some(ConnectState::AwaitingConnectivity(connectivity_info)));
1060+
}
1061+
1062+
// Non-gateways and gateways at/above minimum connections need existing connections to forward
1063+
if num_connections == 0 {
1064+
tracing::warn!(
1065+
tx = %id,
1066+
joiner = %joiner.peer,
1067+
is_gateway,
1068+
accepted,
1069+
"Couldn't forward connect petition, not enough connections",
1070+
);
1071+
return Ok(None);
10591072
}
10601073

10611074
let target_peer = {

crates/core/src/ring/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,13 @@ impl Ring {
580580
is_gateway
581581
);
582582

583-
// BOOTSTRAP FIX: If we're a gateway with 0 connections,
584-
// we're in bootstrap mode and need to accept the first connection directly
585-
if is_gateway && current_connections == 0 {
583+
// BOOTSTRAP FIX: Gateways can accept connections directly when below minimum threshold
584+
// This includes bootstrap mode (0 connections) and building up to minimum connections
585+
if is_gateway && current_connections < self.connection_manager.min_connections {
586586
tracing::info!(
587-
"Gateway bootstrap mode: will accept first incoming connection directly"
587+
current = current_connections,
588+
min = self.connection_manager.min_connections,
589+
"Gateway will accept connections directly (below minimum threshold)"
588590
);
589591
}
590592

0 commit comments

Comments
 (0)