Skip to main content

px_core/
http.rs

1//! Shared HTTP client tunings.
2//!
3//! Every exchange client (Polymarket, Kalshi, Opinion, and any future
4//! addition) should build its `reqwest::Client` via `tuned_client_builder()`.
5//! Centralising the tunings here means one edit rolls out to every exchange
6//! — we don't want performance fixes to drift across three independent
7//! builders.
8//!
9//! Flags applied:
10//!
11//! - `http2_adaptive_window(true)`
12//! - `http2_initial_stream_window_size(512 KB)` — empirically optimal for
13//!   the ~480 KB `/simplified-markets` response class.
14//! - `tcp_nodelay(true)` — disable Nagle's. Right call for HTTP/2.
15//! - `pool_max_idle_per_host(10)` — deep enough for burst patterns.
16//! - `http2_keep_alive_interval(15s)` — keeps the HTTP/2 connection hot
17//!   across bursty call patterns.
18//! - `no_proxy()` — skip the slow OS proxy lookup.
19//!
20//! Exchange-specific settings (`.timeout(…)`, `.user_agent(…)`, `.connector(…)`)
21//! layer on top of the returned builder.
22
23use std::time::Duration;
24
25pub const HTTP2_INITIAL_STREAM_WINDOW_BYTES: u32 = 512 * 1024;
26pub const POOL_MAX_IDLE_PER_HOST: usize = 10;
27pub const HTTP2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(15);
28
29/// A `reqwest::ClientBuilder` preloaded with the openpx-wide HTTP tunings.
30/// Layer exchange-specific settings on top before calling `.build()`.
31#[cfg(feature = "http")]
32pub fn tuned_client_builder() -> reqwest::ClientBuilder {
33    reqwest::Client::builder()
34        .http2_adaptive_window(true)
35        .http2_initial_stream_window_size(HTTP2_INITIAL_STREAM_WINDOW_BYTES)
36        .tcp_nodelay(true)
37        .pool_max_idle_per_host(POOL_MAX_IDLE_PER_HOST)
38        .http2_keep_alive_interval(HTTP2_KEEP_ALIVE_INTERVAL)
39        .no_proxy()
40}