refactor: move HTTP client logic to http.rs and remove unused request module
parent
d7ef92684e
commit
f78b0011c0
|
@ -1,12 +1,31 @@
|
|||
use crate::types::GeminiKey;
|
||||
use crate::{config::KeyCheckerConfig, error::ValidatorError};
|
||||
use backon::{ExponentialBuilder, Retryable};
|
||||
use reqwest::Client;
|
||||
use serde::Serialize;
|
||||
use tokio::time::Duration;
|
||||
use std::time::Duration;
|
||||
use tokio::time::Duration as TokioDuration;
|
||||
use tracing::debug;
|
||||
use url::Url;
|
||||
|
||||
use crate::error::ValidatorError;
|
||||
use crate::types::GeminiKey;
|
||||
pub fn client_builder(config: &KeyCheckerConfig) -> Result<Client, ValidatorError> {
|
||||
// Set the maximum number of connections per host based on concurrency.
|
||||
let pool_size = config.concurrency / 2;
|
||||
|
||||
let mut builder = Client::builder()
|
||||
.timeout(Duration::from_secs(config.timeout_sec))
|
||||
.pool_max_idle_per_host(pool_size);
|
||||
|
||||
if let Some(ref proxy_url) = config.proxy {
|
||||
builder = builder.proxy(reqwest::Proxy::all(proxy_url.clone())?);
|
||||
}
|
||||
|
||||
if !config.enable_multiplexing {
|
||||
builder = builder.http1_only();
|
||||
}
|
||||
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
|
||||
pub async fn send_request<T>(
|
||||
client: Client,
|
||||
|
@ -20,8 +39,8 @@ where
|
|||
{
|
||||
let retry_policy = ExponentialBuilder::default()
|
||||
.with_max_times(max_retries)
|
||||
.with_min_delay(Duration::from_secs(1))
|
||||
.with_max_delay(Duration::from_secs(2));
|
||||
.with_min_delay(TokioDuration::from_secs(1))
|
||||
.with_max_delay(TokioDuration::from_secs(2));
|
||||
|
||||
(async || {
|
||||
let response = client
|
|
@ -1,22 +0,0 @@
|
|||
use crate::{config::KeyCheckerConfig, error::ValidatorError};
|
||||
use reqwest::Client;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn client_builder(config: &KeyCheckerConfig) -> Result<Client, ValidatorError> {
|
||||
// Set the maximum number of connections per host based on concurrency.
|
||||
let pool_size = config.concurrency / 2;
|
||||
|
||||
let mut builder = Client::builder()
|
||||
.timeout(Duration::from_secs(config.timeout_sec))
|
||||
.pool_max_idle_per_host(pool_size);
|
||||
|
||||
if let Some(ref proxy_url) = config.proxy {
|
||||
builder = builder.proxy(reqwest::Proxy::all(proxy_url.clone())?);
|
||||
}
|
||||
|
||||
if !config.enable_multiplexing {
|
||||
builder = builder.http1_only();
|
||||
}
|
||||
|
||||
Ok(builder.build()?)
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
pub mod http_client;
|
||||
pub mod request;
|
||||
pub mod http;
|
||||
|
||||
pub use http_client::client_builder;
|
||||
pub use request::send_request;
|
||||
pub use http::{client_builder, send_request};
|
||||
|
|
|
@ -101,13 +101,10 @@ impl ValidationService {
|
|||
pub async fn start_validation() -> Result<(), ValidatorError> {
|
||||
let config = KeyCheckerConfig::load_config()?;
|
||||
|
||||
// 加载密钥
|
||||
let keys = load_keys_from_txt(config.input_path.as_path())?;
|
||||
|
||||
// 构建HTTP客户端
|
||||
let client = client_builder(&config)?;
|
||||
|
||||
// 创建验证服务并启动
|
||||
let validation_service = ValidationService::new(config, client);
|
||||
validation_service.validate_keys(keys).await
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue