use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use redis_tower_core::{RedisError, RequestDeadline};
use tower_layer::Layer;
use tower_service::Service;
#[derive(Debug, Clone)]
pub struct CommandTimeoutLayer {
duration: Duration,
}
impl CommandTimeoutLayer {
pub fn new(duration: Duration) -> Self {
Self { duration }
}
#[must_use]
pub fn with_request_deadlines(self) -> RequestDeadlineTimeoutLayer {
RequestDeadlineTimeoutLayer {
duration: self.duration,
}
}
}
impl<S> Layer<S> for CommandTimeoutLayer {
type Service = CommandTimeoutService<S>;
fn layer(&self, inner: S) -> Self::Service {
CommandTimeoutService {
inner,
duration: self.duration,
}
}
}
#[derive(Debug, Clone)]
pub struct CommandTimeoutService<S> {
inner: S,
duration: Duration,
}
impl<S, Req> Service<Req> for CommandTimeoutService<S>
where
S: Service<Req, Error = RedisError>,
S::Response: Send + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = RedisError;
type Future = Pin<Box<dyn Future<Output = Result<S::Response, RedisError>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Req) -> Self::Future {
let duration = self.duration;
let fut = self.inner.call(req);
Box::pin(async move {
tokio::time::timeout(duration, fut)
.await
.map_err(|_elapsed| RedisError::CommandTimeout)?
})
}
}
#[derive(Debug, Clone)]
pub struct RequestDeadlineTimeoutLayer {
duration: Duration,
}
impl<S> Layer<S> for RequestDeadlineTimeoutLayer {
type Service = RequestDeadlineTimeoutService<S>;
fn layer(&self, inner: S) -> Self::Service {
RequestDeadlineTimeoutService {
inner,
duration: self.duration,
}
}
}
#[derive(Debug, Clone)]
pub struct RequestDeadlineTimeoutService<S> {
inner: S,
duration: Duration,
}
impl<S, Req> Service<Req> for RequestDeadlineTimeoutService<S>
where
S: Service<Req, Error = RedisError>,
Req: RequestDeadline,
S::Response: Send + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = RedisError;
type Future = Pin<Box<dyn Future<Output = Result<S::Response, RedisError>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Req) -> Self::Future {
let now = tokio::time::Instant::now();
let static_deadline = now + self.duration;
let deadline = req
.request_deadline()
.map_or(static_deadline, |request_deadline| {
request_deadline.min(static_deadline)
});
if deadline <= now {
return Box::pin(async { Err(RedisError::CommandTimeout) });
}
let fut = self.inner.call(req);
Box::pin(async move {
tokio::time::timeout_at(deadline, fut)
.await
.map_err(|_elapsed| RedisError::CommandTimeout)?
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use redis_tower_core::{Command, Frame, WithDeadline};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
#[derive(Clone)]
struct TestCommand;
impl Command for TestCommand {
type Response = ();
fn to_frame(&self) -> Frame {
Frame::Null
}
fn parse_response(&self, _frame: Frame) -> Result<Self::Response, RedisError> {
Ok(())
}
fn name(&self) -> &str {
"TEST"
}
}
struct SlowService {
delay: Duration,
calls: Arc<AtomicUsize>,
}
impl<R> Service<R> for SlowService {
type Response = ();
type Error = RedisError;
type Future = Pin<Box<dyn Future<Output = Result<(), RedisError>> + Send>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), RedisError>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, _: R) -> Self::Future {
self.calls.fetch_add(1, Ordering::Relaxed);
let d = self.delay;
Box::pin(async move {
tokio::time::sleep(d).await;
Ok(())
})
}
}
#[tokio::test]
async fn command_timeout_fires_when_slow() {
let calls = Arc::new(AtomicUsize::new(0));
let layer = CommandTimeoutLayer::new(Duration::from_millis(50));
let mut svc = layer.layer(SlowService {
delay: Duration::from_millis(200),
calls,
});
let result = svc.call(()).await;
assert!(matches!(result, Err(RedisError::CommandTimeout)));
}
#[tokio::test]
async fn command_timeout_passes_when_fast() {
let calls = Arc::new(AtomicUsize::new(0));
let layer = CommandTimeoutLayer::new(Duration::from_millis(200));
let mut svc = layer.layer(SlowService {
delay: Duration::from_millis(10),
calls,
});
let result = svc.call(()).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn request_deadline_shortens_static_timeout() {
let calls = Arc::new(AtomicUsize::new(0));
let mut svc = CommandTimeoutLayer::new(Duration::from_secs(1))
.with_request_deadlines()
.layer(SlowService {
delay: Duration::from_millis(200),
calls,
});
let command = WithDeadline::after(TestCommand, Duration::from_millis(30));
assert!(matches!(
svc.call(command).await,
Err(RedisError::CommandTimeout)
));
}
#[tokio::test]
async fn static_timeout_can_be_earlier_than_request_deadline() {
let calls = Arc::new(AtomicUsize::new(0));
let mut svc = CommandTimeoutLayer::new(Duration::from_millis(30))
.with_request_deadlines()
.layer(SlowService {
delay: Duration::from_millis(200),
calls,
});
let command = WithDeadline::after(TestCommand, Duration::from_secs(1));
assert!(matches!(
svc.call(command).await,
Err(RedisError::CommandTimeout)
));
}
#[tokio::test]
async fn absolute_deadline_is_not_reset_before_call() {
let calls = Arc::new(AtomicUsize::new(0));
let command = WithDeadline::after(TestCommand, Duration::from_millis(50));
tokio::time::sleep(Duration::from_millis(35)).await;
let mut svc = CommandTimeoutLayer::new(Duration::from_secs(1))
.with_request_deadlines()
.layer(SlowService {
delay: Duration::from_millis(35),
calls,
});
assert!(matches!(
svc.call(command).await,
Err(RedisError::CommandTimeout)
));
}
#[tokio::test]
async fn expired_request_never_reaches_inner_service() {
let calls = Arc::new(AtomicUsize::new(0));
let mut svc = CommandTimeoutLayer::new(Duration::from_secs(1))
.with_request_deadlines()
.layer(SlowService {
delay: Duration::ZERO,
calls: Arc::clone(&calls),
});
let command = WithDeadline::new(
TestCommand,
tokio::time::Instant::now() - Duration::from_millis(1),
);
assert!(matches!(
svc.call(command).await,
Err(RedisError::CommandTimeout)
));
assert_eq!(calls.load(Ordering::Relaxed), 0);
}
#[tokio::test]
async fn raw_frame_requests_keep_static_timeout_support() {
let calls = Arc::new(AtomicUsize::new(0));
let mut svc = CommandTimeoutLayer::new(Duration::from_millis(20))
.with_request_deadlines()
.layer(SlowService {
delay: Duration::from_millis(100),
calls,
});
assert!(matches!(
svc.call(Frame::Null).await,
Err(RedisError::CommandTimeout)
));
}
#[tokio::test]
async fn static_layer_remains_generic_over_requests_without_deadline_metadata() {
struct CustomRequest;
let calls = Arc::new(AtomicUsize::new(0));
let mut svc = CommandTimeoutLayer::new(Duration::from_millis(20)).layer(SlowService {
delay: Duration::from_millis(100),
calls,
});
assert!(matches!(
svc.call(CustomRequest).await,
Err(RedisError::CommandTimeout)
));
}
}