use std::sync::OnceLock;
use std::time::Duration;
use crate::artisan::Artful;
use crate::rocket::HttpOptions;
const DEFAULT_POOL_IDLE_TIMEOUT: u64 = 90;
const DEFAULT_POOL_MAX_IDLE_PER_HOST: usize = 20;
pub fn get_client() -> &'static reqwest::Client {
static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
CLIENT.get_or_init(|| build_client(Artful::get_config().http))
}
fn build_client(http: HttpOptions) -> reqwest::Client {
let pool_idle_timeout = http.pool_idle_timeout.unwrap_or(DEFAULT_POOL_IDLE_TIMEOUT);
let pool_max_idle_per_host = http
.pool_max_idle_per_host
.unwrap_or(DEFAULT_POOL_MAX_IDLE_PER_HOST);
reqwest::Client::builder()
.pool_idle_timeout(Some(Duration::from_secs(pool_idle_timeout)))
.pool_max_idle_per_host(pool_max_idle_per_host)
.build()
.expect("Failed to create HTTP client")
}