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>>,
closing_since: Option<Instant>,
config: PeerConfig,
accepting_connections: bool,
}
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(),
closing_since: None,
config,
accepting_connections: true,
}
}
@ -257,6 +260,10 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
if conn.state == ConnectionState::Connecting {
conn.state = ConnectionState::ConnectingNearlyReady;
}
} else {
if !self.accepting_connections {
self.connections
.insert(*addr, Connection::from(*addr, ConnectionState::Closing));
} else {
self.connections.insert(
*addr,
@ -264,6 +271,7 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
);
}
}
}
Package::Ping => {
if let Some(conn) = self.connections.get_mut(addr) {
if conn.state == ConnectionState::ConnectingNearlyReady
@ -413,6 +421,10 @@ impl<T: std::fmt::Debug + Clone + Serialize + DeserializeOwned + Send + Sync + '
pub fn connections(&self) -> Vec<Connection<T>> {
self.connections.values().cloned().collect()
}
pub fn set_accepting_connections(&mut self, accepting: bool) {
self.accepting_connections = accepting;
}
}
/// 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()
}
pub fn set_accepting_connections(&mut self, accepting: bool) {
self.connection_mgr.set_accepting_connections(accepting);
}
pub fn statistics(&self) -> NetStats {
let conns = self.connection_mgr.connections();
let all_stats = conns.iter().map(|c| c.statistics());