Skip to content

Commit 920cdf2

Browse files
committed
fix(connection): bypass gateway guard for courtesy
1 parent e19d2b3 commit 920cdf2

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

crates/core/src/operations/connect.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub(crate) trait RelayContext {
164164
fn self_location(&self) -> &PeerKeyLocation;
165165

166166
/// Determine whether we should accept the joiner immediately.
167-
fn should_accept(&self, joiner: &PeerKeyLocation) -> bool;
167+
fn should_accept(&self, joiner: &PeerKeyLocation, courtesy: bool) -> bool;
168168

169169
/// Choose the next hop for the request, avoiding peers already visited.
170170
fn select_next_hop(
@@ -215,10 +215,11 @@ impl RelayState {
215215
actions.observed_address = Some((self.request.origin.clone(), observed_addr));
216216
}
217217

218-
if !self.accepted_locally && ctx.should_accept(&self.request.origin) {
218+
let acceptor = ctx.self_location().clone();
219+
let courtesy = ctx.courtesy_hint(&acceptor, &self.request.origin);
220+
221+
if !self.accepted_locally && ctx.should_accept(&self.request.origin, courtesy) {
219222
self.accepted_locally = true;
220-
let acceptor = ctx.self_location().clone();
221-
let courtesy = ctx.courtesy_hint(&acceptor, &self.request.origin);
222223
self.courtesy_hint = courtesy;
223224
actions.accept_response = Some(ConnectResponse {
224225
acceptor: acceptor.clone(),
@@ -282,14 +283,14 @@ impl RelayContext for RelayEnv<'_> {
282283
&self.self_location
283284
}
284285

285-
fn should_accept(&self, joiner: &PeerKeyLocation) -> bool {
286+
fn should_accept(&self, joiner: &PeerKeyLocation, courtesy: bool) -> bool {
286287
let location = joiner
287288
.location
288289
.unwrap_or_else(|| Location::from_address(&joiner.peer.addr));
289290
self.op_manager
290291
.ring
291292
.connection_manager
292-
.should_accept(location, &joiner.peer)
293+
.should_accept(location, &joiner.peer, courtesy)
293294
}
294295

295296
fn select_next_hop(
@@ -789,7 +790,7 @@ pub(crate) async fn join_ring_request(
789790
if !op_manager
790791
.ring
791792
.connection_manager
792-
.should_accept(location, &gateway.peer)
793+
.should_accept(location, &gateway.peer, false)
793794
{
794795
return Err(OpError::ConnError(ConnectionError::UnwantedConnection));
795796
}
@@ -1000,7 +1001,7 @@ mod tests {
10001001
&self.self_loc
10011002
}
10021003

1003-
fn should_accept(&self, _joiner: &PeerKeyLocation) -> bool {
1004+
fn should_accept(&self, _joiner: &PeerKeyLocation, _courtesy: bool) -> bool {
10041005
self.accept
10051006
}
10061007

crates/core/src/ring/connection_manager.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,12 @@ impl ConnectionManager {
163163
///
164164
/// # Panic
165165
/// Will panic if the node checking for this condition has no location assigned.
166-
pub fn should_accept(&self, location: Location, peer_id: &PeerId) -> bool {
167-
tracing::info!("Checking if should accept connection");
166+
pub fn should_accept(&self, location: Location, peer_id: &PeerId, courtesy: bool) -> bool {
167+
let courtesy_join = courtesy && self.is_gateway;
168+
tracing::info!(
169+
courtesy = courtesy_join,
170+
"Checking if should accept connection"
171+
);
168172
let open = self
169173
.open_connections
170174
.load(std::sync::atomic::Ordering::SeqCst);
@@ -245,7 +249,7 @@ impl ConnectionManager {
245249
}
246250

247251
const GATEWAY_DIRECT_ACCEPT_LIMIT: usize = 10;
248-
if self.is_gateway {
252+
if self.is_gateway && !courtesy_join {
249253
let direct_total = open + reserved_before;
250254
if direct_total >= GATEWAY_DIRECT_ACCEPT_LIMIT {
251255
tracing::info!(
@@ -268,6 +272,16 @@ impl ConnectionManager {
268272
return true;
269273
}
270274

275+
if courtesy_join {
276+
tracing::debug!(
277+
%peer_id,
278+
open,
279+
reserved = reserved_before,
280+
"should_accept: accepting courtesy connection despite topology limits"
281+
);
282+
return true;
283+
}
284+
271285
let accepted = if total_conn < self.min_connections {
272286
tracing::info!(%peer_id, total_conn, "should_accept: accepted (below min connections)");
273287
true

0 commit comments

Comments
 (0)