-
-
Notifications
You must be signed in to change notification settings - Fork 162
pool-ng #230
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
base: master
Are you sure you want to change the base?
pool-ng #230
Changes from all commits
c87b743
6d7eef9
11159bb
d562e10
7b668a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,157 @@ | ||
use std::env; | ||
use tower::ServiceExt; | ||
use tower_service::Service; | ||
|
||
use http_body_util::Empty; | ||
use hyper::Request; | ||
use hyper_util::client::legacy::{connect::HttpConnector, Client}; | ||
use hyper_util::client::pool; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let url = match env::args().nth(1) { | ||
Some(url) => url, | ||
None => { | ||
eprintln!("Usage: client <url>"); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
// HTTPS requires picking a TLS implementation, so give a better | ||
// warning if the user tries to request an 'https' URL. | ||
let url = url.parse::<hyper::Uri>()?; | ||
if url.scheme_str() != Some("http") { | ||
eprintln!("This example only works with 'http' URLs."); | ||
return Ok(()); | ||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
send_nego().await | ||
} | ||
|
||
async fn send_h1() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
let tcp = hyper_util::client::legacy::connect::HttpConnector::new(); | ||
|
||
let http1 = tcp.and_then(|conn| { | ||
Box::pin(async move { | ||
let (mut tx, c) = hyper::client::conn::http1::handshake::< | ||
_, | ||
http_body_util::Empty<hyper::body::Bytes>, | ||
>(conn) | ||
.await?; | ||
tokio::spawn(async move { | ||
if let Err(e) = c.await { | ||
eprintln!("connection error: {:?}", e); | ||
} | ||
}); | ||
let svc = tower::service_fn(move |req| tx.send_request(req)); | ||
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(svc) | ||
}) | ||
}); | ||
|
||
let mut p = pool::Cache::new(http1).build(); | ||
|
||
let mut c = p.call(http::Uri::from_static("http://hyper.rs")).await?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use consistent uri for all calls. |
||
eprintln!("{:?}", c); | ||
|
||
let req = http::Request::builder() | ||
.header("host", "hyper.rs") | ||
.body(http_body_util::Empty::new()) | ||
.unwrap(); | ||
|
||
c.ready().await?; | ||
let resp = c.call(req).await?; | ||
eprintln!("{:?}", resp); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn send_h2() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
let tcp = hyper_util::client::legacy::connect::HttpConnector::new(); | ||
|
||
let http2 = tcp.and_then(|conn| { | ||
Box::pin(async move { | ||
let (mut tx, c) = hyper::client::conn::http2::handshake::< | ||
_, | ||
_, | ||
http_body_util::Empty<hyper::body::Bytes>, | ||
>(hyper_util::rt::TokioExecutor::new(), conn) | ||
.await?; | ||
println!("connected"); | ||
tokio::spawn(async move { | ||
if let Err(e) = c.await { | ||
eprintln!("connection error: {:?}", e); | ||
} | ||
}); | ||
let svc = tower::service_fn(move |req| tx.send_request(req)); | ||
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(svc) | ||
}) | ||
}); | ||
|
||
let mut p = pool::Singleton::new(http2); | ||
|
||
for _ in 0..5 { | ||
let mut c = p | ||
.call(http::Uri::from_static("http://localhost:3000")) | ||
.await?; | ||
eprintln!("{:?}", c); | ||
|
||
let req = http::Request::builder() | ||
.header("host", "hyper.rs") | ||
.body(http_body_util::Empty::new()) | ||
.unwrap(); | ||
|
||
c.ready().await?; | ||
let resp = c.call(req).await?; | ||
eprintln!("{:?}", resp); | ||
} | ||
|
||
let client = Client::builder(hyper_util::rt::TokioExecutor::new()).build(HttpConnector::new()); | ||
Ok(()) | ||
} | ||
|
||
let req = Request::builder() | ||
.uri(url) | ||
.body(Empty::<bytes::Bytes>::new())?; | ||
async fn send_nego() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: send_negotiate |
||
let tcp = hyper_util::client::legacy::connect::HttpConnector::new(); | ||
|
||
let resp = client.request(req).await?; | ||
let http1 = tower::layer::layer_fn(|tcp| { | ||
tower::service_fn(move |dst| { | ||
let inner = tcp.call(dst); | ||
async move { | ||
let conn = inner.await?; | ||
let (mut tx, c) = hyper::client::conn::http1::handshake::< | ||
_, | ||
http_body_util::Empty<hyper::body::Bytes>, | ||
>(conn) | ||
.await?; | ||
tokio::spawn(async move { | ||
if let Err(e) = c.await { | ||
eprintln!("connection error: {:?}", e); | ||
} | ||
}); | ||
let svc = tower::service_fn(move |req| tx.send_request(req)); | ||
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(svc) | ||
} | ||
}) | ||
}); | ||
|
||
eprintln!("{:?} {:?}", resp.version(), resp.status()); | ||
eprintln!("{:#?}", resp.headers()); | ||
let http2 = tower::layer::layer_fn(|tcp| { | ||
tower::service_fn(move |dst| { | ||
let inner = tcp.call(dst); | ||
async move { | ||
let conn = inner.await?; | ||
let (mut tx, c) = hyper::client::conn::http2::handshake::< | ||
_, | ||
_, | ||
http_body_util::Empty<hyper::body::Bytes>, | ||
>(hyper_util::rt::TokioExecutor::new(), conn) | ||
.await?; | ||
println!("connected"); | ||
tokio::spawn(async move { | ||
if let Err(e) = c.await { | ||
eprintln!("connection error: {:?}", e); | ||
} | ||
}); | ||
let svc = tower::service_fn(move |req| tx.send_request(req)); | ||
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(svc) | ||
} | ||
}) | ||
}); | ||
|
||
let mut svc = pool::negotiate(tcp, |_| false, http1, http2); | ||
|
||
for _ in 0..5 { | ||
let mut c = svc | ||
.call(http::Uri::from_static("http://localhost:3000")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to hyper.rs (or keep consistent with all other tests) |
||
.await?; | ||
eprintln!("{:?}", c); | ||
|
||
let req = http::Request::builder() | ||
.header("host", "hyper.rs") | ||
.body(http_body_util::Empty::new()) | ||
.unwrap(); | ||
|
||
c.ready().await?; | ||
let resp = c.call(req).await?; | ||
eprintln!("{:?}", resp); | ||
} | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::env; | ||
|
||
use http_body_util::Empty; | ||
use hyper::Request; | ||
use hyper_util::client::legacy::{connect::HttpConnector, Client}; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let url = match env::args().nth(1) { | ||
Some(url) => url, | ||
None => { | ||
eprintln!("Usage: client <url>"); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
// HTTPS requires picking a TLS implementation, so give a better | ||
// warning if the user tries to request an 'https' URL. | ||
let url = url.parse::<hyper::Uri>()?; | ||
if url.scheme_str() != Some("http") { | ||
eprintln!("This example only works with 'http' URLs."); | ||
return Ok(()); | ||
} | ||
|
||
let client = Client::builder(hyper_util::rt::TokioExecutor::new()).build(HttpConnector::new()); | ||
|
||
let req = Request::builder() | ||
.uri(url) | ||
.body(Empty::<bytes::Bytes>::new())?; | ||
|
||
let resp = client.request(req).await?; | ||
|
||
eprintln!("{:?} {:?}", resp.version(), resp.status()); | ||
eprintln!("{:#?}", resp.headers()); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: rename file to client_pool