diff options
Diffstat (limited to 'tower-watchdog/src')
-rw-r--r-- | tower-watchdog/src/lib.rs | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/tower-watchdog/src/lib.rs b/tower-watchdog/src/lib.rs index 9a5c609..e0be313 100644 --- a/tower-watchdog/src/lib.rs +++ b/tower-watchdog/src/lib.rs @@ -27,22 +27,45 @@ impl<S> tower_layer::Layer<S> for WatchdogLayer { fn layer(&self, inner: S) -> Self::Service { Self::Service { pet: self.pet.clone(), - inner + inner, } } } pub struct WatchdogService<S> { pet: watchdog::Pet, - inner: S + inner: S, } -impl<S: tower_service::Service<Request> + Clone + 'static, Request: std::fmt::Debug + 'static> tower_service::Service<Request> for WatchdogService<S> { +impl<S: tower_service::Service<Request> + Clone + 'static, Request: std::fmt::Debug + 'static> + tower_service::Service<Request> for WatchdogService<S> +{ type Response = S::Response; type Error = S::Error; - type Future = std::pin::Pin<Box<futures::future::Then<std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), tokio::sync::mpsc::error::SendError<()>>> + Send>>, std::pin::Pin<Box<S::Future>>, Box<dyn FnOnce(Result<(), tokio::sync::mpsc::error::SendError<()>>) -> std::pin::Pin<Box<S::Future>>>>>>; + type Future = std::pin::Pin< + Box< + futures::future::Then< + std::pin::Pin< + Box< + dyn std::future::Future< + Output = Result<(), tokio::sync::mpsc::error::SendError<()>>, + > + Send, + >, + >, + std::pin::Pin<Box<S::Future>>, + Box< + dyn FnOnce( + Result<(), tokio::sync::mpsc::error::SendError<()>>, + ) -> std::pin::Pin<Box<S::Future>>, + >, + >, + >, + >; - fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> { + fn poll_ready( + &mut self, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll<Result<(), Self::Error>> { self.inner.poll_ready(cx) } @@ -57,7 +80,11 @@ impl<S: tower_service::Service<Request> + Clone + 'static, Request: std::fmt::De std::mem::swap(&mut self.inner, &mut inner); let pet = self.pet.clone(); - Box::pin(pet.pet_owned().boxed().then(Box::new(move |_| Box::pin(inner.call(request))))) + Box::pin( + pet.pet_owned() + .boxed() + .then(Box::new(move |_| Box::pin(inner.call(request)))), + ) } } @@ -84,7 +111,10 @@ mod tests { for i in 100..=1_000 { if i != 1000 { assert!(mock.poll_ready().is_ready()); - let request = Box::pin(tokio::time::sleep(std::time::Duration::from_millis(i)).then(|()| mock.call(()))); + let request = Box::pin( + tokio::time::sleep(std::time::Duration::from_millis(i)) + .then(|()| mock.call(())), + ); tokio::select! { _ = &mut watchdog_future => panic!("Watchdog called earlier than response!"), _ = request => {}, @@ -94,7 +124,10 @@ mod tests { // We use `+ 1` here, because the watchdog behavior is // subject to a data race if a request arrives in the // same tick. - let request = Box::pin(tokio::time::sleep(std::time::Duration::from_millis(i + 1)).then(|()| mock.call(()))); + let request = Box::pin( + tokio::time::sleep(std::time::Duration::from_millis(i + 1)) + .then(|()| mock.call(())), + ); tokio::select! { _ = &mut watchdog_future => { }, |