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 committed Mar 29, 2024
1 parent 16ae0f7 commit e80c093
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!("rejecting due to open connection limit");
conn.reject();
} else if Some(conn.remote_address()) == options.block {
info!("rejecting blocked client IP address");
conn.reject();
} else if options.stateless_retry && !conn.remote_address_validated() {
Expand Down

0 comments on commit e80c093

Please sign in to comment.