Skip to content

Commit

Permalink
refactor(updater): unset request timeout, add builder setter (#3847)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 2, 2022
1 parent f67ae6b commit 0ecfad5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changes/http-timeout-refactor.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change:** The `api::http` timeouts are now represented as `std::time::Duration` instead of a `u64`.
5 changes: 5 additions & 0 deletions .changes/updater-timeout.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

The updater default timeout is now unset, and the `UpdateBuilder` has a `timeout` setter.
22 changes: 11 additions & 11 deletions core/tauri/src/api/http.rs
Expand Up @@ -19,8 +19,8 @@ use std::{collections::HashMap, path::PathBuf, time::Duration};
pub struct ClientBuilder {
/// Max number of redirections to follow.
pub max_redirections: Option<usize>,
/// Connect timeout in seconds for the request.
pub connect_timeout: Option<u64>,
/// Connect timeout for the request.
pub connect_timeout: Option<Duration>,
}

impl ClientBuilder {
Expand All @@ -36,10 +36,10 @@ impl ClientBuilder {
self
}

/// Sets the connection timeout in seconds.
/// Sets the connection timeout.
#[must_use]
pub fn connect_timeout(mut self, connect_timeout: u64) -> Self {
self.connect_timeout = Some(connect_timeout);
pub fn connect_timeout(mut self, connect_timeout: Duration) -> Self {
self.connect_timeout.replace(connect_timeout);
self
}

Expand All @@ -59,7 +59,7 @@ impl ClientBuilder {
}

if let Some(connect_timeout) = self.connect_timeout {
client_builder = client_builder.connect_timeout(Duration::from_secs(connect_timeout));
client_builder = client_builder.connect_timeout(connect_timeout);
}

let client = client_builder.build()?;
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Client {
}

if let Some(timeout) = request.timeout {
request_builder = request_builder.timeout(Duration::from_secs(timeout));
request_builder = request_builder.timeout(timeout);
}

let response = if let Some(body) = request.body {
Expand Down Expand Up @@ -163,7 +163,7 @@ impl Client {
}

if let Some(timeout) = request.timeout {
request_builder = request_builder.timeout(Duration::from_secs(timeout));
request_builder = request_builder.timeout(timeout);
}

if let Some(body) = request.body {
Expand Down Expand Up @@ -289,7 +289,7 @@ pub struct HttpRequestBuilder {
/// The request body
pub body: Option<Body>,
/// Timeout for the whole request
pub timeout: Option<u64>,
pub timeout: Option<Duration>,
/// The response type (defaults to Json)
pub response_type: Option<ResponseType>,
}
Expand Down Expand Up @@ -331,8 +331,8 @@ impl HttpRequestBuilder {

/// Sets the general request timeout.
#[must_use]
pub fn timeout(mut self, timeout: u64) -> Self {
self.timeout = Some(timeout);
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout.replace(timeout);
self
}

Expand Down
35 changes: 22 additions & 13 deletions core/tauri/src/updater/core.rs
Expand Up @@ -25,6 +25,7 @@ use std::{
io::{Cursor, Read},
path::{Path, PathBuf},
str::from_utf8,
time::Duration,
};

#[cfg(feature = "updater")]
Expand Down Expand Up @@ -210,6 +211,7 @@ pub struct UpdateBuilder<R: Runtime> {
/// The current executable path. Default is automatically extracted.
pub executable_path: Option<PathBuf>,
should_install: Option<Box<dyn FnOnce(&str, &str) -> bool + Send>>,
timeout: Option<Duration>,
}

impl<R: Runtime> fmt::Debug for UpdateBuilder<R> {
Expand All @@ -220,6 +222,7 @@ impl<R: Runtime> fmt::Debug for UpdateBuilder<R> {
.field("urls", &self.urls)
.field("target", &self.target)
.field("executable_path", &self.executable_path)
.field("timeout", &self.timeout)
.finish()
}
}
Expand All @@ -234,6 +237,7 @@ impl<R: Runtime> UpdateBuilder<R> {
executable_path: None,
current_version: env!("CARGO_PKG_VERSION").into(),
should_install: None,
timeout: None,
}
}

Expand Down Expand Up @@ -286,6 +290,11 @@ impl<R: Runtime> UpdateBuilder<R> {
self
}

pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout.replace(timeout);
self
}

pub async fn build(mut self) -> Result<Update<R>> {
let mut remote_release: Option<RemoteRelease> = None;

Expand Down Expand Up @@ -346,15 +355,11 @@ impl<R: Runtime> UpdateBuilder<R> {
let mut headers = HashMap::new();
headers.insert("Accept".into(), "application/json".into());

let resp = ClientBuilder::new()
.build()?
.send(
HttpRequestBuilder::new("GET", &fixed_link)?
.headers(headers)
// wait 20sec for the firewall
.timeout(20),
)
.await;
let mut request = HttpRequestBuilder::new("GET", &fixed_link)?.headers(headers);
if let Some(timeout) = self.timeout {
request = request.timeout(timeout);
}
let resp = ClientBuilder::new().build()?.send(request).await;

// If we got a success, we stop the loop
// and we set our remote_release variable
Expand Down Expand Up @@ -417,6 +422,7 @@ impl<R: Runtime> UpdateBuilder<R> {
signature: final_release.signature,
#[cfg(target_os = "windows")]
with_elevated_task: final_release.with_elevated_task,
timeout: self.timeout,
})
}
}
Expand Down Expand Up @@ -452,6 +458,8 @@ pub struct Update<R: Runtime> {
/// Optional: Windows only try to use elevated task
/// Default to false
with_elevated_task: bool,
/// Request timeout
timeout: Option<Duration>,
}

impl<R: Runtime> Clone for Update<R> {
Expand All @@ -469,6 +477,7 @@ impl<R: Runtime> Clone for Update<R> {
signature: self.signature.clone(),
#[cfg(target_os = "windows")]
with_elevated_task: self.with_elevated_task,
timeout: self.timeout,
}
}
}
Expand Down Expand Up @@ -499,10 +508,10 @@ impl<R: Runtime> Update<R> {

let client = ClientBuilder::new().build()?;
// Create our request
let req = HttpRequestBuilder::new("GET", self.download_url.as_str())?
.headers(headers)
// wait 20sec for the firewall
.timeout(20);
let mut req = HttpRequestBuilder::new("GET", self.download_url.as_str())?.headers(headers);
if let Some(timeout) = self.timeout {
req = req.timeout(timeout);
}

let response = client.send(req).await?;

Expand Down
10 changes: 9 additions & 1 deletion core/tauri/src/updater/mod.rs
Expand Up @@ -34,7 +34,7 @@
//!
//! "active" must be a boolean. By default, it's set to false.
//!
//! "endpoints" must be an array. The string `{{target}}` and `{{current_version}}` are automatically replaced in the URL allowing you determine [server-side](#update-server-json-format) if an update is available. If multiple endpoints are specified, the updater will fallback if a server is not responding within the pre-defined timeout.
//! "endpoints" must be an array. The string `{{target}}` and `{{current_version}}` are automatically replaced in the URL allowing you determine [server-side](#update-server-json-format) if an update is available. If multiple endpoints are specified, the updater will fallback if a server is not responding within the optional timeout.
//!
//! "dialog" if present must be a boolean. By default, it's set to true. If enabled, [events](#events) are turned-off as the updater will handle everything. If you need the custom events, you MUST turn off the built-in dialog.
//!
Expand Down Expand Up @@ -444,6 +444,8 @@
mod core;
mod error;

use std::time::Duration;

pub use self::error::Error;
/// Alias for [`std::result::Result`] using our own [`Error`].
pub type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -547,6 +549,12 @@ impl<R: Runtime> UpdateBuilder<R> {
self
}

/// Sets the timeout for the requests to the updater endpoints.
pub fn timeout(mut self, timeout: Duration) -> Self {
self.inner = self.inner.timeout(timeout);
self
}

/// Check if an update is available.
///
/// # Examples
Expand Down

0 comments on commit 0ecfad5

Please sign in to comment.