Skip to main content

alien_bindings/
http_client.rs

1/// Shared HTTP client configuration and utilities to prevent file descriptor exhaustion.
2///
3/// This module provides a centralized way to create reqwest clients with proper
4/// connection pooling, timeouts, and resource limits suitable for constrained
5/// environments like AWS Lambda (1024 FD limit).
6use reqwest::Client;
7use std::time::Duration;
8
9/// Creates a properly configured reqwest Client with connection pooling and timeouts.
10///
11/// This client is designed to prevent file descriptor exhaustion by:
12/// - Limiting idle connection pool size per host
13/// - Setting idle connection timeouts to clean up unused connections
14/// - Adding request timeouts to prevent indefinite hangs
15///
16/// Use this for ALL reqwest client creation to ensure consistent resource management.
17pub fn create_http_client() -> Client {
18    Client::builder()
19        .timeout(Duration::from_secs(60))
20        .connect_timeout(Duration::from_secs(10))
21        .pool_max_idle_per_host(4) // Critical: Limit idle connections to prevent FD exhaustion
22        .pool_idle_timeout(Some(Duration::from_secs(90))) // Close idle connections after 90s
23        .build()
24        .expect("Failed to build HTTP client with connection pooling")
25}
26
27/// Creates a reqwest Client with custom timeout settings.
28/// Still includes connection pooling limits to prevent FD exhaustion.
29pub fn create_http_client_with_timeout(timeout: Duration) -> Client {
30    Client::builder()
31        .timeout(timeout)
32        .connect_timeout(Duration::from_secs(10))
33        .pool_max_idle_per_host(4)
34        .pool_idle_timeout(Some(Duration::from_secs(90)))
35        .build()
36        .expect("Failed to build HTTP client with custom timeout")
37}