Skip to content

Commit

Permalink
feat: limit number of active connections and streams
Browse files Browse the repository at this point in the history
Closes #19,#18
  • Loading branch information
dignifiedquire committed Jan 18, 2023
1 parent af15f94 commit 1dd46e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,38 @@ mod tests {

Ok(())
}

#[tokio::test]
async fn multiple_clients() -> Result<()> {
let dir: PathBuf = testdir!();
let path = dir.join("hello_world");
tokio::fs::write(&path, "hello world!").await?;
let db = server::create_db(vec![&path]).await?;
let hash = *db.iter().next().unwrap().0;
tokio::task::spawn(async move {
server::run(db, Default::default()).await.unwrap();
});

async fn run_client(hash: bao::Hash, path: PathBuf) -> Result<()> {
let opts = client::Options::default();
let (mut source, sink) = tokio::io::duplex(1024);
client::run(hash, opts, sink).await?;
let expect = tokio::fs::read(path).await?;
let mut got = Vec::new();
source.read_to_end(&mut got).await?;
assert_eq!(expect, got);
Ok(())
}

let mut tasks = Vec::new();
for _i in 0..3 {
tasks.push(tokio::task::spawn(run_client(hash, path.clone())));
}

for task in tasks {
task.await??;
}

Ok(())
}
}
9 changes: 8 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ pub struct Options {
pub port: Option<u16>,
}

const MAX_CLIENTS: u64 = 1024;
const MAX_STREAMS: u64 = 10;

pub async fn run(db: Arc<HashMap<bao::Hash, Data>>, opts: Options) -> Result<()> {
let keypair = Keypair::generate();
let server_config = tls::make_server_config(&keypair)?;
let tls = s2n_quic::provider::tls::rustls::Server::from(server_config);
let limits = s2n_quic::provider::limits::Default::default();
let limits = s2n_quic::provider::limits::Limits::default()
.with_max_active_connection_ids(MAX_CLIENTS)?
.with_max_open_local_bidirectional_streams(MAX_STREAMS)?
.with_max_open_remote_bidirectional_streams(MAX_STREAMS)?;

let port = if let Some(port) = opts.port {
port
} else {
Expand Down

0 comments on commit 1dd46e9

Please sign in to comment.