Эх сурвалжийг харах

refactor(updater): unset request timeout, add builder setter (#3847)

Lucas Fernandes Nogueira 3 жил өмнө
parent
commit
0ecfad5924

+ 5 - 0
.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 - 0
.changes/updater-timeout.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+The updater default timeout is now unset, and the `UpdateBuilder` has a `timeout` setter.

+ 11 - 11
core/tauri/src/api/http.rs

@@ -19,8 +19,8 @@ use std::{collections::HashMap, path::PathBuf, time::Duration};
 pub struct ClientBuilder {
 pub struct ClientBuilder {
   /// Max number of redirections to follow.
   /// Max number of redirections to follow.
   pub max_redirections: Option<usize>,
   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 {
 impl ClientBuilder {
@@ -36,10 +36,10 @@ impl ClientBuilder {
     self
     self
   }
   }
 
 
-  /// Sets the connection timeout in seconds.
+  /// Sets the connection timeout.
   #[must_use]
   #[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
     self
   }
   }
 
 
@@ -59,7 +59,7 @@ impl ClientBuilder {
     }
     }
 
 
     if let Some(connect_timeout) = self.connect_timeout {
     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()?;
     let client = client_builder.build()?;
@@ -116,7 +116,7 @@ impl Client {
     }
     }
 
 
     if let Some(timeout) = request.timeout {
     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 {
     let response = if let Some(body) = request.body {
@@ -163,7 +163,7 @@ impl Client {
     }
     }
 
 
     if let Some(timeout) = request.timeout {
     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 {
     if let Some(body) = request.body {
@@ -289,7 +289,7 @@ pub struct HttpRequestBuilder {
   /// The request body
   /// The request body
   pub body: Option<Body>,
   pub body: Option<Body>,
   /// Timeout for the whole request
   /// Timeout for the whole request
-  pub timeout: Option<u64>,
+  pub timeout: Option<Duration>,
   /// The response type (defaults to Json)
   /// The response type (defaults to Json)
   pub response_type: Option<ResponseType>,
   pub response_type: Option<ResponseType>,
 }
 }
@@ -331,8 +331,8 @@ impl HttpRequestBuilder {
 
 
   /// Sets the general request timeout.
   /// Sets the general request timeout.
   #[must_use]
   #[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
     self
   }
   }
 
 

+ 22 - 13
core/tauri/src/updater/core.rs

@@ -25,6 +25,7 @@ use std::{
   io::{Cursor, Read},
   io::{Cursor, Read},
   path::{Path, PathBuf},
   path::{Path, PathBuf},
   str::from_utf8,
   str::from_utf8,
+  time::Duration,
 };
 };
 
 
 #[cfg(feature = "updater")]
 #[cfg(feature = "updater")]
@@ -210,6 +211,7 @@ pub struct UpdateBuilder<R: Runtime> {
   /// The current executable path. Default is automatically extracted.
   /// The current executable path. Default is automatically extracted.
   pub executable_path: Option<PathBuf>,
   pub executable_path: Option<PathBuf>,
   should_install: Option<Box<dyn FnOnce(&str, &str) -> bool + Send>>,
   should_install: Option<Box<dyn FnOnce(&str, &str) -> bool + Send>>,
+  timeout: Option<Duration>,
 }
 }
 
 
 impl<R: Runtime> fmt::Debug for UpdateBuilder<R> {
 impl<R: Runtime> fmt::Debug for UpdateBuilder<R> {
@@ -220,6 +222,7 @@ impl<R: Runtime> fmt::Debug for UpdateBuilder<R> {
       .field("urls", &self.urls)
       .field("urls", &self.urls)
       .field("target", &self.target)
       .field("target", &self.target)
       .field("executable_path", &self.executable_path)
       .field("executable_path", &self.executable_path)
+      .field("timeout", &self.timeout)
       .finish()
       .finish()
   }
   }
 }
 }
@@ -234,6 +237,7 @@ impl<R: Runtime> UpdateBuilder<R> {
       executable_path: None,
       executable_path: None,
       current_version: env!("CARGO_PKG_VERSION").into(),
       current_version: env!("CARGO_PKG_VERSION").into(),
       should_install: None,
       should_install: None,
+      timeout: None,
     }
     }
   }
   }
 
 
@@ -286,6 +290,11 @@ impl<R: Runtime> UpdateBuilder<R> {
     self
     self
   }
   }
 
 
+  pub fn timeout(mut self, timeout: Duration) -> Self {
+    self.timeout.replace(timeout);
+    self
+  }
+
   pub async fn build(mut self) -> Result<Update<R>> {
   pub async fn build(mut self) -> Result<Update<R>> {
     let mut remote_release: Option<RemoteRelease> = None;
     let mut remote_release: Option<RemoteRelease> = None;
 
 
@@ -346,15 +355,11 @@ impl<R: Runtime> UpdateBuilder<R> {
       let mut headers = HashMap::new();
       let mut headers = HashMap::new();
       headers.insert("Accept".into(), "application/json".into());
       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
       // If we got a success, we stop the loop
       // and we set our remote_release variable
       // and we set our remote_release variable
@@ -417,6 +422,7 @@ impl<R: Runtime> UpdateBuilder<R> {
       signature: final_release.signature,
       signature: final_release.signature,
       #[cfg(target_os = "windows")]
       #[cfg(target_os = "windows")]
       with_elevated_task: final_release.with_elevated_task,
       with_elevated_task: final_release.with_elevated_task,
+      timeout: self.timeout,
     })
     })
   }
   }
 }
 }
@@ -452,6 +458,8 @@ pub struct Update<R: Runtime> {
   /// Optional: Windows only try to use elevated task
   /// Optional: Windows only try to use elevated task
   /// Default to false
   /// Default to false
   with_elevated_task: bool,
   with_elevated_task: bool,
+  /// Request timeout
+  timeout: Option<Duration>,
 }
 }
 
 
 impl<R: Runtime> Clone for Update<R> {
 impl<R: Runtime> Clone for Update<R> {
@@ -469,6 +477,7 @@ impl<R: Runtime> Clone for Update<R> {
       signature: self.signature.clone(),
       signature: self.signature.clone(),
       #[cfg(target_os = "windows")]
       #[cfg(target_os = "windows")]
       with_elevated_task: self.with_elevated_task,
       with_elevated_task: self.with_elevated_task,
+      timeout: self.timeout,
     }
     }
   }
   }
 }
 }
@@ -499,10 +508,10 @@ impl<R: Runtime> Update<R> {
 
 
     let client = ClientBuilder::new().build()?;
     let client = ClientBuilder::new().build()?;
     // Create our request
     // 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?;
     let response = client.send(req).await?;
 
 

+ 9 - 1
core/tauri/src/updater/mod.rs

@@ -34,7 +34,7 @@
 //!
 //!
 //! "active" must be a boolean. By default, it's set to false.
 //! "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.
 //! "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.
 //!
 //!
@@ -444,6 +444,8 @@
 mod core;
 mod core;
 mod error;
 mod error;
 
 
+use std::time::Duration;
+
 pub use self::error::Error;
 pub use self::error::Error;
 /// Alias for [`std::result::Result`] using our own [`Error`].
 /// Alias for [`std::result::Result`] using our own [`Error`].
 pub type Result<T> = std::result::Result<T, Error>;
 pub type Result<T> = std::result::Result<T, Error>;
@@ -547,6 +549,12 @@ impl<R: Runtime> UpdateBuilder<R> {
     self
     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.
   /// Check if an update is available.
   ///
   ///
   /// # Examples
   /// # Examples