use std::num::{NonZeroU32, NonZeroU64};
use std::time::Duration;
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
const TEST_REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
const DEFAULT_MAX_CONNECTION_AGE: Duration = Duration::from_secs(30 * 60);
const DEFAULT_REPLENISH_N_PER_SECOND_PER_IP: NonZeroU64 = NonZeroU64::new(16).unwrap();
const DEFAULT_BURST_SIZE: NonZeroU32 = NonZeroU32::new(128).unwrap();
const DEFAULT_MAX_CONCURRENT_CONNECTIONS: u64 = 1_000;
const BENCH_REQUEST_TIMEOUT: Duration = Duration::from_secs(24 * 60 * 60);
pub fn duration_to_human_readable_string(duration: Duration) -> &'static str {
Box::new(humantime::format_duration(duration).to_string()).leak()
}
#[derive(clap::Args, Copy, Clone, Debug, PartialEq, Eq)]
pub struct GrpcOptionsInternal {
#[arg(
long = "grpc.timeout",
default_value = duration_to_human_readable_string(DEFAULT_REQUEST_TIMEOUT),
value_parser = humantime::parse_duration,
value_name = "DURATION"
)]
pub request_timeout: Duration,
}
impl Default for GrpcOptionsInternal {
fn default() -> Self {
Self { request_timeout: DEFAULT_REQUEST_TIMEOUT }
}
}
impl From<GrpcOptionsExternal> for GrpcOptionsInternal {
fn from(value: GrpcOptionsExternal) -> Self {
let GrpcOptionsExternal { request_timeout, .. } = value;
Self { request_timeout }
}
}
impl GrpcOptionsInternal {
pub fn test() -> Self {
GrpcOptionsExternal::test().into()
}
pub fn bench() -> Self {
GrpcOptionsExternal::bench().into()
}
}
#[derive(clap::Args, Copy, Clone, Debug, PartialEq, Eq)]
pub struct GrpcOptionsExternal {
#[arg(
long = "grpc.timeout",
default_value = duration_to_human_readable_string(DEFAULT_REQUEST_TIMEOUT),
value_parser = humantime::parse_duration,
value_name = "DURATION"
)]
pub request_timeout: Duration,
#[arg(
long = "grpc.max_connection_age",
default_value = duration_to_human_readable_string(DEFAULT_MAX_CONNECTION_AGE),
value_parser = humantime::parse_duration,
value_name = "MAX_CONNECTION_AGE"
)]
pub max_connection_age: Duration,
#[arg(
long = "grpc.burst_size",
default_value_t = DEFAULT_BURST_SIZE,
value_name = "BURST_SIZE"
)]
pub burst_size: NonZeroU32,
#[arg(
long = "grpc.replenish_n_per_second",
default_value_t = DEFAULT_REPLENISH_N_PER_SECOND_PER_IP,
value_name = "DEFAULT_REPLENISH_N_PER_SECOND"
)]
pub replenish_n_per_second_per_ip: NonZeroU64,
#[arg(
long = "grpc.max_concurrent_connections",
default_value_t = DEFAULT_MAX_CONCURRENT_CONNECTIONS,
value_name = "MAX_CONCURRENT_CONNECTIONS"
)]
pub max_concurrent_connections: u64,
}
impl Default for GrpcOptionsExternal {
fn default() -> Self {
Self {
request_timeout: DEFAULT_REQUEST_TIMEOUT,
max_connection_age: DEFAULT_MAX_CONNECTION_AGE,
burst_size: DEFAULT_BURST_SIZE,
replenish_n_per_second_per_ip: DEFAULT_REPLENISH_N_PER_SECOND_PER_IP,
max_concurrent_connections: DEFAULT_MAX_CONCURRENT_CONNECTIONS,
}
}
}
impl GrpcOptionsExternal {
pub fn test() -> Self {
Self {
request_timeout: TEST_REQUEST_TIMEOUT,
..Default::default()
}
}
pub fn bench() -> Self {
Self {
request_timeout: BENCH_REQUEST_TIMEOUT,
max_connection_age: BENCH_REQUEST_TIMEOUT,
burst_size: NonZeroU32::new(100_000).unwrap(),
replenish_n_per_second_per_ip: NonZeroU64::new(100_000).unwrap(),
max_concurrent_connections: u64::MAX,
}
}
}