mirror of
https://github.com/n0-computer/noq.git
synced 2026-09-24 03:58:19 +00:00
Added Endpoint::reject_new_connections (#1585)
This commit adds a new function that refuses new connections without impacting existing connections. Internally, this just sets the connection limit to 0, which causes incoming connections to be rejected. This is the same approach that was taken in 0.8.5 when `Incoming` was dropped.
This commit is contained in:
@@ -653,6 +653,18 @@ impl Endpoint {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reject new incoming connections without affecting existing connections
|
||||
///
|
||||
/// Convenience short-hand for using
|
||||
/// [`set_server_config`](Self::set_server_config) to update
|
||||
/// [`concurrent_connections`](ServerConfig::concurrent_connections) to
|
||||
/// zero.
|
||||
pub fn reject_new_connections(&mut self) {
|
||||
if let Some(config) = self.server_config.as_mut() {
|
||||
Arc::make_mut(config).concurrent_connections(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Access the configuration used by this endpoint
|
||||
pub fn config(&self) -> &EndpointConfig {
|
||||
&self.config
|
||||
|
||||
@@ -2193,3 +2193,16 @@ fn stream_chunks(mut recv: RecvStream) -> Vec<u8> {
|
||||
|
||||
buf
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_new_connections() {
|
||||
let _guard = subscribe();
|
||||
let mut pair = Pair::default();
|
||||
pair.server.reject_new_connections();
|
||||
|
||||
// The server should now reject incoming connections.
|
||||
let client_ch = pair.begin_connect(client_config());
|
||||
pair.drive();
|
||||
pair.server.assert_no_accept();
|
||||
assert!(pair.client.connections.get(&client_ch).unwrap().is_closed());
|
||||
}
|
||||
|
||||
@@ -236,6 +236,21 @@ impl Endpoint {
|
||||
self.inner.state.lock().unwrap().socket.local_addr()
|
||||
}
|
||||
|
||||
/// Reject new incoming connections without affecting existing connections
|
||||
///
|
||||
/// Convenience short-hand for using
|
||||
/// [`set_server_config`](Self::set_server_config) to update
|
||||
/// [`concurrent_connections`](ServerConfig::concurrent_connections) to
|
||||
/// zero.
|
||||
pub fn reject_new_connections(&self) {
|
||||
self.inner
|
||||
.state
|
||||
.lock()
|
||||
.unwrap()
|
||||
.inner
|
||||
.reject_new_connections();
|
||||
}
|
||||
|
||||
/// Close all of this endpoint's connections immediately and cease accepting new connections.
|
||||
///
|
||||
/// See [`Connection::close()`] for details.
|
||||
|
||||
Reference in New Issue
Block a user