Skip to content

Commit

Permalink
Added Endpoint::reject_new_connections (#1585)
Browse files Browse the repository at this point in the history
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 authored and Ralith committed Jun 13, 2023
1 parent 1d8ba9b commit 7232187
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,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
Expand Down
13 changes: 13 additions & 0 deletions quinn-proto/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2196,3 +2196,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 changes: 15 additions & 0 deletions quinn/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7232187

Please sign in to comment.