Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client]: add timeouts to all request kinds + default timeout #367

Closed
wants to merge 7 commits into from

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Jun 4, 2021

Add default timeout on the all the clients requests to 60 seconds.

The PR itself is !DRY of in a few places.

@niklasad1 niklasad1 marked this pull request as ready for review June 4, 2021 10:58
/// Set request timeout (default is 60 seconds).
///
/// None - implies that no timeout is used.
pub fn request_timeout(mut self, timeout: Option<Duration>) -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little annoying with option here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon that's fine.

let fut = self.transport.send(serde_json::to_string(&notif).map_err(Error::ParseError)?);
match call_with_maybe_timeout(fut, self.request_timeout).await {
Ok(Ok(ok)) => Ok(ok),
Err(_) => Err(Error::RequestTimeout),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is tokio::time::Elapsed


self.id_guard.reclaim_request_id();
let json_value = match send_back_rx_out {
let json_value = match res {
Ok(Ok(v)) => v,
Ok(Err(err)) => return Err(err),
Err(_) => return Err(self.read_error_from_backend().await),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is oneshot::Cancled err in that case the background thread was terminated and we read the error message from the backend.

let mut sender = self.to_back.clone();
let fut = sender.send(FrontToBack::Notification(raw));

let res = if let Some(dur) = self.request_timeout {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this annoying of boiler plate but I found no good way of having a helper for Result<T>, and T when the receiver is either expects T or Result<T>

on option is to use generic as the http client is using with F: Fut -> Result<Fut::Output, Error> but that became super complicated with lots of nested results.

Copy link
Contributor

@maciejhirsz maciejhirsz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, small nits.

ws-client/src/client.rs Outdated Show resolved Hide resolved
@@ -25,17 +28,25 @@ impl HttpClientBuilder {
self
}

/// Set request timeout (default is 60 seconds).
///
/// None - implies that no timeout is used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// None - implies that no timeout is used.
/// `None` - no timeout is used.

It's explicit, not implied :)

let res = if let Some(dur) = self.request_timeout {
let timeout = crate::tokio::sleep(dur);
futures::pin_mut!(fut, timeout);
match futures::future::select(fut, timeout).await {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use select! macro for a bit less boilerplate, but it's fine either way.

Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

.await
.map_err(|e| Error::Transport(Box::new(e)))
let fut = self.transport.send(serde_json::to_string(&notif).map_err(Error::ParseError)?);
match call_with_maybe_timeout(fut, self.request_timeout).await {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can drop the maybe here; call_with_timeout is verbose enough! :)

@@ -46,16 +57,20 @@ pub struct HttpClient {
transport: HttpTransportClient,
/// Request ID that wraps around when overflowing.
request_id: AtomicU64,
/// Request timeout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Request timeout
/// Request timeout. Defaults to 60sec.

dvdplm added a commit that referenced this pull request Jul 6, 2021
Variant of #367

This PR takes a more opinionated stance than #367, where timeouts are optional. In this PR I suggest we make a all requests use a timeout and only let users choose the length.
@dvdplm dvdplm mentioned this pull request Jul 6, 2021
dvdplm added a commit that referenced this pull request Jul 8, 2021
* [clients]: use request timeout by-default

* add timeout for notif

* more feature flag mess

* rexport tokio types

* Update ws-client/src/client.rs

Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>

* Impose a timeout on all requests

Variant of #367

This PR takes a more opinionated stance than #367, where timeouts are optional. In this PR I suggest we make a all requests use a timeout and only let users choose the length.

* fmt

* Address review grumbles

* fmt

* Use tokio::select! for cleaner code

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>
Co-authored-by: Maciej Hirsz <hello@maciej.codes>
@dvdplm dvdplm closed this Jul 9, 2021
@dvdplm
Copy link
Contributor

dvdplm commented Jul 9, 2021

Closing in favour of #406

@niklasad1 niklasad1 deleted the na-client-default-timeout branch July 9, 2021 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants