use reqwest::{Client, ClientBuilder};
use std::time::Duration;
pub async fn prewarm_connections(client: &Client, base_url: &str) -> Result<(), reqwest::Error> {
let endpoints = vec!["/ok", "/time"];
for endpoint in endpoints {
let _ = client
.get(format!("{}{}", base_url, endpoint))
.timeout(Duration::from_millis(1000))
.send()
.await;
}
Ok(())
}
pub fn create_optimized_client() -> Result<Client, reqwest::Error> {
ClientBuilder::new()
.no_proxy()
.pool_max_idle_per_host(10) .pool_idle_timeout(Duration::from_secs(90)) .tcp_nodelay(true) .http2_adaptive_window(true) .http2_initial_stream_window_size(512 * 1024) .gzip(true) .user_agent(concat!(
"polyfill-rs/",
env!("CARGO_PKG_VERSION"),
" (high-frequency-trading)"
))
.build()
}
pub fn create_colocated_client() -> Result<Client, reqwest::Error> {
ClientBuilder::new()
.no_proxy()
.pool_max_idle_per_host(20) .pool_idle_timeout(Duration::from_secs(60)) .connect_timeout(Duration::from_millis(1000)) .timeout(Duration::from_millis(10000)) .tcp_nodelay(true)
.tcp_keepalive(Duration::from_secs(30))
.http2_adaptive_window(true)
.http2_keep_alive_interval(Duration::from_secs(10))
.http2_keep_alive_timeout(Duration::from_secs(5))
.http2_keep_alive_while_idle(true)
.gzip(false)
.no_brotli() .user_agent(concat!(
"polyfill-rs/",
env!("CARGO_PKG_VERSION"),
" (colocated-hft)"
))
.build()
}
pub fn create_internet_client() -> Result<Client, reqwest::Error> {
ClientBuilder::new()
.no_proxy()
.pool_max_idle_per_host(5)
.pool_idle_timeout(Duration::from_secs(90))
.connect_timeout(Duration::from_millis(10000)) .timeout(Duration::from_millis(60000)) .tcp_nodelay(true)
.tcp_keepalive(Duration::from_secs(120))
.http1_title_case_headers()
.gzip(true)
.user_agent(concat!(
"polyfill-rs/",
env!("CARGO_PKG_VERSION"),
" (internet-trading)"
))
.build()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_optimized_client_creation() {
let client = create_optimized_client();
assert!(client.is_ok());
}
#[test]
fn test_colocated_client_creation() {
let client = create_colocated_client();
assert!(client.is_ok());
}
#[test]
fn test_internet_client_creation() {
let client = create_internet_client();
assert!(client.is_ok());
}
}