perf: optimize network requests and update logic to support aborting

This commit is contained in:
wonfen 2025-05-04 11:25:48 +08:00
parent 779291151e
commit 23b0493d0b
4 changed files with 14 additions and 12 deletions

View File

@ -107,9 +107,8 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
let start = Instant::now();
// 使用网络管理器发送请求设置10秒超时
let response = NetworkManager::global()
.get(&url, proxy_type, Some(10), user_agent, false)
.get_with_interrupt(&url, proxy_type, Some(10), user_agent, false)
.await;
match response {

View File

@ -9,6 +9,6 @@ debug = []
reqwest = { version = "0.12.15", features = ["json"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
tokio = { version = "1.44.1", features = ["rt", "macros", "time"] }
[dev-dependencies]
tokio = { version = "1.44.1", features = ["rt", "macros"] }

View File

@ -9,13 +9,22 @@ pub use model::{MihomoData, MihomoManager};
impl MihomoManager {
pub fn new(mihomo_server: String, headers: HeaderMap) -> Self {
let client = reqwest::ClientBuilder::new()
.default_headers(headers)
.no_proxy()
.timeout(Duration::from_secs(15))
.pool_max_idle_per_host(5)
.pool_idle_timeout(Duration::from_secs(15))
.build()
.expect("Failed to build reqwest client");
Self {
mihomo_server,
data: Arc::new(Mutex::new(MihomoData {
proxies: serde_json::Value::Null,
providers_proxies: serde_json::Value::Null,
})),
headers,
client,
}
}
@ -49,12 +58,7 @@ impl MihomoManager {
url: String,
data: Option<serde_json::Value>,
) -> Result<serde_json::Value, String> {
let client_response = reqwest::ClientBuilder::new()
.default_headers(self.headers.clone())
.no_proxy()
.timeout(Duration::from_secs(60))
.build()
.map_err(|e| e.to_string())?
let client_response = self.client
.request(method.clone(), &url)
.json(&data.unwrap_or(json!({})))
.send()

View File

@ -1,5 +1,4 @@
use std::sync::{Arc, Mutex};
use reqwest::header::HeaderMap;
pub struct MihomoData {
pub(crate) proxies: serde_json::Value,
@ -10,7 +9,7 @@ pub struct MihomoData {
pub struct MihomoManager {
pub(crate) mihomo_server: String,
pub(crate) data: Arc<Mutex<MihomoData>>,
pub(crate) headers: HeaderMap,
pub(crate) client: reqwest::Client,
}
#[cfg(feature = "debug")]