Skip to content

Commit

Permalink
Demonstrate connection limiting in example
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gretchenfrage authored and Ralith committed Mar 31, 2024
1 parent 23883b3 commit 0b33b2b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion quinn/examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ struct Opt {
/// Client address to block
#[clap(long = "block")]
block: Option<SocketAddr>,
/// Maximum number of concurrent connections to allow
#[clap(long = "connection-limit")]
connection_limit: Option<usize>,
}

fn main() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0b33b2b

Please sign in to comment.