Skip to content

refactor(examples): extract Http::new outside loops #3001

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

Closed
wants to merge 4 commits into from
Closed
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
25 changes: 12 additions & 13 deletions benches/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,21 @@ fn hello_world_16(b: &mut test::Bencher) {
let listener = rt.block_on(TcpListener::bind(addr)).unwrap();
let addr = listener.local_addr().unwrap();

let mut http = Http::new();
http.pipeline_flush(true);
rt.spawn(async move {
loop {
let (stream, _addr) = listener.accept().await.expect("accept");

Http::new()
.pipeline_flush(true)
.serve_connection(
stream,
service_fn(|_| async {
Ok::<_, Infallible>(Response::new(Full::new(Bytes::from(
"Hello, World!",
))))
}),
)
.await
.unwrap();
http.serve_connection(
stream,
service_fn(|_| {
std::future::ready(Ok::<_, Infallible>(Response::new(Full::new(
Bytes::from("Hello, World!"),
))))
}),
)
.await
.unwrap();
}
});

Expand Down
31 changes: 15 additions & 16 deletions benches/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,24 @@ macro_rules! bench_server {
let listener = rt.block_on(tokio::net::TcpListener::bind(addr)).unwrap();
let addr = listener.local_addr().unwrap();

let http = Http::new();
rt.spawn(async move {
loop {
let (stream, _) = listener.accept().await.expect("accept");

Http::new()
.serve_connection(
stream,
service_fn(|_| async {
Ok::<_, hyper::Error>(
Response::builder()
.header($header.0, $header.1)
.header("content-type", "text/plain")
.body($body())
.unwrap(),
)
}),
)
.await
.unwrap();
http.serve_connection(
stream,
service_fn(|_| {
std::future::ready(Ok::<_, hyper::Error>(
Response::builder()
.header($header.0, $header.1)
.header("content-type", "text/plain")
.body($body())
.unwrap(),
))
}),
)
.await
.unwrap();
}
});

Expand Down
6 changes: 4 additions & 2 deletions examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

let future = http.serve_connection(stream, service_fn(echo));
tokio::task::spawn(async move {
if let Err(err) = Http::new().serve_connection(stream, service_fn(echo)).await {
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
Expand Down
4 changes: 3 additions & 1 deletion examples/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Listening on http://{}", in_addr);
println!("Proxying on http://{}", out_addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

Expand Down Expand Up @@ -55,8 +56,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
});

let future = http.serve_connection(stream, service);
tokio::task::spawn(async move {
if let Err(err) = Http::new().serve_connection(stream, service).await {
if let Err(err) = future.await {
println!("Failed to servce connection: {:?}", err);
}
});
Expand Down
9 changes: 4 additions & 5 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

let future = http.serve_connection(stream, service_fn(hello));
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.serve_connection(stream, service_fn(hello))
.await
{
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
Expand Down
15 changes: 7 additions & 8 deletions examples/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let mut http = Http::new();
http.http1_preserve_header_case(true)
.http1_title_case_headers(true);
loop {
let (stream, _) = listener.accept().await?;

let future = http
.serve_connection(stream, service_fn(proxy))
.with_upgrades();
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.http1_preserve_header_case(true)
.http1_title_case_headers(true)
.serve_connection(stream, service_fn(proxy))
.with_upgrades()
.await
{
if let Err(err) = future.await {
println!("Failed to serve connection: {:?}", err);
}
});
Expand Down
19 changes: 7 additions & 12 deletions examples/multi_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,28 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

let addr1: SocketAddr = ([127, 0, 0, 1], 1337).into();
let addr2: SocketAddr = ([127, 0, 0, 1], 1338).into();
let http = Http::new();

let srv1 = async move {
let srv1 = async {
let listener = TcpListener::bind(addr1).await.unwrap();
loop {
let (stream, _) = listener.accept().await.unwrap();

let future = http.serve_connection(stream, service_fn(index1));
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.serve_connection(stream, service_fn(index1))
.await
{
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
}
};

let srv2 = async move {
let srv2 = async {
let listener = TcpListener::bind(addr2).await.unwrap();
loop {
let (stream, _) = listener.accept().await.unwrap();

let future = http.serve_connection(stream, service_fn(index2));
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.serve_connection(stream, service_fn(index2))
.await
{
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
Expand Down
9 changes: 4 additions & 5 deletions examples/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

let future = http.serve_connection(stream, service_fn(param_example));
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.serve_connection(stream, service_fn(param_example))
.await
{
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
Expand Down
8 changes: 3 additions & 5 deletions examples/send_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

let future = http.serve_connection(stream, service_fn(response_examples));
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.serve_connection(stream, service_fn(response_examples))
.await
{
if let Err(err) = future.await {
println!("Failed to serve connection: {:?}", err);
}
});
Expand Down
8 changes: 3 additions & 5 deletions examples/service_struct_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

let future = http.serve_connection(stream, Svc { counter: 81818 });
tokio::task::spawn(async move {
if let Err(err) = Http::new()
.serve_connection(stream, Svc { counter: 81818 })
.await
{
if let Err(err) = future.await {
println!("Failed to serve connection: {:?}", err);
}
});
Expand Down
9 changes: 4 additions & 5 deletions examples/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {

let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new().with_executor(LocalExec);
loop {
let (stream, _) = listener.accept().await?;

Expand All @@ -83,12 +85,9 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
async move { Ok::<_, Error>(Response::new(Body::from(format!("Request #{}", value)))) }
});

let future = http.serve_connection(stream, service);
tokio::task::spawn_local(async move {
if let Err(err) = Http::new()
.with_executor(LocalExec)
.serve_connection(stream, service)
.await
{
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
Expand Down
11 changes: 8 additions & 3 deletions examples/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

Expand All @@ -46,8 +48,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
});

if let Err(err) = Http::new().serve_connection(stream, service).await {
println!("Error serving connection: {:?}", err);
}
let future = http.serve_connection(stream, service);
tokio::task::spawn(async move {
if let Err(err) = future.await {
println!("Error serving connection: {:?}", err);
}
});
}
}
9 changes: 2 additions & 7 deletions examples/upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,8 @@ async fn main() {

let mut rx = rx.clone();
tokio::task::spawn(async move {
let conn = Http::new().serve_connection(stream, service_fn(server_upgrade));

// Don't forget to enable upgrades on the connection.
let mut conn = conn.with_upgrades();

let mut conn = Http::new().serve_connection(stream, service_fn(server_upgrade)).with_upgrades();
let mut conn = Pin::new(&mut conn);

tokio::select! {
Expand All @@ -170,9 +167,7 @@ async fn main() {
}
});
}
_ = rx.changed() => {
break;
}
_ = rx.changed() => break,
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions examples/web_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ async fn main() -> Result<()> {

let listener = TcpListener::bind(&addr).await?;
println!("Listening on http://{}", addr);

let http = Http::new();
loop {
let (stream, _) = listener.accept().await?;

let future = http.serve_connection(stream, service_fn(response_examples));
tokio::task::spawn(async move {
let service = service_fn(move |req| response_examples(req));

if let Err(err) = Http::new().serve_connection(stream, service).await {
if let Err(err) = future.await {
println!("Failed to serve connection: {:?}", err);
}
});
Expand Down
12 changes: 6 additions & 6 deletions src/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let addr: SocketAddr = ([127, 0, 0, 1], 8080).into();
//!
//! let mut tcp_listener = TcpListener::bind(addr).await?;
//!
//! let mut http = Http::new();
//! http.http1_only(true)
//! .http1_keep_alive(true);
//! loop {
//! let (tcp_stream, _) = tcp_listener.accept().await?;
//! let future = http.serve_connection(tcp_stream, service_fn(hello));
//! tokio::task::spawn(async move {
//! if let Err(http_err) = Http::new()
//! .http1_only(true)
//! .http1_keep_alive(true)
//! .serve_connection(tcp_stream, service_fn(hello))
//! .await {
//! if let Err(http_err) = future.await {
//! eprintln!("Error while serving HTTP connection: {}", http_err);
//! }
//! });
Expand Down