use serde::{Deserialize, Serialize}; pub const DEFAULT_KEY_PREFIX: &str = "event:actor"; pub const DEFAULT_CALL_TIMEOUT_SECS: u64 = 5; pub const DEFAULT_IDLE_TIMEOUT_SECS: u64 = 60; pub const DEFAULT_LEASE_TTL_SECS: u64 = 15; pub const DEFAULT_MAILBOX_CAPACITY: usize = 1024; pub const DEFAULT_INBOX_MAX_LEN: usize = 10_000; pub const DEFAULT_READ_COUNT: usize = 64; pub const DEFAULT_READ_BLOCK_MS: u64 = 1_000; /// event 进程的 actor host 参数。`node_id` 为空时回退到 `actor.node` 文件(单机兼容)。 #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(default)] pub struct ActorConfig { pub node_id: Option, pub key_prefix: String, pub call_timeout_secs: u64, pub idle_timeout_secs: Option, pub lease_ttl_secs: u64, pub mailbox_capacity: usize, pub inbox_max_len: usize, pub read_count: usize, pub read_block_ms: u64, } impl ActorConfig { pub fn validate(&self) -> Result<(), String> { if let Some(node_id) = self.node_id.as_deref().filter(|value| !value.is_empty()) { if node_id.len() > 128 || !node_id .chars() .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-')) { return Err( "event.actor.node_id must use letters, digits, `_` or `-` and be at most 128 characters" .to_string(), ); } } if self.key_prefix.trim().is_empty() { return Err("event.actor.key_prefix must not be blank".to_string()); } for (name, value) in [ ("call_timeout_secs", self.call_timeout_secs), ("lease_ttl_secs", self.lease_ttl_secs), ("read_block_ms", self.read_block_ms), ] { if value == 0 { return Err(format!("event.actor.{name} must be greater than zero")); } } if self.idle_timeout_secs == Some(0) { return Err("event.actor.idle_timeout_secs must be greater than zero".to_string()); } if self.mailbox_capacity == 0 || self.inbox_max_len == 0 || self.read_count == 0 { return Err( "event.actor.mailbox_capacity/inbox_max_len/read_count must be greater than zero" .to_string(), ); } Ok(()) } } impl Default for ActorConfig { fn default() -> Self { Self { node_id: None, key_prefix: DEFAULT_KEY_PREFIX.to_string(), call_timeout_secs: DEFAULT_CALL_TIMEOUT_SECS, idle_timeout_secs: Some(DEFAULT_IDLE_TIMEOUT_SECS), lease_ttl_secs: DEFAULT_LEASE_TTL_SECS, mailbox_capacity: DEFAULT_MAILBOX_CAPACITY, inbox_max_len: DEFAULT_INBOX_MAX_LEN, read_count: DEFAULT_READ_COUNT, read_block_ms: DEFAULT_READ_BLOCK_MS, } } }