use courierust::courierust_body::Body;
use courierust::courierust_client::{Client, ClientConfig};
use courierust::courierust_h2::priority::Priority;
use courierust::courierust_http::request::Request;
use courierust::courierust_http::response::Response;
use courierust::courierust_server::{Server, ServerConfig};
use std::time::Instant;
fn main() -> courierust::Result<()> {
let server_cfg = ServerConfig {
http2: true,
..Default::default()
};
let server = Server::bind_with_config("127.0.0.1:0", server_cfg)?;
let addr = server.local_addr()?;
let _handle = server.serve_background(|_req: Request<Body>| -> Response<Body> {
std::thread::sleep(std::time::Duration::from_millis(5));
let mut resp = Response::with_status(200.into());
resp.body = Body::Bytes(courierust::courierust_bytes::Bytes::from_static(b"ok"));
resp
})?;
let client_cfg = ClientConfig {
http2: true,
..Default::default()
};
let client = Client::with_config(client_cfg);
let url = format!("http://{addr}/bench");
let low = Priority {
urgency: 7,
incremental: false,
};
let high = Priority {
urgency: 0,
incremental: false,
};
let mut lows = Vec::new();
for _ in 0..32 {
let c = client.clone();
let u = url.clone();
lows.push(std::thread::spawn(move || {
let req = Request::new(courierust::courierust_http::method::Method::GET, "/low");
let _ = c.execute_priority(&u, req, low);
}));
}
let start = Instant::now();
let req = Request::new(courierust::courierust_http::method::Method::GET, "/high");
let resp = client.execute_priority(&url, req, high)?;
let elapsed = start.elapsed();
assert_eq!(resp.status.as_u16(), 200);
for h in lows {
let _ = h.join();
}
println!(
"high-urgency request completed in {:?} despite 32 low-urgency requests in flight",
elapsed
);
println!("(low urgency = {}, high urgency = {})", low, high);
Ok(())
}