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
Initial push to add tower based clients #1915
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4134d52
Add tower client and connection
LucioFranco e21612e
Inital push to add MakeService and MakeConnection
LucioFranco 8d1ba91
Remove git deps for http-body and tower-make
LucioFranco 6fc4c97
Use executor from builder
LucioFranco f43ffcc
Merge remote-tracking branch 'origin/master' into lucio/tower-client
LucioFranco c1a7ff1
Fix HttpConnector usage
LucioFranco 77526c0
Fix fmt
LucioFranco 4ebdbd9
Merge branch 'master' into lucio/tower-client
seanmonstar File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,26 @@ | ||
|
||
use hyper::client::service::{Connect, Service, MakeService}; | ||
use hyper::client::conn::Builder; | ||
use hyper::client::connect::HttpConnector; | ||
use hyper::{Body, Request}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
pretty_env_logger::init(); | ||
|
||
let mut mk_svc = Connect::new(HttpConnector::new(), Builder::new()); | ||
|
||
let uri = "http://127.0.0.1:8080".parse::<http::Uri>()?; | ||
|
||
|
||
let mut svc = mk_svc.make_service(uri.clone()).await?; | ||
|
||
let body = Body::empty(); | ||
|
||
let req = Request::get(uri).body(body)?; | ||
let res = svc.call(req).await?; | ||
|
||
println!("RESPONSE={:?}", res); | ||
|
||
Ok(()) | ||
} |
This file contains 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 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 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 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,85 @@ | ||
//! Utilities used to interact with the Tower ecosystem. | ||
//! | ||
//! This module provides exports of `Service`, `MakeService` and `Connect` which | ||
//! all provide hook-ins into the Tower ecosystem. | ||
|
||
use super::conn::{SendRequest, Builder}; | ||
use std::marker::PhantomData; | ||
use crate::{common::{Poll, task, Pin}, body::Payload}; | ||
use std::future::Future; | ||
use std::error::Error as StdError; | ||
use tower_make::MakeConnection; | ||
|
||
pub use tower_service::Service; | ||
pub use tower_make::MakeService; | ||
|
||
/// Creates a connection via `SendRequest`. | ||
/// | ||
/// This accepts a `hyper::client::conn::Builder` and provides | ||
/// a `MakeService` implementation to create connections from some | ||
/// target `T`. | ||
#[derive(Debug)] | ||
pub struct Connect<C, B, T> { | ||
LucioFranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inner: C, | ||
builder: Builder, | ||
_pd: PhantomData<fn(T, B)> | ||
} | ||
|
||
impl<C, B, T> Connect<C, B, T> { | ||
/// Create a new `Connect` with some inner connector `C` and a connection | ||
/// builder. | ||
pub fn new(inner: C, builder: Builder) -> Self { | ||
Self { | ||
inner, | ||
builder, | ||
_pd: PhantomData | ||
} | ||
} | ||
} | ||
|
||
impl<C, B, T> Service<T> for Connect<C, B, T> | ||
where | ||
C: MakeConnection<T>, | ||
C::Connection: Unpin + Send + 'static, | ||
C::Future: Send + 'static, | ||
C::Error: Into<Box<dyn StdError + Send + Sync>> + Send, | ||
B: Payload + Unpin + 'static, | ||
B::Data: Unpin, | ||
{ | ||
type Response = SendRequest<B>; | ||
type Error = crate::Error; | ||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx).map_err(|e| crate::Error::new(crate::error::Kind::Connect).with(e.into())) | ||
} | ||
|
||
fn call(&mut self, req: T) -> Self::Future { | ||
let builder = self.builder.clone(); | ||
let io = self.inner.make_connection(req); | ||
|
||
let fut = async move { | ||
match io.await { | ||
Ok(io) => { | ||
match builder.handshake(io).await { | ||
Ok((sr, conn)) => { | ||
builder.exec.execute(async move { | ||
if let Err(e) = conn.await { | ||
debug!("connection error: {:?}", e); | ||
} | ||
})?; | ||
Ok(sr) | ||
}, | ||
Err(e) => Err(e) | ||
} | ||
}, | ||
Err(e) => { | ||
let err = crate::Error::new(crate::error::Kind::Connect).with(e.into()); | ||
Err(err) | ||
} | ||
} | ||
}; | ||
|
||
Box::pin(fut) | ||
} | ||
} |
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.
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.
I am thinking we should rename this to
Connection<B>
andBackground<T, B>
for the oldConnection
.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.
I'm undecided. Is there an issue with the current naming?
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.
I feel that
Service1<Service2<Connection<Request>>>
seems much better thanService1<Service2<SendRequest<Request>>>
thats really it.