Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Endpoint::reject_new_connections #1585

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this me Arc::make_mut and also handle a case where the value is used concurrently more explicitly? I don't think clone-on-write is really what's meant here...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the purposes of this code, it seems sufficient to me that self.server_config holds the modified config. Seems to me that we don't really care that Connections are holding on to a "stale" ServerConfig that still allows more concurrent_connections.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misread what Arc::make_mut actually does. Yeah, this design is good!

}
}

/// 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 @@ -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 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