-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
Draft
seanmonstar
wants to merge
6
commits into
master
Choose a base branch
from
pool-ng
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
pool-ng #230
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e8655f
feat(pool): add a Singleton pool type
seanmonstar edf0db4
feat(pool): add a Cache pooling service
seanmonstar 1e18440
feat(pool): add a Negotiate pooling service
seanmonstar 2849897
feat(pool): add a Map pool service type
seanmonstar bc68416
wip: pool-expire
seanmonstar 09a85d0
pools wip
seanmonstar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?; | ||
seanmonstar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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>> { | ||
seanmonstar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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")) | ||
seanmonstar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .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(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.