mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-04 21:07:32 +08:00
perf: optimize network requests and update logic to support aborting
This commit is contained in:
parent
779291151e
commit
23b0493d0b
@ -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 {
|
||||
|
@ -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"] }
|
||||
|
@ -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()
|
||||
|
@ -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")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user