Skip to content

Commit 9616cca

Browse files
authored
refactor: Use the same http client across project (#364)
* refactor: Use the same http client across project Signed-off-by: Xuanwo <[email protected]> * Remove concurrency until we find better way Signed-off-by: Xuanwo <[email protected]> * Remove not needed actions Signed-off-by: Xuanwo <[email protected]>
1 parent f5fad2a commit 9616cca

File tree

7 files changed

+53
-59
lines changed

7 files changed

+53
-59
lines changed

.github/workflows/service_test_hdfs.yml

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,6 @@ concurrency:
77
cancel-in-progress: true
88

99
jobs:
10-
default:
11-
runs-on: ${{ matrix.os }}
12-
strategy:
13-
matrix:
14-
hdfs-version: [ "2.10.1", "3.2.3", "3.3.2" ]
15-
os:
16-
- ubuntu-latest
17-
steps:
18-
- uses: actions/checkout@v3
19-
20-
- name: Checkout python env
21-
uses: actions/setup-python@v4
22-
with:
23-
python-version: '3.8'
24-
- name: Checkout java env
25-
uses: actions/setup-java@v3
26-
with:
27-
distribution: temurin
28-
java-version: '11'
29-
- name: Setup-hdfs env
30-
uses: beyondstorage/setup-hdfs@master
31-
with:
32-
hdfs-version: ${{ matrix.hdfs-version }}
33-
34-
- name: Test
35-
shell: bash
36-
run: cargo test hdfs --features compress,retry,testing,services-hdfs -- --nocapture
37-
env:
38-
RUST_BACKTRACE: full
39-
RUST_LOG: debug
40-
OPENDAL_HDFS_TEST: on
41-
OPENDAL_HDFS_ROOT: /tmp/
42-
OPENDAL_HDFS_NAME_NODE: default
43-
LD_LIBRARY_PATH: ${{ env.JAVA_HOME }}/lib/server:${{ env.LD_LIBRARY_PATH }}
44-
45-
4610
hdfs:
4711
runs-on: ${{ matrix.os }}
4812
strategy:

.github/workflows/service_test_memory.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
matrix:
1414
os:
1515
- ubuntu-latest
16-
- macos-11
1716
steps:
1817
- uses: actions/checkout@v3
1918
- name: Test

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/operator.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,11 @@ impl Operator {
194194
#[derive(Clone, Debug)]
195195
pub struct BatchOperator {
196196
src: Operator,
197-
198-
concurrency: usize,
199197
}
200198

201199
impl BatchOperator {
202200
pub(crate) fn new(op: Operator) -> Self {
203-
BatchOperator {
204-
src: op,
205-
concurrency: 4,
206-
}
207-
}
208-
209-
/// Specify the concurrency of batch operators.
210-
pub fn with_concurrency(mut self, concurrency: usize) -> Self {
211-
self.concurrency = concurrency;
212-
self
201+
BatchOperator { src: op }
213202
}
214203

215204
/// Walk a dir in top down way: list current dir first and than list nested dir.
@@ -244,7 +233,7 @@ impl BatchOperator {
244233
}
245234

246235
let obs = self.walk_bottom_up(path)?;
247-
obs.try_for_each_concurrent(self.concurrency, |v| async move {
236+
obs.try_for_each(|v| async move {
248237
debug!("deleting {}", v.path());
249238
v.into_object().delete().await
250239
})

src/services/azblob/backend.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use crate::error::BackendError;
4949
use crate::error::ObjectError;
5050
use crate::io_util::new_http_channel;
5151
use crate::io_util::HttpBodyWriter;
52+
use crate::io_util::HttpClient;
5253
use crate::object::ObjectMetadata;
5354
use crate::ops::BytesRange;
5455
use crate::ops::OpCreate;
@@ -202,7 +203,7 @@ impl Builder {
202203
("endpoint".to_string(), endpoint.to_string()),
203204
]);
204205

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

207208
let mut signer_builder = Signer::builder();
208209
if let (Some(name), Some(key)) = (&self.account_name, &self.account_key) {
@@ -228,7 +229,7 @@ impl Builder {
228229
#[derive(Debug, Clone)]
229230
pub struct Backend {
230231
container: String,
231-
client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
232+
client: HttpClient,
232233
root: String, // root will be "/" or /abc/
233234
endpoint: String,
234235
signer: Arc<Signer>,

src/services/s3/backend.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::error::BackendError;
5050
use crate::error::ObjectError;
5151
use crate::io_util::new_http_channel;
5252
use crate::io_util::HttpBodyWriter;
53+
use crate::io_util::HttpClient;
5354
use crate::ops::BytesRange;
5455
use crate::ops::OpCreate;
5556
use crate::ops::OpDelete;
@@ -415,10 +416,7 @@ impl Builder {
415416
// Read RFC-0057: Auto Region for detailed behavior.
416417
async fn detect_region(
417418
&self,
418-
client: &hyper::Client<
419-
hyper_tls::HttpsConnector<hyper::client::HttpConnector>,
420-
hyper::Body,
421-
>,
419+
client: &HttpClient,
422420
bucket: &str,
423421
context: &HashMap<String, String>,
424422
) -> Result<(String, String)> {
@@ -628,7 +626,7 @@ impl Builder {
628626
})?),
629627
};
630628

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

633631
let (endpoint, region) = self.detect_region(&client, bucket, &context).await?;
634632
context.insert("endpoint".to_string(), endpoint.clone());
@@ -680,7 +678,7 @@ pub struct Backend {
680678
bucket: String,
681679
endpoint: String,
682680
signer: Arc<Signer>,
683-
client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
681+
client: HttpClient,
684682
// root will be "/" or "/abc/"
685683
root: String,
686684

@@ -1295,7 +1293,7 @@ mod tests {
12951293

12961294
#[tokio::test]
12971295
async fn test_detect_region() {
1298-
let client = hyper::Client::builder().build(hyper_tls::HttpsConnector::new());
1296+
let client = HttpClient::new();
12991297

13001298
let endpoint_cases = vec![
13011299
Some("s3.amazonaws.com"),

0 commit comments

Comments
 (0)