Skip to content

Commit

Permalink
use multi thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-ius committed May 12, 2024
1 parent 6332e4f commit 6540cd3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions hfd-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "hfd-cli"
version = "0.2.0"
version = "0.2.1"
edition = "2021"

[dependencies]
clap = { version= "4.5.4", features=["derive"] }
reqwest = { version = "0.12.4", default-features = false, features = ["stream", "http2", "json", "rustls-tls"] }
tokio = { version = "1.37.0", default-features = false, features = ["rt", "fs"] }
tokio = { version = "1.37.0", default-features = false, features = ["rt-multi-thread", "fs"] }
serde_json = { version = "1.0.116" }
tokio-stream = "0.1.15"

Expand Down
16 changes: 8 additions & 8 deletions hfd-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ async fn download_chunk(
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let client= reqwest::Client::builder()
.default_headers(headers.clone())
.pool_idle_timeout(Duration::from_millis(50))
.pool_max_idle_per_host(2)
.timeout(Duration::from_secs(30))
.build()?;
.pool_idle_timeout(Duration::from_secs(10))
.http2_keep_alive_timeout(Duration::from_secs(30)).build()?;

let range = format!("bytes={s}-{e}");

let response = client.get(url).header(RANGE, range).send().await?;
Expand All @@ -112,8 +111,6 @@ async fn download(
path: PathBuf,
chunk_size: usize
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// let url = self.hf_url.path(&file);
// let path = self.root.join(&file);
let client = reqwest::Client::builder()
.default_headers(headers.clone())
.http2_keep_alive_timeout(Duration::from_secs(15)).build()?;
Expand All @@ -127,7 +124,10 @@ async fn download(
.and_then(|s| s.parse().ok())
.ok_or("Failed to parse size")?;

let _ = tokio::fs::File::create(&path).await?.set_len(length as u64).await?;
tokio::fs::File::create(&path)
.await?
.set_len(length as u64)
.await?;

let tasks: Vec<_> = (0..length)
.into_iter()
Expand Down Expand Up @@ -238,7 +238,7 @@ impl HfClient {
}

pub fn _rt(_url: &str, _token: Option<&str>, _dir: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
let hfc = HfClient::build(_url)?
.apply_token(_token)
.apply_root(_dir);
Expand Down

0 comments on commit 6540cd3

Please sign in to comment.