route dispatch with timeout

legacy
Avril 5 years ago
parent ee3cdbf8bb
commit d06dee54eb
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -20,7 +20,7 @@ impl<T> OpaqueDebug<T>
} }
/// Consume into the value /// Consume into the value
#[inline] pub const fn into_inner(self) -> T #[inline] pub fn into_inner(self) -> T
{ {
self.0 self.0
} }

@ -10,10 +10,12 @@ use std::{
}; };
use tokio::{ use tokio::{
sync::{ sync::{
mpsc, mpsc::{
broadcast, self,
notify, error::SendTimeoutError,
},
}, },
time,
}; };
use futures::{ use futures::{
future::{ future::{
@ -96,20 +98,9 @@ impl Router
/// ///
/// # Returns /// # Returns
/// When one or more dispatchers match but faile, `Err` is returned. Inside the `Err` tuple is the amount of successful dispatches, and also a vector containing the indecies of the failed hook sends. /// When one or more dispatchers match but faile, `Err` is returned. Inside the `Err` tuple is the amount of successful dispatches, and also a vector containing the indecies of the failed hook sends.
pub async fn dispatch(&mut self, method: &Method, uri: impl AsRef<str>, timeout: impl Future) -> Result<usize, (usize, Vec<Index>)> pub async fn dispatch(&mut self, method: &Method, uri: impl AsRef<str>, timeout: Option<time::Duration>) -> Result<usize, (usize, Vec<Index>)>
{ {
let string = uri.as_ref(); let string = uri.as_ref();
let (tcx, trx) = broadcast::channel(1);
tokio::pin!(timeout);
let begin_to = tokio::sync::Barrier::new(self.routes.len()+1);
let timeout = async {
timeout.await;
begin_to.wait().await;
tcx.send(());
};
let mut timeouts = iter::once(trx)
.chain(iter::repeat_with(|| tcx.subscribe()));
let output = async {
let mut success=0usize; let mut success=0usize;
let vec: Vec<_> = let vec: Vec<_> =
future::join_all(self.routes.iter_mut() future::join_all(self.routes.iter_mut()
@ -119,25 +110,27 @@ impl Router
_ => { _ => {
if route.is_match(string) { if route.is_match(string) {
trace!("{:?} @{}: -> {}",i, route.as_string(), string); trace!("{:?} @{}: -> {}",i, route.as_string(), string);
let mut timeout = timeouts.next().unwrap(); let timeout = timeout.clone();
Some(async move { macro_rules! send {
match tokio::select!{ () => {
_ = timeout.recv() => { match timeout {
None None => sender.send(string.to_owned()).await
.map_err(|e| SendTimeoutError::Closed(e.0)),
Some(time) => sender.send_timeout(string.to_owned(), time).await
} }
s = sender.send(string.to_owned()) => {
Some(s)
} }
} { };
Some(Err(er)) => { Some(async move {
warn!("{:?}: Dispatch failed on hooked route for {}", i, er.0); match send!() {
Err(SendTimeoutError::Closed(er)) => {
error!("{:?}: Dispatch failed on hooked route for {}", i, er);
Err(i) Err(i)
}, },
Some(_) => Ok(()), Err(SendTimeoutError::Timeout(er)) => {
None => { warn!("{:?}: Dispatch timed out on hooked route for {}", i, er);
warn!("{:?}: Dispatch timed out on hooked route", i);
Err(i) Err(i)
}, },
_ => Ok(()),
} }
}) })
} else { } else {
@ -152,10 +145,6 @@ impl Router
} }
res.err() res.err()
}).collect(); }).collect();
(success, vec)
};
tokio::pin!(output);
let (_, (success, vec)) = future::join(timeout, output).await;
if vec.len() > 0 { if vec.len() > 0 {
Err((success, vec)) Err((success, vec))
} else { } else {

Loading…
Cancel
Save