From ff487f01c881ba410f6be54a8b155af3165fae3a Mon Sep 17 00:00:00 2001 From: Phoenix Kahlo Date: Thu, 28 Mar 2024 22:09:44 -0500 Subject: [PATCH] Demonstrate connection limiting in example This commit adds a new --connection-limit option to the server example to illustrate how a user could implement a limit to the number of connections open at a time with the new "incoming" API and Endpoint::open_connections method rather than with the now-removed concurrent_connections ServerConfig parameter. --- quinn/examples/server.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/quinn/examples/server.rs b/quinn/examples/server.rs index 402a29b6c..607f0197f 100644 --- a/quinn/examples/server.rs +++ b/quinn/examples/server.rs @@ -40,6 +40,9 @@ struct Opt { /// Client address to block #[clap(long = "block")] block: Option, + /// Maximum number of concurrent connections to allow + #[clap(long = "connection-limit")] + connection_limit: Option, } fn main() { @@ -145,7 +148,13 @@ async fn run(options: Opt) -> Result<()> { eprintln!("listening on {}", endpoint.local_addr()?); while let Some(conn) = endpoint.accept().await { - if Some(conn.remote_address()) == options.block { + if options + .connection_limit + .map_or(false, |n| endpoint.open_connections() >= n) + { + info!("refusing due to open connection limit"); + conn.refuse(); + } else if Some(conn.remote_address()) == options.block { info!("refusing blocked client IP address"); conn.refuse(); } else if options.stateless_retry && !conn.remote_address_validated() {