1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-01 05:10:06 +01:00
actix-net/actix-utils/src/keepalive.rs

122 lines
2.6 KiB
Rust
Raw Normal View History

2018-09-20 20:16:12 +02:00
use std::marker::PhantomData;
use std::time::{Duration, Instant};
2018-12-09 19:15:49 +01:00
use actix_service::{NewService, Service};
2018-09-20 20:16:12 +02:00
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use tokio_timer::Delay;
2018-10-29 23:48:56 +01:00
use super::time::{LowResTime, LowResTimeService};
2018-09-20 20:16:12 +02:00
use super::Never;
pub struct KeepAlive<R, E, F> {
f: F,
ka: Duration,
2018-10-29 23:48:56 +01:00
time: LowResTime,
2018-09-20 20:16:12 +02:00
_t: PhantomData<(R, E)>,
}
impl<R, E, F> KeepAlive<R, E, F>
where
F: Fn() -> E + Clone,
{
2018-10-29 23:48:56 +01:00
pub fn new(ka: Duration, time: LowResTime, f: F) -> Self {
2018-09-20 20:16:12 +02:00
KeepAlive {
f,
ka,
2018-10-29 23:48:56 +01:00
time,
2018-09-20 20:16:12 +02:00
_t: PhantomData,
}
}
}
impl<R, E, F> Clone for KeepAlive<R, E, F>
where
2018-11-30 03:56:15 +01:00
F: Clone,
2018-09-20 20:16:12 +02:00
{
fn clone(&self) -> Self {
KeepAlive {
f: self.f.clone(),
ka: self.ka,
2018-10-29 23:48:56 +01:00
time: self.time.clone(),
2018-09-20 20:16:12 +02:00
_t: PhantomData,
}
}
}
2018-11-30 03:56:15 +01:00
impl<R, E, F> NewService<R> for KeepAlive<R, E, F>
2018-09-20 20:16:12 +02:00
where
F: Fn() -> E + Clone,
{
type Response = R;
type Error = E;
type InitError = Never;
type Service = KeepAliveService<R, E, F>;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self) -> Self::Future {
ok(KeepAliveService::new(
self.ka,
2018-10-29 23:48:56 +01:00
self.time.timer(),
2018-09-20 20:16:12 +02:00
self.f.clone(),
))
}
}
pub struct KeepAliveService<R, E, F> {
f: F,
ka: Duration,
2018-10-29 23:48:56 +01:00
time: LowResTimeService,
2018-09-20 20:16:12 +02:00
delay: Delay,
expire: Instant,
_t: PhantomData<(R, E)>,
}
impl<R, E, F> KeepAliveService<R, E, F>
where
F: Fn() -> E,
{
2018-10-29 23:48:56 +01:00
pub fn new(ka: Duration, time: LowResTimeService, f: F) -> Self {
let expire = time.now() + ka;
2018-09-20 20:16:12 +02:00
KeepAliveService {
f,
ka,
2018-10-29 23:48:56 +01:00
time,
2018-09-20 20:16:12 +02:00
expire,
2018-10-29 23:48:56 +01:00
delay: Delay::new(expire),
2018-09-20 20:16:12 +02:00
_t: PhantomData,
}
}
}
2018-11-30 03:56:15 +01:00
impl<R, E, F> Service<R> for KeepAliveService<R, E, F>
2018-09-20 20:16:12 +02:00
where
F: Fn() -> E,
{
type Response = R;
type Error = E;
type Future = FutureResult<R, E>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
match self.delay.poll() {
Ok(Async::Ready(_)) => {
2018-10-29 23:48:56 +01:00
let now = self.time.now();
2018-09-20 20:16:12 +02:00
if self.expire <= now {
Err((self.f)())
} else {
self.delay.reset(self.expire);
2018-09-20 20:16:12 +02:00
let _ = self.delay.poll();
Ok(Async::Ready(()))
}
}
Ok(Async::NotReady) => Ok(Async::Ready(())),
Err(_) => panic!(),
}
}
2018-11-30 03:56:15 +01:00
fn call(&mut self, req: R) -> Self::Future {
2018-10-29 23:48:56 +01:00
self.expire = self.time.now() + self.ka;
2018-09-20 20:16:12 +02:00
ok(req)
}
}