Return an Error instead of panic if sync Client cannot startup runtime.

The timeout is also increased to 10 seconds from 5.

Closes #392
This commit is contained in:
Sean McArthur
2018-12-26 16:59:51 -08:00
parent c4985674aa
commit 4df232efad
2 changed files with 19 additions and 13 deletions

View File

@@ -462,14 +462,6 @@ impl ClientHandle {
}; };
let work = rx.for_each(move |(req, tx)| { let work = rx.for_each(move |(req, tx)| {
/*
let tx: oneshot::Sender<::Result<async_impl::Response>> = tx;
let task = client.execute(req)
.then(move |r| {
trace!("result received: {:?}", r);
tx.send(r).map_err(|_| ())
});
*/
let mut tx_opt: Option<oneshot::Sender<::Result<async_impl::Response>>> = Some(tx); let mut tx_opt: Option<oneshot::Sender<::Result<async_impl::Response>>> = Some(tx);
let mut res_fut = client.execute(req); let mut res_fut = client.execute(req);
@@ -508,10 +500,15 @@ impl ClientHandle {
.expect("runtime unexpected error"); .expect("runtime unexpected error");
})); }));
// Wait up to 5 seconds for the background thread to be spawned. // Wait some seconds for the background thread to be spawned.
// More than that and something bad is up! // More than that and something bad is up!
wait::timeout(spawn_rx, Some(Duration::from_secs(5))) match wait::timeout(spawn_rx, Some(Duration::from_secs(10))) {
.expect("runtime thread cancelled")?; Ok(Ok(())) => (),
Ok(Err(err)) => return Err(err),
Err(wait::Waited::Err(_/*mpsc::Canceled*/)) |
Err(wait::Waited::TimedOut) => return Err(::error::runtime_startup()),
}
let inner_handle = Arc::new(InnerClientHandle { let inner_handle = Arc::new(InnerClientHandle {
tx: Some(tx), tx: Some(tx),

View File

@@ -147,7 +147,8 @@ impl Error {
Kind::TooManyRedirects | Kind::TooManyRedirects |
Kind::RedirectLoop | Kind::RedirectLoop |
Kind::ClientError(_) | Kind::ClientError(_) |
Kind::ServerError(_) => None, Kind::ServerError(_) |
Kind::RuntimeStartupFailure => None,
} }
} }
@@ -250,6 +251,7 @@ impl fmt::Display for Error {
f.write_str("Server Error: ")?; f.write_str("Server Error: ")?;
fmt::Display::fmt(code, f) fmt::Display::fmt(code, f)
} }
Kind::RuntimeStartupFailure => f.write_str("Client runtime failed to start"),
} }
} }
} }
@@ -275,6 +277,7 @@ impl StdError for Error {
Kind::RedirectLoop => "Infinite redirect loop", Kind::RedirectLoop => "Infinite redirect loop",
Kind::ClientError(_) => "Client Error", Kind::ClientError(_) => "Client Error",
Kind::ServerError(_) => "Server Error", Kind::ServerError(_) => "Server Error",
Kind::RuntimeStartupFailure => "Client runtime failed to start",
} }
} }
@@ -298,7 +301,8 @@ impl StdError for Error {
Kind::TooManyRedirects | Kind::TooManyRedirects |
Kind::RedirectLoop | Kind::RedirectLoop |
Kind::ClientError(_) | Kind::ClientError(_) |
Kind::ServerError(_) => None, Kind::ServerError(_) |
Kind::RuntimeStartupFailure => None,
} }
} }
} }
@@ -323,6 +327,7 @@ pub(crate) enum Kind {
RedirectLoop, RedirectLoop,
ClientError(StatusCode), ClientError(StatusCode),
ServerError(StatusCode), ServerError(StatusCode),
RuntimeStartupFailure,
} }
@@ -489,6 +494,10 @@ pub(crate) fn url_bad_scheme(url: Url) -> Error {
Error::new(Kind::UrlBadScheme, Some(url)) Error::new(Kind::UrlBadScheme, Some(url))
} }
pub(crate) fn runtime_startup() -> Error {
Error::new(Kind::RuntimeStartupFailure, None)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;