use std::future::Future;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use pin_project_lite::pin_project;
use tokio::time::Sleep;
use super::error::Elapsed;
pin_project! {
#[derive(Debug)]
pub(crate) struct ResponseFuture<T> {
#[pin]
response: T,
#[pin]
sleep: Pin<Box<Sleep>>,
}
}
impl<T> ResponseFuture<T> {
pub(crate) fn new(response: T, sleep: Pin<Box<Sleep>>) -> Self {
ResponseFuture { response, sleep }
}
}
impl<F, T, E> Future for ResponseFuture<F>
where
F: Future<Output = Result<T, E>>,
E: Into<tower::BoxError>,
{
type Output = Result<T, tower::BoxError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
match this.response.poll(cx) {
Poll::Ready(v) => return Poll::Ready(v.map_err(Into::into)),
Poll::Pending => {}
}
match Pin::new(&mut this.sleep).poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(_) => {
u64_counter!(
"apollo_router_timeout",
"Number of timed out client requests",
1
);
Poll::Ready(Err(Elapsed::new().into()))
}
}
}
}