use super::background::Handle;
use super::ResponseFuture;
use crate::body::{Body, LiftBody};
use futures::Poll;
use http::{Request, Response};
use http_body::Body as HttpBody;
use hyper::client::conn;
use tower_service::Service;
#[derive(Debug)]
pub struct Connection<B>
where
B: HttpBody,
{
sender: conn::SendRequest<LiftBody<B>>,
handle: Handle,
}
impl<B> Connection<B>
where
B: HttpBody,
{
pub(super) fn new(sender: conn::SendRequest<LiftBody<B>>, handle: Handle) -> Self {
Connection { sender, handle }
}
}
impl<B> Service<Request<B>> for Connection<B>
where
B: HttpBody + Send + 'static,
B::Data: Send,
B::Error: Into<crate::Error>,
{
type Response = Response<Body>;
type Error = hyper::Error;
type Future = ResponseFuture<conn::ResponseFuture>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
if let Some(e) = self.handle.get_error() {
return Err(e);
}
self.sender.poll_ready()
}
fn call(&mut self, req: Request<B>) -> Self::Future {
let inner = self.sender.send_request(req.map(LiftBody::from));
ResponseFuture { inner }
}
}