Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(desktop-gpui): reuse one HTTP client for authed API requests
Richie McIlroy53012ae88d50
diff --git a/apps/desktop-gpui/src/auth.rs b/apps/desktop-gpui/src/auth.rsindex 4fae01f01..53e95f9e9 100644--- a/apps/desktop-gpui/src/auth.rs+++ b/apps/desktop-gpui/src/auth.rs@@ -323,20 +323,66 @@ impl UploadRequestContext {}}-pub fn api_client() -> Result<reqwest::Client, AuthApiError> {+/// Idle API connections are dropped after 4 s, before the 5 s keep-alive+/// timeout of a self-hosted Node server, so a request is never sent on a+/// connection the server is closing.+pub(crate) const API_POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(4);+/// The shared API client is rebuilt this often, so system proxy changes still+/// apply. It is wall clock time, which counts a sleep where the pool's idle+/// clock does not, so calls after a longer sleep start on new connections.+const API_CLIENT_MAX_AGE: Duration = Duration::from_secs(30);++fn api_client_builder() -> reqwest::ClientBuilder {reqwest::Client::builder().connect_timeout(Duration::from_secs(10)).timeout(Duration::from_secs(45))+}++/// A new client, for a caller that keeps one for a single upload.+pub fn api_client() -> Result<reqwest::Client, AuthApiError> {+ api_client_builder().build().map_err(|error| AuthApiError::Other(error.to_string()))}+static SHARED_API_CLIENT: Mutex<Option<(std::time::SystemTime, reqwest::Client)>> =+ Mutex::new(None);++/// Starts the next `authed_request` on a new client, so a test's connection+/// count cannot see another test's client reach its maximum age.+#[cfg(test)]+pub(crate) fn reset_shared_api_client() {+ *SHARED_API_CLIENT+ .lock()+ .unwrap_or_else(|error| error.into_inner()) = None;+}++/// The client `authed_request` uses. Calls close together, like Instant+/// presigns about once a second or the requests after a recording stops,+/// reuse its open connections instead of each paying a TCP and TLS handshake.+fn shared_api_client() -> Result<reqwest::Client, AuthApiError> {+ let mut shared = SHARED_API_CLIENT+ .lock()+ .unwrap_or_else(|error| error.into_inner());+ if let Some((built, client)) = shared.as_ref()+ && built.elapsed().is_ok_and(|age| age < API_CLIENT_MAX_AGE)+ {+ return Ok(client.clone());+ }+ let client = api_client_builder()+ .pool_idle_timeout(API_POOL_IDLE_TIMEOUT)+ .build()+ .map_err(|error| AuthApiError::Other(error.to_string()))?;+ *shared = Some((std::time::SystemTime::now(), client.clone()));+ Ok(client)+}+pub async fn authed_request(method: reqwest::Method,path: &str,body: Option<Value>,) -> Result<reqwest::Response, AuthApiError> {- authed_request_with(&api_client()?, method, path, body).await+ authed_request_with(&shared_api_client()?, method, path, body).await}pub async fn authed_request_with(diff --git a/apps/desktop-gpui/src/upload/multipart_tests.rs b/apps/desktop-gpui/src/upload/multipart_tests.rsindex df5be2ea8..5b41803dc 100644--- a/apps/desktop-gpui/src/upload/multipart_tests.rs+++ b/apps/desktop-gpui/src/upload/multipart_tests.rs@@ -756,3 +756,109 @@ async fn multipart_mock_network_benchmark() {);}}++/// Signs this thread's store in against `server`, so `auth::authed_request`+/// builds its URL and credentials exactly as the app does.+struct SignedIn(PathBuf);++impl SignedIn {+ fn new(server: std::net::SocketAddr) -> Self {+ static NEXT: AtomicUsize = AtomicUsize::new(0);+ let dir = std::env::temp_dir().join(format!(+ "cap-gpui-api-client-{}-{}",+ std::process::id(),+ NEXT.fetch_add(1, Ordering::AcqRel),+ ));+ std::fs::create_dir_all(&dir).unwrap();+ let path = dir.join("store");+ std::fs::write(+ &path,+ serde_json::to_vec(&json!({+ "general_settings": { "serverUrl": format!("http://{server}") },+ "auth": { "secret": { "api_key": "api-key" }, "user_id": "owner" },+ }))+ .unwrap(),+ )+ .unwrap();+ crate::store::set_test_tauri_store_path(Some(path));+ Self(dir)+ }+}++impl Drop for SignedIn {+ fn drop(&mut self) {+ crate::store::set_test_tauri_store_path(None);+ let _ = std::fs::remove_dir_all(&self.0);+ }+}++async fn authed_presign(part: u32) -> Duration {+ let started = Instant::now();+ let response = auth::authed_request(+ reqwest::Method::POST,+ &format!("/presign/{part}"),+ Some(json!({ "partNumber": part })),+ )+ .await+ .unwrap();+ assert!(response.status().is_success());+ let body = response.json::<Value>().await.unwrap();+ assert!(+ body["presignedUrl"]+ .as_str()+ .unwrap()+ .ends_with(&format!("/part/{part}"))+ );+ started.elapsed()+}++#[tokio::test]+async fn sequential_api_requests_reuse_one_connection_until_it_idles_out() {+ let server = mock_network::start(fast_local_profile());+ let _signed_in = SignedIn::new(server.addr);+ auth::reset_shared_api_client();+ for part in 1..=20 {+ authed_presign(part).await;+ }+ assert_eq!(server.connections.load(Ordering::Acquire), 1);++ tokio::time::sleep(auth::API_POOL_IDLE_TIMEOUT + Duration::from_millis(500)).await;+ authed_presign(21).await;+ assert_eq!(server.connections.load(Ordering::Acquire), 2);+}++/// `cargo test --bin cap-gpui api_request_mock_network_benchmark -- --ignored --nocapture`+/// 100 sequential presigns through `auth::authed_request`, the way Instant+/// recording sends them, with no added latency and over a 50 ms RTT link+/// where a new connection costs a 2 RTT TCP and TLS handshake.+#[tokio::test]+#[ignore = "benchmark: 200 presigns through a throttled loopback server"]+async fn api_request_mock_network_benchmark() {+ const CALLS: u32 = 100;+ println!("| RTT | connections | p50 | p95 | total |");+ println!("|---|---|---|---|---|");+ for (rtt_ms, handshake_rtts) in [(0u64, 0u32), (50, 2)] {+ let server = mock_network::start(mock_network::Profile {+ rtt: Duration::from_millis(rtt_ms),+ link_bytes_per_sec: 1e9,+ max_window: 1 << 20,+ handshake_rtts,+ presign_server_time: Duration::ZERO,+ });+ let _signed_in = SignedIn::new(server.addr);+ let started = Instant::now();+ let mut latencies = Vec::new();+ for part in 1..=CALLS {+ latencies.push(authed_presign(part).await);+ }+ let total = started.elapsed();+ latencies.sort();+ let percentile = |p: usize| latencies[(latencies.len() * p / 100).min(latencies.len() - 1)];+ println!(+ "| {rtt_ms} ms | {} | {:.2?} | {:.2?} | {total:.2?} |",+ server.connections.load(Ordering::Acquire),+ percentile(50),+ percentile(95),+ );+ }+}