Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 4357d40

Browse files
committed
use shared http client
1 parent f5c96b0 commit 4357d40

5 files changed

Lines changed: 64 additions & 11 deletions

File tree

src/bin/soar-dl/download_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl DownloadManager {
6969

7070
async fn handle_platform_download<P: ReleasePlatform, R, A>(
7171
&self,
72-
handler: &ReleaseHandler<P>,
72+
handler: &ReleaseHandler<'_, P>,
7373
project: &str,
7474
) -> Result<(), PlatformError>
7575
where

src/downloader.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use url::Url;
2121
use crate::{
2222
archive,
2323
error::DownloadError,
24+
http_client::SHARED_CLIENT,
2425
oci::{OciClient, OciLayer, OciManifest, Reference},
2526
utils::{
2627
build_absolute_path, extract_filename, extract_filename_from_url, is_elf, matches_pattern,
@@ -45,9 +46,8 @@ pub struct DownloadOptions {
4546
pub extract_dir: Option<String>,
4647
}
4748

48-
#[derive(Default)]
49-
pub struct Downloader {
50-
client: reqwest::Client,
49+
pub struct Downloader<'a> {
50+
client: &'a reqwest::Client,
5151
}
5252

5353
#[derive(Clone)]
@@ -63,7 +63,19 @@ pub struct OciDownloadOptions {
6363
pub exact_case: bool,
6464
}
6565

66-
impl Downloader {
66+
impl<'a> Default for Downloader<'a> {
67+
fn default() -> Self {
68+
Downloader {
69+
client: &SHARED_CLIENT,
70+
}
71+
}
72+
}
73+
74+
impl Downloader<'_> {
75+
pub fn client(&self) -> &reqwest::Client {
76+
self.client
77+
}
78+
6779
pub async fn download(&self, options: DownloadOptions) -> Result<String, DownloadError> {
6880
let url = Url::parse(&options.url).map_err(|err| DownloadError::InvalidUrl {
6981
url: options.url.clone(),

src/http_client.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::{sync::LazyLock, time::Duration};
2+
3+
use reqwest::{header::HeaderMap, Client};
4+
5+
pub static SHARED_CLIENT: LazyLock<Client> = LazyLock::new(|| {
6+
Client::builder()
7+
.user_agent("pkgforge/soar")
8+
.build()
9+
.expect("failed to build default client")
10+
});
11+
12+
pub struct ClientConfig {
13+
pub user_agent: Option<String>,
14+
pub headers: Option<HeaderMap>,
15+
pub proxy: Option<String>,
16+
pub timeout: Option<Duration>,
17+
}
18+
19+
impl ClientConfig {
20+
pub fn build(self) -> Result<Client, reqwest::Error> {
21+
let mut builder = Client::builder();
22+
23+
if let Some(user_agent) = self.user_agent {
24+
builder = builder.user_agent(user_agent);
25+
}
26+
27+
if let Some(headers) = self.headers {
28+
builder = builder.default_headers(headers);
29+
}
30+
31+
if let Some(proxy_url) = self.proxy {
32+
builder = builder.proxy(reqwest::Proxy::all(&proxy_url)?);
33+
}
34+
35+
if let Some(timeout) = self.timeout {
36+
builder = builder.timeout(timeout);
37+
}
38+
39+
builder.build()
40+
}
41+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod downloader;
33
pub mod error;
44
pub mod github;
55
pub mod gitlab;
6+
pub mod http_client;
67
pub mod oci;
78
pub mod platform;
89
pub mod utils;

src/platform.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,15 @@ pub struct PlatformDownloadOptions {
129129
}
130130

131131
#[derive(Default)]
132-
pub struct ReleaseHandler<P: ReleasePlatform> {
133-
downloader: Downloader,
134-
client: reqwest::Client,
132+
pub struct ReleaseHandler<'a, P: ReleasePlatform> {
133+
downloader: Downloader<'a>,
135134
_platform: std::marker::PhantomData<P>,
136135
}
137136

138-
impl<P: ReleasePlatform> ReleaseHandler<P> {
137+
impl<P: ReleasePlatform> ReleaseHandler<'_, P> {
139138
pub fn new() -> Self {
140139
Self {
141140
downloader: Downloader::default(),
142-
client: reqwest::Client::new(),
143141
_platform: std::marker::PhantomData,
144142
}
145143
}
@@ -168,7 +166,8 @@ impl<P: ReleasePlatform> ReleaseHandler<P> {
168166
}
169167

170168
Ok(self
171-
.client
169+
.downloader
170+
.client()
172171
.get(&url)
173172
.headers(headers)
174173
.send()

0 commit comments

Comments
 (0)