Files
Kigi-CLI/crates/codegen/kigi-sampler/src/shared_http.rs
T
ZacharyZhang-NY a02b555e66 docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment
guidelines: delete redundant restatements, decorative banners, change
narration, and end-of-line comments; keep and tighten the crucial ones
(invariants, bug rationale, SAFETY blocks, ported-source attribution).

No functional code changed. Every edit is proven comment-only against the
prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a
separate doctest-fence check. Where removing a comment made rustfmt or clippy
want to re-lay-out adjacent code, the minimal triggering comment is restored so
code tokens stay byte-identical.

Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy
--workspace --all-targets (0 warnings).

Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for
these guidelines (flags banners, end-of-line comments, change narration, and
commented-out code).
2026-07-23 16:55:39 -04:00

153 lines
5.7 KiB
Rust

//! Process-wide shared `reqwest::Client`s for sampling requests.
//!
//! Sharing one client across all `SamplingClient` instances is safe because
//! the builders below take no config-derived input: auth, extra headers, base
//! URL, and User-Agent are all applied per-request in `SamplingClient::post`.
//! Stale-connection exposure is bounded by HTTP/2 keepalive pings (15s
//! interval, 5s timeout, while idle), the 90s idle-pool eviction, and the
//! first-retry HTTP/1.1 rebuild escape hatch (that client never pools, so
//! every use opens a fresh connection).
//!
//! Wire-level behavior (connection reuse, header isolation, pool-less http1
//! fallback, kill switch) is pinned by the `shared_http_wire` and
//! `shared_http_kill_switch` integration binaries, which own their process
//! environment.
use std::sync::OnceLock;
use std::time::Duration;
static SHARED_H2: OnceLock<reqwest::Client> = OnceLock::new();
static SHARED_HTTP1: OnceLock<reqwest::Client> = OnceLock::new();
/// Kill switch: `KIGI_SAMPLER_SHARED_CLIENT=0` (or `false`, any case) makes
/// every `SamplingClient` build its own `reqwest::Client`. Resolved once per
/// process: the environment cannot change externally after spawn, and
/// latching keeps the rollback state consistent with the read-once pool
/// knobs.
fn sharing_disabled() -> bool {
static DISABLED: OnceLock<bool> = OnceLock::new();
*DISABLED.get_or_init(|| {
let disabled = match std::env::var("KIGI_SAMPLER_SHARED_CLIENT") {
Ok(v) => v == "0" || v.eq_ignore_ascii_case("false"),
Err(_) => false,
};
if disabled {
tracing::info!("sampler HTTP client sharing disabled via KIGI_SAMPLER_SHARED_CLIENT");
}
disabled
})
}
/// Clone the shared client out of `cell`, building it on first use. Build
/// failures are not cached: on `Err` the cell stays empty and the next call
/// retries. A racing loser's freshly built client is simply dropped.
fn shared(
cell: &OnceLock<reqwest::Client>,
build: fn() -> Result<reqwest::Client, reqwest::Error>,
disabled: bool,
) -> Result<reqwest::Client, reqwest::Error> {
if disabled {
return build();
}
if let Some(client) = cell.get() {
return Ok(client.clone());
}
let built = build()?;
Ok(cell.get_or_init(|| built).clone())
}
pub(crate) fn client() -> Result<reqwest::Client, reqwest::Error> {
shared(&SHARED_H2, build_http_client, sharing_disabled())
}
/// Pool-less by construction, so sharing this client is behaviorally
/// identical to building a fresh one.
pub(crate) fn client_http1() -> Result<reqwest::Client, reqwest::Error> {
shared(&SHARED_HTTP1, build_http_client_http1, sharing_disabled())
}
/// Env knobs are read once, when the shared client is first built.
fn build_http_client() -> Result<reqwest::Client, reqwest::Error> {
let pool_max_idle: usize = std::env::var("KIGI_POOL_MAX_IDLE")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(2);
let pool_idle_timeout_secs: u64 = std::env::var("KIGI_POOL_IDLE_TIMEOUT_SECS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(90);
let connect_timeout_secs: u64 = std::env::var("KIGI_CONNECT_TIMEOUT_SECS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(10);
reqwest::Client::builder()
.pool_max_idle_per_host(pool_max_idle)
.pool_idle_timeout(Duration::from_secs(pool_idle_timeout_secs))
.connect_timeout(Duration::from_secs(connect_timeout_secs))
.tcp_nodelay(true)
.http2_keep_alive_interval(Duration::from_secs(15))
.http2_keep_alive_timeout(Duration::from_secs(5))
.http2_keep_alive_while_idle(true)
.build()
}
/// Fallback used after HTTP/2 transport failures.
fn build_http_client_http1() -> Result<reqwest::Client, reqwest::Error> {
let connect_timeout_secs: u64 = std::env::var("KIGI_CONNECT_TIMEOUT_SECS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(10);
reqwest::Client::builder()
.pool_max_idle_per_host(0)
.pool_idle_timeout(Duration::from_secs(0))
.connect_timeout(Duration::from_secs(connect_timeout_secs))
.tcp_nodelay(true)
.http1_only()
.build()
}
#[cfg(test)]
mod tests {
use std::sync::OnceLock;
use std::sync::atomic::{AtomicUsize, Ordering};
use super::shared;
static BUILD_CALLS: AtomicUsize = AtomicUsize::new(0);
/// Fails on the first call (a real `reqwest::Error`, no I/O), then builds.
fn flaky_build() -> Result<reqwest::Client, reqwest::Error> {
if BUILD_CALLS.fetch_add(1, Ordering::SeqCst) == 0 {
return Err(reqwest::Proxy::all("not a proxy url").unwrap_err());
}
reqwest::Client::builder().build()
}
#[test]
fn shared_does_not_cache_build_failures() {
static CELL: OnceLock<reqwest::Client> = OnceLock::new();
assert!(shared(&CELL, flaky_build, false).is_err());
assert!(CELL.get().is_none(), "failure must leave the cell empty");
assert!(shared(&CELL, flaky_build, false).is_ok());
assert!(CELL.get().is_some(), "success must populate the cell");
assert!(shared(&CELL, flaky_build, false).is_ok());
assert_eq!(
BUILD_CALLS.load(Ordering::SeqCst),
2,
"third call must reuse the cached client, not rebuild"
);
}
#[test]
fn shared_disabled_bypasses_cell() {
static CELL: OnceLock<reqwest::Client> = OnceLock::new();
assert!(shared(&CELL, || reqwest::Client::builder().build(), true).is_ok());
assert!(
CELL.get().is_none(),
"disabled mode must never touch the cell"
);
}
}