A free, open source, and extensible speech-to-text application that works completely offline.
fix: reuse one HTTP client for post-processing requests (#2344)
rnmrnmrnmrnmrnmrnm0ed36e112a17
diff --git a/src-tauri/src/llm_client.rs b/src-tauri/src/llm_client.rsindex 3ab51339..b727bd1e 100644--- a/src-tauri/src/llm_client.rs+++ b/src-tauri/src/llm_client.rs@@ -6,6 +6,7 @@ use serde_json::Value;use std::collections::HashSet;use std::error::Error as StdError;use std::sync::{Mutex, OnceLock};+use std::time::Duration;#[derive(Debug, Serialize)]struct ChatMessage {@@ -215,13 +216,46 @@ fn build_headers(provider: &PostProcessProvider, api_key: &str) -> Result<HeaderOk(headers)}-/// Create an HTTP client with provider-specific headers-fn create_client(provider: &PostProcessProvider, api_key: &str) -> Result<reqwest::Client, String> {- let headers = build_headers(provider, api_key)?;- reqwest::Client::builder()- .default_headers(headers)+/// HTTP client with provider-specific headers. The headers are set per+/// request, so every instance can share one connection pool and consecutive+/// requests reuse an open connection instead of a new TCP + TLS handshake.+struct ProviderClient {+ client: &'static reqwest::Client,+ headers: HeaderMap,+}++impl ProviderClient {+ fn post(&self, url: &str) -> reqwest::RequestBuilder {+ self.client.post(url).headers(self.headers.clone())+ }++ fn get(&self, url: &str) -> reqwest::RequestBuilder {+ self.client.get(url).headers(self.headers.clone())+ }+}++/// Process-wide client. A short idle timeout and TCP keepalive keep the pool+/// from handing out connections that died during sleep or a network change.+fn shared_client() -> Result<&'static reqwest::Client, String> {+ static SHARED: OnceLock<reqwest::Client> = OnceLock::new();+ if let Some(client) = SHARED.get() {+ return Ok(client);+ }+ let client = reqwest::Client::builder()+ .pool_idle_timeout(Duration::from_secs(30))+ .tcp_keepalive(Duration::from_secs(15))+ .connect_timeout(Duration::from_secs(10)).build()- .map_err(|e| report_reqwest_error("Failed to build HTTP client", &e))+ .map_err(|e| report_reqwest_error("Failed to build HTTP client", &e))?;+ Ok(SHARED.get_or_init(|| client))+}++/// Create an HTTP client with provider-specific headers+fn create_client(provider: &PostProcessProvider, api_key: &str) -> Result<ProviderClient, String> {+ Ok(ProviderClient {+ client: shared_client()?,+ headers: build_headers(provider, api_key)?,+ })}/// Format a bounded error source chain.