From 02a8537556236437d905cefe8aa2c5d0a96f129a Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 23 Feb 2024 17:01:51 +0100 Subject: Replace with a single AWS HTTP client --- src/storage/garage.rs | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) (limited to 'src/storage') diff --git a/src/storage/garage.rs b/src/storage/garage.rs index 709e729..870854a 100644 --- a/src/storage/garage.rs +++ b/src/storage/garage.rs @@ -1,7 +1,38 @@ use crate::storage::*; use aws_sdk_s3::{self as s3, error::SdkError, operation::get_object::GetObjectError}; +use aws_smithy_runtime::client::http::hyper_014::HyperClientBuilder; +use aws_smithy_runtime_api::client::http::SharedHttpClient; +//use hyper_rustls::HttpsConnector; +//use hyper_util::client::legacy::connect::HttpConnector; + + use serde::Serialize; +pub struct GarageRoot { + aws_http: SharedHttpClient, +} + +impl GarageRoot { + pub fn new() -> Self { + /*let http = hyper_rustls::HttpsConnectorBuilder::new() + .https_or_http() + .with_native_roots() + .enable_http1() + .enable_http2() + .build();*/ + let aws_http = HyperClientBuilder::new().build_https(); + Self { aws_http } + } + + pub fn user(&self, conf: GarageConf) -> anyhow::Result> { + let mut unicity: Vec = vec![]; + unicity.extend_from_slice(file!().as_bytes()); + unicity.append(&mut rmp_serde::to_vec(&conf)?); + + Ok(Arc::new(GarageUser { conf, aws_http: self.aws_http.clone(), unicity })) + } +} + #[derive(Clone, Debug, Serialize)] pub struct GarageConf { pub region: String, @@ -12,23 +43,18 @@ pub struct GarageConf { pub bucket: String, } +//@FIXME we should get rid of this builder +//and allocate a S3 + K2V client only once per user +//(and using a shared HTTP client) #[derive(Clone, Debug)] -pub struct GarageBuilder { +pub struct GarageUser { conf: GarageConf, + aws_http: SharedHttpClient, unicity: Vec, } -impl GarageBuilder { - pub fn new(conf: GarageConf) -> anyhow::Result> { - let mut unicity: Vec = vec![]; - unicity.extend_from_slice(file!().as_bytes()); - unicity.append(&mut rmp_serde::to_vec(&conf)?); - Ok(Arc::new(Self { conf, unicity })) - } -} - #[async_trait] -impl IBuilder for GarageBuilder { +impl IBuilder for GarageUser { async fn build(&self) -> Result { let s3_creds = s3::config::Credentials::new( self.conf.aws_access_key_id.clone(), @@ -41,6 +67,7 @@ impl IBuilder for GarageBuilder { let sdk_config = aws_config::from_env() .region(aws_config::Region::new(self.conf.region.clone())) .credentials_provider(s3_creds) + .http_client(self.aws_http.clone()) .endpoint_url(self.conf.s3_endpoint.clone()) .load() .await; -- cgit v1.2.3 From 2a084df300dc30d40cf3599c1cb7a90132d5a6e8 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 23 Feb 2024 17:31:29 +0100 Subject: Also share HTTPClient for K2V --- src/storage/garage.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src/storage') diff --git a/src/storage/garage.rs b/src/storage/garage.rs index 870854a..a23bbb2 100644 --- a/src/storage/garage.rs +++ b/src/storage/garage.rs @@ -1,27 +1,29 @@ -use crate::storage::*; use aws_sdk_s3::{self as s3, error::SdkError, operation::get_object::GetObjectError}; use aws_smithy_runtime::client::http::hyper_014::HyperClientBuilder; use aws_smithy_runtime_api::client::http::SharedHttpClient; -//use hyper_rustls::HttpsConnector; -//use hyper_util::client::legacy::connect::HttpConnector; - - +use hyper_rustls::HttpsConnector; +use hyper_util::rt::TokioExecutor; +use hyper_util::client::legacy::{connect::HttpConnector, Client as HttpClient}; use serde::Serialize; +use crate::storage::*; + pub struct GarageRoot { + k2v_http: HttpClient, k2v_client::Body>, aws_http: SharedHttpClient, } impl GarageRoot { - pub fn new() -> Self { - /*let http = hyper_rustls::HttpsConnectorBuilder::new() - .https_or_http() - .with_native_roots() - .enable_http1() - .enable_http2() - .build();*/ + pub fn new() -> anyhow::Result { + let connector = hyper_rustls::HttpsConnectorBuilder::new() + .with_native_roots()? + .https_or_http() + .enable_http1() + .enable_http2() + .build(); + let k2v_http = HttpClient::builder(TokioExecutor::new()).build(connector); let aws_http = HyperClientBuilder::new().build_https(); - Self { aws_http } + Ok(Self { k2v_http, aws_http }) } pub fn user(&self, conf: GarageConf) -> anyhow::Result> { @@ -29,7 +31,12 @@ impl GarageRoot { unicity.extend_from_slice(file!().as_bytes()); unicity.append(&mut rmp_serde::to_vec(&conf)?); - Ok(Arc::new(GarageUser { conf, aws_http: self.aws_http.clone(), unicity })) + Ok(Arc::new(GarageUser { + conf, + aws_http: self.aws_http.clone(), + k2v_http: self.k2v_http.clone(), + unicity + })) } } @@ -50,6 +57,7 @@ pub struct GarageConf { pub struct GarageUser { conf: GarageConf, aws_http: SharedHttpClient, + k2v_http: HttpClient, k2v_client::Body>, unicity: Vec, } @@ -87,7 +95,7 @@ impl IBuilder for GarageUser { user_agent: None, }; - let k2v_client = match k2v_client::K2vClient::new(k2v_config) { + let k2v_client = match k2v_client::K2vClient::new_with_client(k2v_config, self.k2v_http.clone()) { Err(e) => { tracing::error!("unable to build k2v client: {}", e); return Err(StorageError::Internal); -- cgit v1.2.3 From 0b122582e8374b9de43c8f096534301e18105f0c Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 23 Feb 2024 18:28:04 +0100 Subject: fix code formatting --- src/storage/garage.rs | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'src/storage') diff --git a/src/storage/garage.rs b/src/storage/garage.rs index a23bbb2..7152764 100644 --- a/src/storage/garage.rs +++ b/src/storage/garage.rs @@ -2,8 +2,8 @@ use aws_sdk_s3::{self as s3, error::SdkError, operation::get_object::GetObjectEr use aws_smithy_runtime::client::http::hyper_014::HyperClientBuilder; use aws_smithy_runtime_api::client::http::SharedHttpClient; use hyper_rustls::HttpsConnector; -use hyper_util::rt::TokioExecutor; use hyper_util::client::legacy::{connect::HttpConnector, Client as HttpClient}; +use hyper_util::rt::TokioExecutor; use serde::Serialize; use crate::storage::*; @@ -15,13 +15,13 @@ pub struct GarageRoot { impl GarageRoot { pub fn new() -> anyhow::Result { - let connector = hyper_rustls::HttpsConnectorBuilder::new() - .with_native_roots()? - .https_or_http() - .enable_http1() - .enable_http2() - .build(); - let k2v_http = HttpClient::builder(TokioExecutor::new()).build(connector); + let connector = hyper_rustls::HttpsConnectorBuilder::new() + .with_native_roots()? + .https_or_http() + .enable_http1() + .enable_http2() + .build(); + let k2v_http = HttpClient::builder(TokioExecutor::new()).build(connector); let aws_http = HyperClientBuilder::new().build_https(); Ok(Self { k2v_http, aws_http }) } @@ -31,11 +31,11 @@ impl GarageRoot { unicity.extend_from_slice(file!().as_bytes()); unicity.append(&mut rmp_serde::to_vec(&conf)?); - Ok(Arc::new(GarageUser { - conf, - aws_http: self.aws_http.clone(), + Ok(Arc::new(GarageUser { + conf, + aws_http: self.aws_http.clone(), k2v_http: self.k2v_http.clone(), - unicity + unicity, })) } } @@ -95,13 +95,14 @@ impl IBuilder for GarageUser { user_agent: None, }; - let k2v_client = match k2v_client::K2vClient::new_with_client(k2v_config, self.k2v_http.clone()) { - Err(e) => { - tracing::error!("unable to build k2v client: {}", e); - return Err(StorageError::Internal); - } - Ok(v) => v, - }; + let k2v_client = + match k2v_client::K2vClient::new_with_client(k2v_config, self.k2v_http.clone()) { + Err(e) => { + tracing::error!("unable to build k2v client: {}", e); + return Err(StorageError::Internal); + } + Ok(v) => v, + }; Ok(Box::new(GarageStore { bucket: self.conf.bucket.clone(), -- cgit v1.2.3