2019-05-12 15:03:50 +02:00
|
|
|
use std::convert::Infallible;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2018-09-20 20:16:12 +02:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-12-05 11:40:24 +01:00
|
|
|
use std::time::Duration;
|
2018-09-20 20:16:12 +02:00
|
|
|
|
2019-12-05 11:40:24 +01:00
|
|
|
use actix_rt::time::{delay_until, Delay, Instant};
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
2020-03-11 20:35:29 +01:00
|
|
|
use futures_util::future::{ok, Ready};
|
2018-09-20 20:16:12 +02:00
|
|
|
|
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-11-14 13:38:24 +01:00
|
|
|
impl<R, E, F> ServiceFactory for KeepAlive<R, E, F>
|
2018-09-20 20:16:12 +02:00
|
|
|
where
|
|
|
|
F: Fn() -> E + Clone,
|
|
|
|
{
|
2019-03-09 16:27:35 +01:00
|
|
|
type Request = R;
|
2018-09-20 20:16:12 +02:00
|
|
|
type Response = R;
|
|
|
|
type Error = E;
|
2019-05-12 15:03:50 +02:00
|
|
|
type InitError = Infallible;
|
|
|
|
type Config = ();
|
2018-09-20 20:16:12 +02:00
|
|
|
type Service = KeepAliveService<R, E, F>;
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
2018-09-20 20:16:12 +02:00
|
|
|
|
2019-12-02 16:27:48 +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 {
|
2019-12-05 11:40:24 +01:00
|
|
|
let expire = Instant::from_std(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,
|
2019-12-05 11:40:24 +01:00
|
|
|
delay: delay_until(expire),
|
2018-09-20 20:16:12 +02:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:27:35 +01:00
|
|
|
impl<R, E, F> Service for KeepAliveService<R, E, F>
|
2018-09-20 20:16:12 +02:00
|
|
|
where
|
|
|
|
F: Fn() -> E,
|
|
|
|
{
|
2019-03-09 16:27:35 +01:00
|
|
|
type Request = R;
|
2018-09-20 20:16:12 +02:00
|
|
|
type Response = R;
|
|
|
|
type Error = E;
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<R, E>>;
|
2018-09-20 20:16:12 +02:00
|
|
|
|
2019-12-02 17:30:09 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 13:38:24 +01:00
|
|
|
match Pin::new(&mut self.delay).poll(cx) {
|
|
|
|
Poll::Ready(_) => {
|
2019-12-05 11:40:24 +01:00
|
|
|
let now = Instant::from_std(self.time.now());
|
2018-09-20 20:16:12 +02:00
|
|
|
if self.expire <= now {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Err((self.f)()))
|
2018-09-20 20:16:12 +02:00
|
|
|
} else {
|
2018-11-08 06:20:50 +01:00
|
|
|
self.delay.reset(self.expire);
|
2019-11-14 13:38:24 +01:00
|
|
|
let _ = Pin::new(&mut self.delay).poll(cx);
|
|
|
|
Poll::Ready(Ok(()))
|
2018-09-20 20:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => Poll::Ready(Ok(())),
|
2018-09-20 20:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: R) -> Self::Future {
|
2019-12-05 11:40:24 +01:00
|
|
|
self.expire = Instant::from_std(self.time.now() + self.ka);
|
2018-09-20 20:16:12 +02:00
|
|
|
ok(req)
|
|
|
|
}
|
|
|
|
}
|