Skip to content

Commit

Permalink
Added Endpoint::reject_new_connections
Browse files Browse the repository at this point in the history
Closes quinn-rs#1584

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.
  • Loading branch information
ecton committed Jun 6, 2023
1 parent 65bbb1e commit 30639bc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,13 @@ impl Endpoint {
}
}

/// Rejects new connections without affecting existing connections.
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
Expand Down
8 changes: 8 additions & 0 deletions quinn-proto/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,3 +2193,11 @@ 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();
pair.assert_connection_refused();
}
10 changes: 10 additions & 0 deletions quinn-proto/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ impl Pair {
}
}

/// Attempts to connect, asserting that the server doesn't accept the
/// connection and the client marks the connection as closed.
pub(super) fn assert_connection_refused(&mut self) {
dbg!(&self.client.connections);
let client_ch = self.begin_connect(client_config());
self.drive();
self.server.assert_no_accept();
assert!(self.client.connections.get(&client_ch).unwrap().is_closed());
}

pub(super) fn connect(&mut self) -> (ConnectionHandle, ConnectionHandle) {
self.connect_with(client_config())
}
Expand Down
10 changes: 10 additions & 0 deletions quinn/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ impl Endpoint {
self.inner.state.lock().unwrap().socket.local_addr()
}

/// Rejects new connections without affecting existing connections.
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.
Expand Down

0 comments on commit 30639bc

Please sign in to comment.