oxicode_agent/tools/http_client.rs
1//! Shared HTTP client singleton for oxicode-agent tools.
2//!
3//! All agent tools that need an HTTP client should use `shared_http_client()`
4//! to reuse the same connection pool and TLS sessions across the process lifetime.
5
6use std::sync::OnceLock;
7
8/// Return a process-lifetime shared `reqwest::Client`.
9///
10/// The client is configured with sensible defaults for tool use:
11/// connection pooling (4 idle conns/host, 30 s idle timeout) and a
12/// 30-second request timeout.
13pub fn shared_http_client() -> &'static reqwest::Client {
14 static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
15 CLIENT.get_or_init(|| {
16 // SAFETY: `reqwest::Client::builder()` with only timeout/pool settings
17 // cannot fail to build — no TLS misconfiguration or invalid proxy
18 // is involved. Infallible by construction.
19 #[allow(clippy::expect_used)]
20 reqwest::Client::builder()
21 .pool_max_idle_per_host(4)
22 .pool_idle_timeout(std::time::Duration::from_secs(30))
23 .timeout(std::time::Duration::from_secs(30))
24 .build()
25 .expect("HTTP client init failed")
26 })
27}