diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index de8dd537c9..109f7f4681 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -9,9 +9,31 @@ env: RUST_BACKTRACE: 1 jobs: + style: + name: Check Style + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v1 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt + + - name: cargo fmt --check + uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + + test: name: Test ${{ matrix.rust }} on ${{ matrix.os }} - + needs: [style] strategy: matrix: rust: diff --git a/.rustfmt.toml b/.rustfmt.toml deleted file mode 100644 index c7ad93bafe..0000000000 --- a/.rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -disable_all_formatting = true diff --git a/benches/connect.rs b/benches/connect.rs index b9d13198f5..c8ca68a769 100644 --- a/benches/connect.rs +++ b/benches/connect.rs @@ -3,11 +3,11 @@ extern crate test; +use http::Uri; +use hyper::client::connect::HttpConnector; +use hyper::service::Service; use std::net::SocketAddr; use tokio::net::TcpListener; -use hyper::client::connect::{HttpConnector}; -use hyper::service::Service; -use http::Uri; #[bench] fn http_connector(b: &mut test::Bencher) { @@ -17,7 +17,9 @@ fn http_connector(b: &mut test::Bencher) { .basic_scheduler() .build() .expect("rt build"); - let mut listener = rt.block_on(TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 0)))).expect("bind"); + let mut listener = rt + .block_on(TcpListener::bind(&SocketAddr::from(([127, 0, 0, 1], 0)))) + .expect("bind"); let addr = listener.local_addr().expect("local_addr"); let dst: Uri = format!("http://{}/", addr).parse().expect("uri parse"); let mut connector = HttpConnector::new(); @@ -28,7 +30,6 @@ fn http_connector(b: &mut test::Bencher) { } }); - b.iter(|| { rt.block_on(async { connector.call(dst.clone()).await.expect("connect"); diff --git a/benches/end_to_end.rs b/benches/end_to_end.rs index 68ee7f053d..281852d8a7 100644 --- a/benches/end_to_end.rs +++ b/benches/end_to_end.rs @@ -7,15 +7,14 @@ use std::net::SocketAddr; use futures_util::future::join_all; -use hyper::{body::HttpBody as _, Body, Method, Request, Response, Server}; use hyper::client::HttpConnector; +use hyper::{body::HttpBody as _, Body, Method, Request, Response, Server}; // HTTP1 #[bench] fn http1_get(b: &mut test::Bencher) { - opts() - .bench(b) + opts().bench(b) } #[bench] @@ -48,9 +47,7 @@ fn http1_body_both_10mb(b: &mut test::Bencher) { #[bench] fn http1_parallel_x10_empty(b: &mut test::Bencher) { - opts() - .parallel(10) - .bench(b) + opts().parallel(10).bench(b) } #[bench] @@ -76,19 +73,13 @@ fn http1_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) { #[bench] fn http1_parallel_x10_res_1mb(b: &mut test::Bencher) { let body = &[b'x'; 1024 * 1024 * 1]; - opts() - .parallel(10) - .response_body(body) - .bench(b) + opts().parallel(10).response_body(body).bench(b) } #[bench] fn http1_parallel_x10_res_10mb(b: &mut test::Bencher) { let body = &[b'x'; 1024 * 1024 * 10]; - opts() - .parallel(10) - .response_body(body) - .bench(b) + opts().parallel(10).response_body(body).bench(b) } // HTTP2 @@ -97,9 +88,7 @@ const HTTP2_MAX_WINDOW: u32 = std::u32::MAX >> 1; #[bench] fn http2_get(b: &mut test::Bencher) { - opts() - .http2() - .bench(b) + opts().http2().bench(b) } #[bench] @@ -123,10 +112,7 @@ fn http2_req_100kb(b: &mut test::Bencher) { #[bench] fn http2_parallel_x10_empty(b: &mut test::Bencher) { - opts() - .http2() - .parallel(10) - .bench(b) + opts().http2().parallel(10).bench(b) } #[bench] @@ -293,9 +279,10 @@ impl Opts { let make_request = || { let chunk_cnt = self.request_chunks; let body = if chunk_cnt > 0 { - let (mut tx, body) = Body::channel(); - let chunk = self.request_body.expect("request_chunks means request_body"); + let chunk = self + .request_body + .expect("request_chunks means request_body"); exec.spawn(async move { for _ in 0..chunk_cnt { tx.send_data(chunk.into()).await.expect("send_data"); @@ -303,8 +290,7 @@ impl Opts { }); body } else { - self - .request_body + self.request_body .map(Body::from) .unwrap_or_else(|| Body::empty()) }; @@ -328,14 +314,12 @@ impl Opts { let req = make_request(); rt.block_on(send_request(req)); }); - } else { b.iter(|| { - let futs = (0..self.parallel_cnt) - .map(|_| { - let req = make_request(); - send_request(req) - }); + let futs = (0..self.parallel_cnt).map(|_| { + let req = make_request(); + send_request(req) + }); // Await all spawned futures becoming completed. rt.block_on(join_all(futs)); }); @@ -353,12 +337,16 @@ fn spawn_server(rt: &mut tokio::runtime::Runtime, opts: &Opts) -> SocketAddr { .http2_only(opts.http2) .http2_initial_stream_window_size(opts.http2_stream_window) .http2_initial_connection_window_size(opts.http2_conn_window) - .serve(make_service_fn( move |_| async move { - Ok::<_, hyper::Error>(service_fn(move |req: Request
| async move { - let mut req_body = req.into_body(); - while let Some(_chunk) = req_body.data().await {} - Ok::<_, hyper::Error>(Response::new(Body::from(body))) - })) + .serve(make_service_fn(move |_| { + async move { + Ok::<_, hyper::Error>(service_fn(move |req: Request| { + async move { + let mut req_body = req.into_body(); + while let Some(_chunk) = req_body.data().await {} + Ok::<_, hyper::Error>(Response::new(Body::from(body))) + } + })) + } })) }); let addr = srv.local_addr(); @@ -367,5 +355,5 @@ fn spawn_server(rt: &mut tokio::runtime::Runtime, opts: &Opts) -> SocketAddr { panic!("server error: {}", err); } }); - return addr + return addr; } diff --git a/benches/pipeline.rs b/benches/pipeline.rs index 562c373517..40b86e3173 100644 --- a/benches/pipeline.rs +++ b/benches/pipeline.rs @@ -4,14 +4,14 @@ extern crate test; use std::io::{Read, Write}; -use std::net::{TcpStream}; +use std::net::TcpStream; use std::sync::mpsc; use std::time::Duration; use tokio::sync::oneshot; -use hyper::{Body, Response, Server}; use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Response, Server}; const PIPELINED_REQUESTS: usize = 16; @@ -25,10 +25,12 @@ fn hello_world(b: &mut test::Bencher) { std::thread::spawn(move || { let addr = "127.0.0.1:0".parse().unwrap(); - let make_svc = make_service_fn(|_| async { - Ok::<_, hyper::Error>(service_fn(|_| async { - Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!"))) - })) + let make_svc = make_service_fn(|_| { + async { + Ok::<_, hyper::Error>(service_fn(|_| { + async { Ok::<_, hyper::Error>(Response::new(Body::from("Hello, World!"))) } + })) + } }); let mut rt = tokio::runtime::Builder::new() @@ -44,10 +46,9 @@ fn hello_world(b: &mut test::Bencher) { addr_tx.send(srv.local_addr()).unwrap(); - let graceful = srv - .with_graceful_shutdown(async { - until_rx.await.ok(); - }); + let graceful = srv.with_graceful_shutdown(async { + until_rx.await.ok(); + }); rt.block_on(async { if let Err(e) = graceful.await { @@ -66,7 +67,8 @@ fn hello_world(b: &mut test::Bencher) { let total_bytes = { let mut tcp = TcpStream::connect(addr).unwrap(); - tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").unwrap(); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n") + .unwrap(); let mut buf = Vec::new(); tcp.read_to_end(&mut buf).unwrap() } * PIPELINED_REQUESTS; @@ -85,4 +87,3 @@ fn hello_world(b: &mut test::Bencher) { assert_eq!(sum, total_bytes); }); } - diff --git a/benches/server.rs b/benches/server.rs index 28f8f21e4d..44e23debd5 100644 --- a/benches/server.rs +++ b/benches/server.rs @@ -11,26 +11,31 @@ use std::time::Duration; use futures_util::{stream, StreamExt}; use tokio::sync::oneshot; -use hyper::{Body, Response, Server}; use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Response, Server}; macro_rules! bench_server { - ($b:ident, $header:expr, $body:expr) => ({ + ($b:ident, $header:expr, $body:expr) => {{ let _ = pretty_env_logger::try_init(); let (_until_tx, until_rx) = oneshot::channel::<()>(); let addr = { let (addr_tx, addr_rx) = mpsc::channel(); std::thread::spawn(move || { let addr = "127.0.0.1:0".parse().unwrap(); - let make_svc = make_service_fn(|_| async { - Ok::<_, hyper::Error>(service_fn(|_| async { - Ok::<_, hyper::Error>(Response::builder() - .header($header.0, $header.1) - .header("content-type", "text/plain") - .body($body()) - .unwrap() - ) - })) + let make_svc = make_service_fn(|_| { + async { + Ok::<_, hyper::Error>(service_fn(|_| { + async { + Ok::<_, hyper::Error>( + Response::builder() + .header($header.0, $header.1) + .header("content-type", "text/plain") + .body($body()) + .unwrap(), + ) + } + })) + } }); let mut rt = tokio::runtime::Builder::new() @@ -39,17 +44,13 @@ macro_rules! bench_server { .build() .expect("rt build"); - let srv = rt.block_on(async move { - Server::bind(&addr) - .serve(make_svc) - }); + let srv = rt.block_on(async move { Server::bind(&addr).serve(make_svc) }); addr_tx.send(srv.local_addr()).unwrap(); - let graceful = srv - .with_graceful_shutdown(async { - until_rx.await.ok(); - }); + let graceful = srv.with_graceful_shutdown(async { + until_rx.await.ok(); + }); rt.block_on(async move { if let Err(e) = graceful.await { panic!("server error: {}", e); @@ -62,7 +63,8 @@ macro_rules! bench_server { let total_bytes = { let mut tcp = TcpStream::connect(addr).unwrap(); - tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").unwrap(); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n") + .unwrap(); let mut buf = Vec::new(); tcp.read_to_end(&mut buf).unwrap() }; @@ -73,14 +75,15 @@ macro_rules! bench_server { $b.bytes = 35 + total_bytes as u64; $b.iter(|| { - tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap(); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n") + .unwrap(); let mut sum = 0; while sum < total_bytes { sum += tcp.read(&mut buf).unwrap(); } assert_eq!(sum, total_bytes); }); - }) + }}; } fn body(b: &'static [u8]) -> hyper::Body { @@ -94,7 +97,9 @@ fn throughput_fixedsize_small_payload(b: &mut test::Bencher) { #[bench] fn throughput_fixedsize_large_payload(b: &mut test::Bencher) { - bench_server!(b, ("content-length", "1000000"), || body(&[b'x'; 1_000_000])) + bench_server!(b, ("content-length", "1000000"), || body( + &[b'x'; 1_000_000] + )) } #[bench] @@ -107,12 +112,16 @@ fn throughput_fixedsize_many_chunks(b: &mut test::Bencher) { #[bench] fn throughput_chunked_small_payload(b: &mut test::Bencher) { - bench_server!(b, ("transfer-encoding", "chunked"), || body(b"Hello, World!")) + bench_server!(b, ("transfer-encoding", "chunked"), || body( + b"Hello, World!" + )) } #[bench] fn throughput_chunked_large_payload(b: &mut test::Bencher) { - bench_server!(b, ("transfer-encoding", "chunked"), || body(&[b'x'; 1_000_000])) + bench_server!(b, ("transfer-encoding", "chunked"), || body( + &[b'x'; 1_000_000] + )) } #[bench] @@ -134,14 +143,17 @@ fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) { let mut buf = [0u8; 8192]; while rx.try_recv().is_err() { sock.read(&mut buf).unwrap(); - sock.write_all(b"\ + sock.write_all( + b"\ HTTP/1.1 200 OK\r\n\ Content-Length: 13\r\n\ Content-Type: text/plain; charset=utf-8\r\n\ Date: Fri, 12 May 2017 18:21:45 GMT\r\n\ \r\n\ Hello, World!\ - ").unwrap(); + ", + ) + .unwrap(); } }); @@ -150,7 +162,8 @@ fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) { b.bytes = 130 + 35; b.iter(|| { - tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap(); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n") + .unwrap(); let n = tcp.read(&mut buf).unwrap(); assert_eq!(n, 130); }); @@ -191,7 +204,8 @@ fn raw_tcp_throughput_large_payload(b: &mut test::Bencher) { b.bytes = expect_read as u64 + 35; b.iter(|| { - tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap(); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n") + .unwrap(); let mut sum = 0; while sum < expect_read { sum += tcp.read(&mut buf).unwrap(); diff --git a/examples/client.rs b/examples/client.rs index f60990e024..de4e4ec3be 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -2,7 +2,7 @@ #![warn(rust_2018_idioms)] use std::env; -use hyper::{Client, body::HttpBody as _}; +use hyper::{body::HttpBody as _, Client}; use tokio::io::{self, AsyncWriteExt as _}; // A simple type alias so as to DRY. diff --git a/examples/client_json.rs b/examples/client_json.rs index a22eac18d4..98a79682d8 100644 --- a/examples/client_json.rs +++ b/examples/client_json.rs @@ -4,8 +4,8 @@ #[macro_use] extern crate serde_derive; -use hyper::Client; use futures_util::StreamExt; +use hyper::Client; // A simple type alias so as to DRY. type Result