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

local test #2054

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
50 changes: 36 additions & 14 deletions quinn/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use url::Url;
mod common;

/// HTTP/0.9 over QUIC client
#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
#[clap(name = "client")]
struct Opt {
/// Perform NSS-compatible TLS key logging to the file specified in `SSLKEYLOGFILE`.
Expand Down Expand Up @@ -68,15 +68,8 @@ fn main() {

#[tokio::main]
async fn run(options: Opt) -> Result<()> {
let url = options.url;
let url_host = strip_ipv6_brackets(url.host_str().unwrap());
let remote = (url_host, url.port().unwrap_or(4433))
.to_socket_addrs()?
.next()
.ok_or_else(|| anyhow!("couldn't resolve to an address"))?;

let mut roots = rustls::RootCertStore::empty();
if let Some(ca_path) = options.ca {
if let Some(ref ca_path) = options.ca {
roots.add(CertificateDer::from(fs::read(ca_path)?))?;
} else {
let dirs = directories_next::ProjectDirs::from("org", "quinn", "quinn-examples").unwrap();
Expand All @@ -101,8 +94,38 @@ async fn run(options: Opt) -> Result<()> {
client_crypto.key_log = Arc::new(rustls::KeyLogFile::new());
}

let client_config =
let options = Arc::new(options);
let mut handles = vec![];
info!("starting");
for _i in 0..5 {
let client_crypto = client_crypto.clone();
let options = options.clone();
let handle = tokio::spawn(async move {
let _ = work(client_crypto, options).await;
});
handles.push(handle);
}
for handle in handles {
handle.await?
}

info!("exiting");
Ok(())
}

async fn work(client_crypto: rustls::ClientConfig, options: Arc<Opt>) -> Result<()> {
let url = options.url.clone();
let url_host = strip_ipv6_brackets(url.host_str().unwrap());
let remote = (url_host, url.port().unwrap_or(4433))
.to_socket_addrs()?
.next()
.ok_or_else(|| anyhow!("couldn't resolve to an address"))?;
let mut client_config =
quinn::ClientConfig::new(Arc::new(QuicClientConfig::try_from(client_crypto)?));
let mut transport_config = quinn::TransportConfig::default();
transport_config.max_idle_timeout(Some(std::time::Duration::from_secs(1).try_into()?));
client_config.transport_config(Arc::new(transport_config));

let mut endpoint = quinn::Endpoint::client(options.bind)?;
endpoint.set_default_client_config(client_config);

Expand Down Expand Up @@ -144,13 +167,12 @@ async fn run(options: Opt) -> Result<()> {
duration,
resp.len() as f32 / (duration_secs(&duration) * 1024.0)
);
io::stdout().write_all(&resp).unwrap();
io::stdout().flush().unwrap();
//io::stdout().write_all(&resp).unwrap();
//io::stdout().flush().unwrap();
conn.close(0u32.into(), b"done");

// Give the server a fair chance to receive the close packet
endpoint.wait_idle().await;

endpoint.close(0u8.into(), b"");
Ok(())
}

Expand Down
25 changes: 19 additions & 6 deletions quinn/examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ async fn run(options: Opt) -> Result<()> {
quinn::ServerConfig::with_crypto(Arc::new(QuicServerConfig::try_from(server_crypto)?));
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
transport_config.max_concurrent_uni_streams(0_u8.into());
transport_config.max_idle_timeout(Some(std::time::Duration::from_secs(2).try_into()?));
transport_config.keep_alive_interval(Some(std::time::Duration::from_millis(500).try_into()?));

let root = Arc::<Path>::from(options.root.clone());
if !root.exists() {
Expand All @@ -137,6 +139,8 @@ async fn run(options: Opt) -> Result<()> {
let endpoint = quinn::Endpoint::server(server_config, options.listen)?;
eprintln!("listening on {}", endpoint.local_addr()?);

let mut longsleep = true;

while let Some(conn) = endpoint.accept().await {
if options
.connection_limit
Expand All @@ -151,13 +155,22 @@ async fn run(options: Opt) -> Result<()> {
info!("requiring connection to validate its address");
conn.retry().unwrap();
} else {
info!("accepting connection");
info!("attempt to accept connection");
let fut = handle_connection(root.clone(), conn);
tokio::spawn(async move {
if let Err(e) = fut.await {
error!("connection failed: {reason}", reason = e.to_string())
}
});

if longsleep {
// If this sleep is longer than the min idle timeout of both endpoints,
// It will cause failures
// This "longsleep" simulates the first bad request to break the system.
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
longsleep = false;
}

// tokio::spawn(async move {
if let Err(e) = fut.await {
error!("connection failed: {reason}", reason = e.to_string())
}
//});
}
}

Expand Down
Loading