From 2d6c7193d355cb4fac73805e87aa201760dfc720 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Mon, 19 Aug 2024 17:43:04 +0800 Subject: [PATCH] refactor: Use global request client to share the connection pool Signed-off-by: Xuanwo --- src/common/storage/src/operator.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/common/storage/src/operator.rs b/src/common/storage/src/operator.rs index 807c79a36a50e..a0556dc297fdc 100644 --- a/src/common/storage/src/operator.rs +++ b/src/common/storage/src/operator.rs @@ -63,6 +63,12 @@ use crate::StorageConfig; static GLOBAL_HICKORY_RESOLVER: LazyLock> = LazyLock::new(|| Arc::new(HickoryResolver::default())); +static GLOBAL_HTTP_CLIENT: LazyLock = LazyLock::new(|| { + new_storage_http_client().unwrap_or_else(|err| { + panic!("http client must be created successfully, but failed for {err}") + }) +}); + /// init_operator will init an opendal operator based on storage config. pub fn init_operator(cfg: &StorageParams) -> Result { let op = match &cfg { @@ -162,7 +168,7 @@ pub fn init_azblob_operator(cfg: &StorageAzblobConfig) -> Result { // Credential .account_name(&cfg.account_name) .account_key(&cfg.account_key) - .http_client(new_storage_http_client()?); + .http_client(GLOBAL_HTTP_CLIENT.clone()); Ok(builder) } @@ -187,7 +193,7 @@ fn init_gcs_operator(cfg: &StorageGcsConfig) -> Result { .bucket(&cfg.bucket) .root(&cfg.root) .credential(&cfg.credential) - .http_client(new_storage_http_client()?); + .http_client(GLOBAL_HTTP_CLIENT.clone()); Ok(builder) } @@ -284,7 +290,7 @@ fn init_s3_operator(cfg: &StorageS3Config) -> Result { builder = builder.enable_virtual_host_style(); } - builder = builder.http_client(new_storage_http_client()?); + builder = builder.http_client(GLOBAL_HTTP_CLIENT.clone()); Ok(builder) } @@ -301,7 +307,7 @@ fn init_obs_operator(cfg: &StorageObsConfig) -> Result { // Credential .access_key_id(&cfg.access_key_id) .secret_access_key(&cfg.secret_access_key) - .http_client(new_storage_http_client()?); + .http_client(GLOBAL_HTTP_CLIENT.clone()); Ok(builder) } @@ -317,7 +323,7 @@ fn init_oss_operator(cfg: &StorageOssConfig) -> Result { .root(&cfg.root) .server_side_encryption(&cfg.server_side_encryption) .server_side_encryption_key_id(&cfg.server_side_encryption_key_id) - .http_client(new_storage_http_client()?); + .http_client(GLOBAL_HTTP_CLIENT.clone()); Ok(builder) } @@ -350,7 +356,7 @@ fn init_cos_operator(cfg: &StorageCosConfig) -> Result { .secret_key(&cfg.secret_key) .bucket(&cfg.bucket) .root(&cfg.root) - .http_client(new_storage_http_client()?); + .http_client(GLOBAL_HTTP_CLIENT.clone()); Ok(builder) }