Skip to content

Commit

Permalink
perf(oma-refresh): reuse reqwest::Clinet
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed May 23, 2024
1 parent 87c5a50 commit e43fbd2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
5 changes: 4 additions & 1 deletion oma-fetch/examples/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use oma_fetch::{
DownloadEntryBuilder, DownloadEvent, DownloadResult, DownloadSource, DownloadSourceType,
OmaFetcher,
};
use reqwest::ClientBuilder;
use tokio::io::AsyncWriteExt;

#[tokio::main]
Expand Down Expand Up @@ -50,7 +51,9 @@ async fn main() -> DownloadResult<()> {
// .dir(PathBuf::from("./oma-fetcher-test"))
// .build()?;

let fetcher = OmaFetcher::new(None, vec![file_1, file_2], None)?;
let client = ClientBuilder::new().user_agent("oma").build().unwrap();

let fetcher = OmaFetcher::new(&client, vec![file_1, file_2], None)?;

tokio::fs::create_dir_all("./oma-fetcher-test")
.await
Expand Down
21 changes: 7 additions & 14 deletions oma-fetch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use derive_builder::Builder;
use download::SingleDownloaderBuilder;
use futures::StreamExt;

use reqwest::{Client, ClientBuilder};
use reqwest::Client;

pub mod checksum;
mod download;
Expand Down Expand Up @@ -90,8 +90,8 @@ impl Ord for DownloadSourceType {
}
}

pub struct OmaFetcher {
client: Client,
pub struct OmaFetcher<'a> {
client: &'a Client,
download_list: Vec<DownloadEntry>,
limit_thread: usize,
retry_times: usize,
Expand Down Expand Up @@ -145,21 +145,14 @@ impl Summary {
}

/// OmaFetcher is a Download Manager
impl OmaFetcher {
impl<'a> OmaFetcher<'a> {
pub fn new(
client: Option<Client>,
client: &'a Client,
download_list: Vec<DownloadEntry>,
limit_thread: Option<usize>,
) -> DownloadResult<Self> {
let client = client.unwrap_or(
ClientBuilder::new()
.user_agent("oma")
.build()
.map_err(DownloadError::ReqwestError)?,
);

) -> DownloadResult<OmaFetcher<'a>> {
Ok(Self {
client,
client: &client,
download_list,
limit_thread: limit_thread.unwrap_or(4),
retry_times: 3,
Expand Down
8 changes: 5 additions & 3 deletions oma-pm/src/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use oma_apt::{
};
use oma_console::console::{self, style};
use oma_fetch::{
DownloadEntryBuilder, DownloadEntryBuilderError, DownloadError, DownloadEvent, DownloadSource,
DownloadSourceType, OmaFetcher, Summary,
reqwest::ClientBuilder, DownloadEntryBuilder, DownloadEntryBuilderError, DownloadError,
DownloadEvent, DownloadSource, DownloadSourceType, OmaFetcher, Summary,
};
use oma_utils::{
dpkg::{is_hold, DpkgError},
Expand Down Expand Up @@ -718,7 +718,9 @@ impl OmaApt {
download_list.push(download_entry);
}

let downloader = OmaFetcher::new(None, download_list, network_thread)?;
let client = ClientBuilder::new().user_agent("oma").build().unwrap();

let downloader = OmaFetcher::new(&client, download_list, network_thread)?;

let res = downloader
.start_download(|count, event| callback(count, event, Some(total_size)))
Expand Down
12 changes: 6 additions & 6 deletions oma-refresh/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use futures::{future::join, FutureExt, StreamExt};
use oma_apt_sources_lists::{SourceEntry, SourceError};
use oma_fetch::{
checksum::ChecksumError,
reqwest::{self, ClientBuilder},
reqwest::{self, Client, ClientBuilder},
DownloadEntry, DownloadEntryBuilder, DownloadEntryBuilderError, DownloadEvent, DownloadResult,
DownloadSource, DownloadSourceType, OmaFetcher, Summary,
};
Expand Down Expand Up @@ -241,12 +241,12 @@ impl OmaRefresh {
let client = ClientBuilder::new().user_agent("oma").build()?;

let is_inrelease_map = self
.is_inrelease_map(&sourcelist, client, &m, &callback)
.get_is_inrelease_map(&sourcelist, &client, &m, &callback)
.await?;

let tasks = self.collect_download_release_tasks(&sourcelist, &m, is_inrelease_map)?;

let release_results = OmaFetcher::new(None, tasks, Some(self.limit))?
let release_results = OmaFetcher::new(&client, tasks, Some(self.limit))?
.start_download(|c, event| callback(c, RefreshEvent::from(event), None))
.await;

Expand All @@ -258,7 +258,7 @@ impl OmaRefresh {
.collect_all_release_entry(all_inrelease, &sourcelist, &m)
.await?;

let res = OmaFetcher::new(None, tasks, Some(self.limit))?
let res = OmaFetcher::new(&client, tasks, Some(self.limit))?
.start_download(|count, event| callback(count, RefreshEvent::from(event), Some(total)))
.await;

Expand All @@ -267,10 +267,10 @@ impl OmaRefresh {
Ok(())
}

async fn is_inrelease_map<F>(
async fn get_is_inrelease_map<F>(
&self,
sourcelist: &[OmaSourceEntry],
client: reqwest::Client,
client: &Client,
m: &Option<HashMap<String, MirrorMapItem>>,
callback: &F,
) -> Result<HashMap<usize, bool>>
Expand Down

0 comments on commit e43fbd2

Please sign in to comment.