use std::time::Duration;
#[derive(Debug, Clone)]
pub struct PollWorkerConfig {
pub concurrency: usize,
pub idle_backoff: Duration,
pub error_backoff: Duration,
}
impl Default for PollWorkerConfig {
fn default() -> Self {
Self {
concurrency: 4,
idle_backoff: Duration::from_millis(100),
error_backoff: Duration::from_secs(1),
}
}
}
impl PollWorkerConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_concurrency(mut self, concurrency: usize) -> Self {
self.concurrency = concurrency;
self
}
pub fn with_idle_backoff(mut self, d: Duration) -> Self {
self.idle_backoff = d;
self
}
pub fn with_error_backoff(mut self, d: Duration) -> Self {
self.error_backoff = d;
self
}
}
#[derive(Debug, Clone)]
pub struct TaskStartupConfig {
pub concurrency: usize,
pub timeout: Duration,
pub enable_ready_check: bool,
pub ready_check_timeout: Duration,
}
impl Default for TaskStartupConfig {
fn default() -> Self {
Self {
concurrency: num_cpus::get(),
timeout: Duration::from_secs(30),
enable_ready_check: true,
ready_check_timeout: Duration::from_secs(30),
}
}
}
impl TaskStartupConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_concurrency(mut self, concurrency: usize) -> Self {
self.concurrency = concurrency;
self
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn with_ready_check(mut self, enable: bool) -> Self {
self.enable_ready_check = enable;
self
}
pub fn with_ready_check_timeout(mut self, timeout: Duration) -> Self {
self.ready_check_timeout = timeout;
self
}
}
#[derive(Debug, Clone)]
pub struct HealthCheckConfig {
pub interval: Duration,
pub timeout: Duration,
pub failure_threshold: u32,
pub enabled: bool,
}
impl Default for HealthCheckConfig {
fn default() -> Self {
Self {
interval: Duration::from_secs(10),
timeout: Duration::from_secs(5),
failure_threshold: 3,
enabled: true,
}
}
}
impl HealthCheckConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn with_failure_threshold(mut self, threshold: u32) -> Self {
self.failure_threshold = threshold;
self
}
pub fn with_enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
}
#[derive(Debug, Clone)]
pub struct MetricsConfig {
pub enabled: bool,
pub port: u16,
pub path: String,
}
impl Default for MetricsConfig {
fn default() -> Self {
Self {
enabled: true,
port: 9090,
path: "/metrics".to_string(),
}
}
}
impl MetricsConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn with_port(mut self, port: u16) -> Self {
self.port = port;
self
}
pub fn with_path(mut self, path: impl Into<String>) -> Self {
self.path = path.into();
self
}
}
#[derive(Debug, Clone)]
pub struct RuntimeConfig {
pub shutdown_timeout: Duration,
pub task_startup: TaskStartupConfig,
pub health_check: HealthCheckConfig,
pub metrics: MetricsConfig,
pub default_poll_worker: PollWorkerConfig,
}
impl Default for RuntimeConfig {
fn default() -> Self {
Self {
shutdown_timeout: Duration::from_secs(5),
task_startup: TaskStartupConfig::default(),
health_check: HealthCheckConfig::default(),
metrics: MetricsConfig::default(),
default_poll_worker: PollWorkerConfig::default(),
}
}
}
impl RuntimeConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_shutdown_timeout(mut self, timeout: Duration) -> Self {
self.shutdown_timeout = timeout;
self
}
pub fn with_task_startup(mut self, config: TaskStartupConfig) -> Self {
self.task_startup = config;
self
}
pub fn with_health_check(mut self, config: HealthCheckConfig) -> Self {
self.health_check = config;
self
}
pub fn with_metrics(mut self, config: MetricsConfig) -> Self {
self.metrics = config;
self
}
pub fn with_default_poll_worker(mut self, config: PollWorkerConfig) -> Self {
self.default_poll_worker = config;
self
}
pub fn validate(&self) -> Result<(), String> {
if self.shutdown_timeout.is_zero() {
return Err("shutdown_timeout must be greater than zero".to_string());
}
if self.task_startup.concurrency == 0 {
return Err("task_startup.concurrency must be greater than zero".to_string());
}
if self.health_check.failure_threshold == 0 {
return Err("health_check.failure_threshold must be greater than zero".to_string());
}
Ok(())
}
}