Add toggle to disable accepting new connections

This commit is contained in:
Sofia 2026-07-17 22:12:17 +03:00
parent 4d843d2ccb
commit a947f3a8cb
2 changed files with 20 additions and 4 deletions

View File

@ -49,6 +49,8 @@ pub(crate) struct ConnectionManager<
connections: HashMap<SocketAddr, Connection<T>>, connections: HashMap<SocketAddr, Connection<T>>,
closing_since: Option<Instant>, closing_since: Option<Instant>,
config: PeerConfig, config: PeerConfig,
accepting_connections: bool,
} }
impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + 'static> impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + 'static>
@ -61,6 +63,7 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
connections: HashMap::new(), connections: HashMap::new(),
closing_since: None, closing_since: None,
config, config,
accepting_connections: true,
} }
} }
@ -258,10 +261,15 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
conn.state = ConnectionState::ConnectingNearlyReady; conn.state = ConnectionState::ConnectingNearlyReady;
} }
} else { } else {
self.connections.insert( if !self.accepting_connections {
*addr, self.connections
Connection::from(*addr, ConnectionState::ReceivingConnection), .insert(*addr, Connection::from(*addr, ConnectionState::Closing));
); } else {
self.connections.insert(
*addr,
Connection::from(*addr, ConnectionState::ReceivingConnection),
);
}
} }
} }
Package::Ping => { Package::Ping => {
@ -413,6 +421,10 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
pub fn connections(&self) -> Vec<Connection<T>> { pub fn connections(&self) -> Vec<Connection<T>> {
self.connections.values().cloned().collect() self.connections.values().cloned().collect()
} }
pub fn set_accepting_connections(&mut self, accepting: bool) {
self.accepting_connections = accepting;
}
} }
/// Wrap UdpSocket with some helper methods /// Wrap UdpSocket with some helper methods

View File

@ -238,6 +238,10 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
self.connection_mgr.connections() self.connection_mgr.connections()
} }
pub fn set_accepting_connections(&mut self, accepting: bool) {
self.connection_mgr.set_accepting_connections(accepting);
}
pub fn statistics(&self) -> NetStats { pub fn statistics(&self) -> NetStats {
let conns = self.connection_mgr.connections(); let conns = self.connection_mgr.connections();
let all_stats = conns.iter().map(|c| c.statistics()); let all_stats = conns.iter().map(|c| c.statistics());