From daacff6b96445c04233fcfc300b887b0536c0558 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Thu, 5 Dec 2024 16:34:28 +0800 Subject: [PATCH] feat: refine remote registry operation in tman (#370) --- core/src/ten_manager/src/constants.rs | 7 +++ .../src/dev_server/addons/extensions.rs | 1 - core/src/ten_manager/src/registry/remote.rs | 60 ++++++++++++++----- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/core/src/ten_manager/src/constants.rs b/core/src/ten_manager/src/constants.rs index 3433754d2d..6bafabb476 100644 --- a/core/src/ten_manager/src/constants.rs +++ b/core/src/ten_manager/src/constants.rs @@ -37,3 +37,10 @@ pub const PROPERTY_JSON_FILENAME: &str = "property.json"; pub const INSTALL_PATHS_APP_PREFIX: &str = "@app"; pub const TEN_PACKAGE_FILE_EXTENSION: &str = "tpkg"; + +// Maximum number of retry attempts. +pub const REMOTE_REGISTRY_MAX_RETRIES: u32 = 30; +// Delay between retries in milliseconds. +pub const REMOTE_REGISTRY_RETRY_DELAY_MS: u64 = 500; +// Timeout duration for requests in seconds. +pub const REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS: u64 = 10; diff --git a/core/src/ten_manager/src/dev_server/addons/extensions.rs b/core/src/ten_manager/src/dev_server/addons/extensions.rs index e1eb792d62..f6800cf0c7 100644 --- a/core/src/ten_manager/src/dev_server/addons/extensions.rs +++ b/core/src/ten_manager/src/dev_server/addons/extensions.rs @@ -23,7 +23,6 @@ use crate::dev_server::{ DevServerState, }; - #[derive(Serialize, Deserialize, Debug, PartialEq)] struct DevServerExtensionAddon { #[serde(rename = "name")] diff --git a/core/src/ten_manager/src/registry/remote.rs b/core/src/ten_manager/src/registry/remote.rs index 27453ec07b..a76c787742 100644 --- a/core/src/ten_manager/src/registry/remote.rs +++ b/core/src/ten_manager/src/registry/remote.rs @@ -22,6 +22,10 @@ use ten_rust::pkg_info::{ }; use super::{FoundResult, SearchCriteria}; +use crate::constants::{ + REMOTE_REGISTRY_MAX_RETRIES, REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS, + REMOTE_REGISTRY_RETRY_DELAY_MS, +}; use crate::{ config::TmanConfig, error::TmanError, log::tman_verbose_println, registry::found_result::RegistryPackageData, @@ -90,8 +94,8 @@ async fn get_package_upload_info( // Add a new environments field, or perhaps directly upload the content of // manifest.json to the cloud store. - let max_retries = 3; - let retry_delay = Duration::from_millis(100); + let max_retries = REMOTE_REGISTRY_MAX_RETRIES; + let retry_delay = Duration::from_millis(REMOTE_REGISTRY_RETRY_DELAY_MS); retry_async(tman_config, max_retries, retry_delay, || { let base_url = base_url.to_string(); @@ -146,6 +150,9 @@ async fn get_package_upload_info( let response = client .post(base_url) .headers(headers) + .timeout(Duration::from_secs( + REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS, + )) .json(&payload) .send() .await @@ -184,8 +191,8 @@ async fn upload_package_to_remote( package_file_path: &str, url: &str, ) -> Result<()> { - let max_retries = 3; - let retry_delay = Duration::from_millis(100); + let max_retries = REMOTE_REGISTRY_MAX_RETRIES; + let retry_delay = Duration::from_millis(REMOTE_REGISTRY_RETRY_DELAY_MS); retry_async(tman_config, max_retries, retry_delay, || { let client = client.clone(); @@ -241,8 +248,8 @@ async fn ack_of_uploading( client: &reqwest::Client, resource_id: &str, ) -> Result<()> { - let max_retries = 3; - let retry_delay = Duration::from_millis(100); + let max_retries = REMOTE_REGISTRY_MAX_RETRIES; + let retry_delay = Duration::from_millis(REMOTE_REGISTRY_RETRY_DELAY_MS); retry_async(tman_config, max_retries, retry_delay, || { let base_url = base_url.to_string(); @@ -260,7 +267,14 @@ async fn ack_of_uploading( let body = json!({ "resourceId": resource_id }); - let response = client.patch(url).json(&body).send().await; + let response = client + .patch(url) + .timeout(Duration::from_secs( + REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS, + )) + .json(&body) + .send() + .await; match response { Ok(resp) => { @@ -331,8 +345,8 @@ pub async fn get_package<'a>( let temp_file = Arc::new(RwLock::new(temp_file)); - let max_retries = 3; - let retry_delay = Duration::from_millis(100); + let max_retries = REMOTE_REGISTRY_MAX_RETRIES; + let retry_delay = Duration::from_millis(REMOTE_REGISTRY_RETRY_DELAY_MS); let download_complete = Arc::new(RwLock::new(false)); @@ -365,6 +379,9 @@ pub async fn get_package<'a>( let response = client .get(&url) .headers(headers) + .timeout(Duration::from_secs( + REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS, + )) .send() .await .with_context(|| { @@ -464,8 +481,8 @@ pub async fn get_package_list( pkg_identity: &PkgIdentity, criteria: &SearchCriteria, ) -> Result> { - let max_retries = 3; - let retry_delay = Duration::from_millis(100); + let max_retries = REMOTE_REGISTRY_MAX_RETRIES; + let retry_delay = Duration::from_millis(REMOTE_REGISTRY_RETRY_DELAY_MS); retry_async(tman_config, max_retries, retry_delay, || { let base_url = base_url.to_string(); @@ -486,7 +503,13 @@ pub async fn get_package_list( .append_pair("pageSize", "10") .append_pair("page", ¤t_page.to_string()); - let response = client.get(url).send().await; + let response = client + .get(url) + .timeout(Duration::from_secs( + REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS, + )) + .send() + .await; let response = match response { Ok(response) => response, @@ -563,8 +586,8 @@ pub async fn delete_package( version: &Version, hash: &str, ) -> Result<()> { - let max_retries = 3; - let retry_delay = Duration::from_millis(100); + let max_retries = REMOTE_REGISTRY_MAX_RETRIES; + let retry_delay = Duration::from_millis(REMOTE_REGISTRY_RETRY_DELAY_MS); retry_async(tman_config, max_retries, retry_delay, || { let base_url = base_url.to_string(); @@ -619,7 +642,14 @@ pub async fn delete_package( } // Sending the DELETE request. - let response = client.delete(url).headers(headers).send().await; + let response = client + .delete(url) + .headers(headers) + .timeout(Duration::from_secs( + REMOTE_REGISTRY_REQUEST_TIMEOUT_SECS, + )) + .send() + .await; match response { Ok(resp) => {