Skip to content

Commit 1604027

Browse files
committed
refactor: Use the same http client across project
Signed-off-by: Xuanwo <[email protected]>
1 parent f5fad2a commit 1604027

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/io_util/http_client.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::ops::Deref;
16+
17+
/// HttpClient that used across opendal.
18+
///
19+
/// NOTE: we could change or support more underlying http backend.
20+
#[derive(Debug, Clone)]
21+
pub struct HttpClient(
22+
hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
23+
);
24+
25+
impl HttpClient {
26+
/// Create a new http client.
27+
pub fn new() -> Self {
28+
HttpClient(hyper::Client::builder().build(hyper_tls::HttpsConnector::new()))
29+
}
30+
}
31+
32+
/// Forward all function to http backend.
33+
impl Deref for HttpClient {
34+
type Target =
35+
hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>;
36+
37+
fn deref(&self) -> &Self::Target {
38+
&self.0
39+
}
40+
}

src/io_util/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ mod http_body;
4040
pub(crate) use http_body::new_http_channel;
4141
pub(crate) use http_body::HttpBodyWriter;
4242

43+
mod http_client;
44+
pub(crate) use http_client::HttpClient;
45+
4346
mod seekable_reader;
4447
pub use seekable_reader::seekable_read;
4548
pub use seekable_reader::SeekableReader;

src/services/azblob/backend.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ use crate::accessor::AccessorMetadata;
4747
use crate::error::other;
4848
use crate::error::BackendError;
4949
use crate::error::ObjectError;
50-
use crate::io_util::new_http_channel;
5150
use crate::io_util::HttpBodyWriter;
51+
use crate::io_util::{new_http_channel, HttpClient};
5252
use crate::object::ObjectMetadata;
5353
use crate::ops::BytesRange;
5454
use crate::ops::OpCreate;
@@ -202,7 +202,7 @@ impl Builder {
202202
("endpoint".to_string(), endpoint.to_string()),
203203
]);
204204

205-
let client = hyper::Client::builder().build(hyper_tls::HttpsConnector::new());
205+
let client = HttpClient::new();
206206

207207
let mut signer_builder = Signer::builder();
208208
if let (Some(name), Some(key)) = (&self.account_name, &self.account_key) {
@@ -228,7 +228,7 @@ impl Builder {
228228
#[derive(Debug, Clone)]
229229
pub struct Backend {
230230
container: String,
231-
client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
231+
client: HttpClient,
232232
root: String, // root will be "/" or /abc/
233233
endpoint: String,
234234
signer: Arc<Signer>,

src/services/s3/backend.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ use super::object_stream::DirStream;
4848
use crate::error::other;
4949
use crate::error::BackendError;
5050
use crate::error::ObjectError;
51-
use crate::io_util::new_http_channel;
5251
use crate::io_util::HttpBodyWriter;
52+
use crate::io_util::{new_http_channel, HttpClient};
5353
use crate::ops::BytesRange;
5454
use crate::ops::OpCreate;
5555
use crate::ops::OpDelete;
@@ -415,10 +415,7 @@ impl Builder {
415415
// Read RFC-0057: Auto Region for detailed behavior.
416416
async fn detect_region(
417417
&self,
418-
client: &hyper::Client<
419-
hyper_tls::HttpsConnector<hyper::client::HttpConnector>,
420-
hyper::Body,
421-
>,
418+
client: &HttpClient,
422419
bucket: &str,
423420
context: &HashMap<String, String>,
424421
) -> Result<(String, String)> {
@@ -628,7 +625,7 @@ impl Builder {
628625
})?),
629626
};
630627

631-
let client = hyper::Client::builder().build(hyper_tls::HttpsConnector::new());
628+
let client = HttpClient::new();
632629

633630
let (endpoint, region) = self.detect_region(&client, bucket, &context).await?;
634631
context.insert("endpoint".to_string(), endpoint.clone());
@@ -680,7 +677,7 @@ pub struct Backend {
680677
bucket: String,
681678
endpoint: String,
682679
signer: Arc<Signer>,
683-
client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
680+
client: HttpClient,
684681
// root will be "/" or "/abc/"
685682
root: String,
686683

@@ -1295,7 +1292,7 @@ mod tests {
12951292

12961293
#[tokio::test]
12971294
async fn test_detect_region() {
1298-
let client = hyper::Client::builder().build(hyper_tls::HttpsConnector::new());
1295+
let client = HttpClient::new();
12991296

13001297
let endpoint_cases = vec![
13011298
Some("s3.amazonaws.com"),

0 commit comments

Comments
 (0)