2018-10-30 04:29:47 +01:00
|
|
|
//! Service that applies a timeout to requests.
|
2018-10-24 06:38:36 +02:00
|
|
|
//!
|
|
|
|
//! If the response does not complete within the specified timeout, the response
|
|
|
|
//! will be aborted.
|
|
|
|
use std::fmt;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2018-12-09 19:15:49 +01:00
|
|
|
use actix_service::{NewService, Service};
|
2018-12-06 23:04:42 +01:00
|
|
|
use futures::try_ready;
|
2018-10-24 06:38:36 +02:00
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
use tokio_timer::{clock, Delay};
|
|
|
|
|
|
|
|
/// Applies a timeout to requests.
|
|
|
|
#[derive(Debug)]
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct Timeout<T> {
|
2018-10-24 06:38:36 +02:00
|
|
|
inner: T,
|
|
|
|
timeout: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Timeout error
|
|
|
|
pub enum TimeoutError<E> {
|
|
|
|
/// Service error
|
|
|
|
Service(E),
|
|
|
|
/// Service call timeout
|
|
|
|
Timeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
TimeoutError::Service(e) => write!(f, "TimeoutError::Service({:?})", e),
|
|
|
|
TimeoutError::Timeout => write!(f, "TimeoutError::Timeout"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T> Timeout<T> {
|
|
|
|
pub fn new<Request>(timeout: Duration, inner: T) -> Self
|
|
|
|
where
|
|
|
|
T: NewService<Request> + Clone,
|
|
|
|
{
|
2018-10-24 06:38:36 +02:00
|
|
|
Timeout { inner, timeout }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 23:31:27 +01:00
|
|
|
impl<T> Clone for Timeout<T>
|
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Timeout {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
timeout: self.timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, Request> NewService<Request> for Timeout<T>
|
2018-10-24 06:38:36 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request> + Clone,
|
2018-10-24 06:38:36 +02:00
|
|
|
{
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = TimeoutError<T::Error>;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Service = TimeoutService<T::Service>;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = TimeoutFut<T, Request>;
|
2018-10-24 06:38:36 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
TimeoutFut {
|
|
|
|
fut: self.inner.new_service(),
|
2018-12-06 23:04:42 +01:00
|
|
|
timeout: self.timeout,
|
2018-10-24 06:38:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 04:29:47 +01:00
|
|
|
/// `Timeout` response future
|
|
|
|
#[derive(Debug)]
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct TimeoutFut<T: NewService<Request>, Request> {
|
2018-10-30 04:29:47 +01:00
|
|
|
fut: T::Future,
|
|
|
|
timeout: Duration,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, Request> Future for TimeoutFut<T, Request>
|
2018-10-24 06:38:36 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request>,
|
2018-10-24 06:38:36 +02:00
|
|
|
{
|
|
|
|
type Item = TimeoutService<T::Service>;
|
|
|
|
type Error = T::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let service = try_ready!(self.fut.poll());
|
|
|
|
Ok(Async::Ready(TimeoutService::new(self.timeout, service)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies a timeout to requests.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TimeoutService<T> {
|
|
|
|
inner: T,
|
|
|
|
timeout: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TimeoutService<T> {
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<Request>(timeout: Duration, inner: T) -> Self
|
|
|
|
where
|
|
|
|
T: Service<Request>,
|
|
|
|
{
|
2018-10-24 06:38:36 +02:00
|
|
|
TimeoutService { inner, timeout }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T: Clone> Clone for TimeoutService<T> {
|
2018-10-24 06:38:36 +02:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
TimeoutService {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
timeout: self.timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, Request> Service<Request> for TimeoutService<T>
|
2018-10-24 06:38:36 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: Service<Request>,
|
2018-10-24 06:38:36 +02:00
|
|
|
{
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = TimeoutError<T::Error>;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = TimeoutServiceResponse<T, Request>;
|
2018-10-24 06:38:36 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-12-06 23:04:42 +01:00
|
|
|
self.inner.poll_ready().map_err(TimeoutError::Service)
|
2018-10-24 06:38:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, request: Request) -> Self::Future {
|
2018-10-24 06:38:36 +02:00
|
|
|
TimeoutServiceResponse {
|
|
|
|
fut: self.inner.call(request),
|
|
|
|
sleep: Delay::new(clock::now() + self.timeout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `TimeoutService` response future
|
|
|
|
#[derive(Debug)]
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct TimeoutServiceResponse<T: Service<Request>, Request> {
|
2018-10-24 06:38:36 +02:00
|
|
|
fut: T::Future,
|
|
|
|
sleep: Delay,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, Request> Future for TimeoutServiceResponse<T, Request>
|
2018-10-24 06:38:36 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: Service<Request>,
|
2018-10-24 06:38:36 +02:00
|
|
|
{
|
|
|
|
type Item = T::Response;
|
|
|
|
type Error = TimeoutError<T::Error>;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
// First, try polling the future
|
|
|
|
match self.fut.poll() {
|
|
|
|
Ok(Async::Ready(v)) => return Ok(Async::Ready(v)),
|
|
|
|
Ok(Async::NotReady) => {}
|
|
|
|
Err(e) => return Err(TimeoutError::Service(e)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check the sleep
|
|
|
|
match self.sleep.poll() {
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(_)) => Err(TimeoutError::Timeout),
|
|
|
|
Err(_) => Err(TimeoutError::Timeout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|