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:
Jonathan Johnson
2023-06-07 01:15:52 -07:00
committed by GitHub
parent 65bbb1e154
commit 98f5fe2a3f
3 changed files with 40 additions and 0 deletions
+12
View File
@@ -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
+13
View File
@@ -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());
}
+15
View File
@@ -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.