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

123 lines
2.7 KiB
Rust
Raw Normal View History

2018-09-20 20:16:12 +02:00
use std::marker::PhantomData;
use std::time::{Duration, Instant};
use actix_service::{NewService, Service, Void};
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
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,
}
}
}
2019-02-22 21:44:37 +01:00
impl<R, E, F> NewService<()> for KeepAlive<R, E, F>
2018-09-20 20:16:12 +02:00
where
F: Fn() -> E + Clone,
{
type Request = R;
2018-09-20 20:16:12 +02:00
type Response = R;
type Error = E;
type InitError = Void;
2018-09-20 20:16:12 +02:00
type Service = KeepAliveService<R, E, F>;
type Future = FutureResult<Self::Service, Self::InitError>;
2019-02-22 21:44:37 +01:00
fn new_service(&self, _: &()) -> Self::Future {
2018-09-20 20:16:12 +02:00
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,
}
}
}
impl<R, E, F> Service for KeepAliveService<R, E, F>
2018-09-20 20:16:12 +02:00
where
F: Fn() -> E,
{
type Request = R;
2018-09-20 20:16:12 +02:00
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(())),
2019-02-04 20:04:10 +01:00
Err(_e) => panic!(),
2018-09-20 20:16:12 +02:00
}
}
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)
}
}