-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Thread and various performance improvements
- Loading branch information
1 parent
f54584a
commit c0e7aa6
Showing
6 changed files
with
232 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,66 @@ | ||
use dotenv::dotenv; | ||
use std::env; | ||
use num_cpus; | ||
use tracing::info; | ||
use tracing::debug; | ||
|
||
pub struct AppConfig { | ||
pub port: u16, | ||
pub host: String, | ||
pub worker_threads: usize, | ||
pub max_connections: usize, | ||
pub tcp_keepalive_interval: u64, | ||
pub tcp_nodelay: bool, | ||
pub buffer_size: usize, | ||
} | ||
|
||
impl AppConfig { | ||
pub fn new() -> Self { | ||
dotenv().ok(); | ||
info!("Loading environment configuration"); | ||
dotenv::dotenv().ok(); | ||
|
||
// Optimize thread count based on CPU cores | ||
let cpu_count = num_cpus::get(); | ||
debug!("Detected {} CPU cores", cpu_count); | ||
|
||
let default_workers = if cpu_count <= 4 { | ||
cpu_count * 2 | ||
} else { | ||
cpu_count + 4 | ||
}; | ||
debug!("Calculated default worker threads: {}", default_workers); | ||
|
||
Self { | ||
let config = Self { | ||
port: env::var("PORT") | ||
.unwrap_or_else(|_| "3000".to_string()) | ||
.parse() | ||
.expect("PORT must be a number"), | ||
host: env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string()), | ||
} | ||
worker_threads: env::var("WORKER_THREADS") | ||
.ok() | ||
.and_then(|v| v.parse().ok()) | ||
.unwrap_or(default_workers), | ||
max_connections: env::var("MAX_CONNECTIONS") | ||
.ok() | ||
.and_then(|v| v.parse().ok()) | ||
.unwrap_or(10_000), | ||
tcp_keepalive_interval: env::var("TCP_KEEPALIVE_INTERVAL") | ||
.ok() | ||
.and_then(|v| v.parse().ok()) | ||
.unwrap_or(30), | ||
tcp_nodelay: env::var("TCP_NODELAY") | ||
.ok() | ||
.and_then(|v| v.parse().ok()) | ||
.unwrap_or(true), | ||
buffer_size: env::var("BUFFER_SIZE") | ||
.ok() | ||
.and_then(|v| v.parse().ok()) | ||
.unwrap_or(8 * 1024), // 8KB default | ||
}; | ||
|
||
info!("Configuration loaded: port={}, host={}", config.port, config.host); | ||
debug!("Advanced settings: workers={}, max_conn={}, buffer_size={}", | ||
config.worker_threads, config.max_connections, config.buffer_size); | ||
|
||
config | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,55 @@ | ||
use crate::{config::AppConfig, proxy::proxy_request_to_provider}; | ||
use axum::{ | ||
body::Body, | ||
extract::State, | ||
extract::{State, ConnectInfo}, | ||
http::{HeaderMap, Request}, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use serde_json::json; | ||
use std::sync::Arc; | ||
use tracing::{error, info}; | ||
use std::{sync::Arc, net::SocketAddr}; | ||
use tracing::{error, Instrument, debug}; | ||
|
||
pub async fn health_check() -> impl IntoResponse { | ||
Json(json!({ | ||
"status": "healthy", | ||
"version": env!("CARGO_PKG_VERSION") | ||
})) | ||
debug!("Health check endpoint called"); | ||
Json(json!({ "status": "healthy", "version": env!("CARGO_PKG_VERSION") })) | ||
} | ||
|
||
pub async fn proxy_request( | ||
State(config): State<Arc<AppConfig>>, | ||
headers: HeaderMap, | ||
ConnectInfo(addr): ConnectInfo<SocketAddr>, | ||
request: Request<Body>, | ||
) -> impl IntoResponse { | ||
let provider = headers | ||
.get("x-provider") | ||
.and_then(|h| h.to_str().ok()) | ||
.unwrap_or("openai"); | ||
|
||
info!( | ||
debug!( | ||
"Received request for provider: {}, client: {}, path: {}", | ||
provider, | ||
addr, | ||
request.uri().path() | ||
); | ||
|
||
let span = tracing::info_span!( | ||
"proxy_request", | ||
provider = provider, | ||
method = %request.method(), | ||
path = %request.uri().path(), | ||
"Incoming proxy request" | ||
client = %addr | ||
); | ||
|
||
match proxy_request_to_provider(config, provider, request).await { | ||
Ok(response) => response, | ||
Err(e) => { | ||
error!( | ||
error = %e, | ||
provider = provider, | ||
"Proxy request failed" | ||
); | ||
e.into_response() | ||
async move { | ||
match proxy_request_to_provider(config, provider, request).await { | ||
Ok(response) => response, | ||
Err(e) => { | ||
error!(error = %e, "Proxy request failed"); | ||
e.into_response() | ||
} | ||
} | ||
} | ||
.instrument(span) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
use once_cell::sync::Lazy; | ||
use std::time::Duration; | ||
use crate::config::AppConfig; | ||
use tracing::info; | ||
use tracing::debug; | ||
|
||
pub fn create_client(config: &AppConfig) -> reqwest::Client { | ||
info!("Creating HTTP client with optimized settings"); | ||
debug!( | ||
"Client config: max_connections={}, keepalive={}s, nodelay={}", | ||
config.max_connections, | ||
config.tcp_keepalive_interval, | ||
config.tcp_nodelay | ||
); | ||
|
||
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(|| { | ||
reqwest::Client::builder() | ||
.pool_idle_timeout(Duration::from_secs(60)) | ||
.pool_max_idle_per_host(32) | ||
.tcp_keepalive(Duration::from_secs(60)) | ||
.timeout(Duration::from_secs(60)) | ||
.pool_max_idle_per_host(config.max_connections) | ||
.pool_idle_timeout(Duration::from_secs(30)) | ||
.http2_prior_knowledge() | ||
.http2_keep_alive_interval(Duration::from_secs(config.tcp_keepalive_interval)) | ||
.http2_keep_alive_timeout(Duration::from_secs(30)) | ||
.http2_adaptive_window(true) | ||
.tcp_keepalive(Duration::from_secs(config.tcp_keepalive_interval)) | ||
.tcp_nodelay(config.tcp_nodelay) | ||
.use_rustls_tls() | ||
.timeout(Duration::from_secs(30)) | ||
.connect_timeout(Duration::from_secs(10)) | ||
.gzip(true) | ||
.brotli(true) | ||
.build() | ||
.expect("Failed to create HTTP client") | ||
} | ||
|
||
pub static CLIENT: Lazy<reqwest::Client> = Lazy::new(|| { | ||
let config = AppConfig::new(); | ||
create_client(&config) | ||
}); |
Oops, something went wrong.