tower_hyper/client/
connection.rs

1use super::background::Handle;
2use super::ResponseFuture;
3use crate::body::{Body, LiftBody};
4use futures::Poll;
5use http::{Request, Response};
6use http_body::Body as HttpBody;
7use hyper::client::conn;
8use tower_service::Service;
9
10/// The connection provided from `hyper`
11///
12/// This provides an interface for `DirectService` that will
13/// drive the inner service via `poll_service` and can send
14/// requests via `call`.
15#[derive(Debug)]
16pub struct Connection<B>
17where
18    B: HttpBody,
19{
20    sender: conn::SendRequest<LiftBody<B>>,
21    handle: Handle,
22}
23
24impl<B> Connection<B>
25where
26    B: HttpBody,
27{
28    pub(super) fn new(sender: conn::SendRequest<LiftBody<B>>, handle: Handle) -> Self {
29        Connection { sender, handle }
30    }
31}
32
33impl<B> Service<Request<B>> for Connection<B>
34where
35    B: HttpBody + Send + 'static,
36    B::Data: Send,
37    B::Error: Into<crate::Error>,
38{
39    type Response = Response<Body>;
40    type Error = hyper::Error;
41    type Future = ResponseFuture<conn::ResponseFuture>;
42
43    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
44        if let Some(e) = self.handle.get_error() {
45            return Err(e);
46        }
47        self.sender.poll_ready()
48    }
49
50    fn call(&mut self, req: Request<B>) -> Self::Future {
51        let inner = self.sender.send_request(req.map(LiftBody::from));
52        ResponseFuture { inner }
53    }
54}