pub mod event_bus;
pub mod extension_manager;
pub mod state_actor;
pub mod system;
pub mod transaction_processor;
pub use transaction_processor::{TransactionProcessorActor, TransactionMessage};
pub use state_actor::{StateActor, StateMessage};
pub use event_bus::{EventBusActor, EventBusMessage};
pub use extension_manager::{ExtensionManagerActor, ExtensionMessage};
pub use system::{ForgeActorSystem, ActorSystemConfig};
use ractor::{SpawnErr};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::oneshot;
#[derive(Debug, Error)]
pub enum ActorSystemError {
#[error("Actor启动失败: {actor_name} - {source}")]
ActorStartupFailed { actor_name: String, source: SpawnErr },
#[error("Actor通信失败: {message}")]
CommunicationFailed { message: String },
#[error("Actor系统关闭失败: {message}")]
ShutdownFailed { message: String },
#[error("配置错误: {message}")]
ConfigurationError { message: String },
#[error("超时错误: {operation}")]
TimeoutError { operation: String },
#[error("其他错误: {message}")]
Other { message: String },
}
pub type ActorSystemResult<T> = Result<T, ActorSystemError>;
#[derive(Debug)]
pub struct MessageWrapper<T> {
pub inner: T,
pub reply_to: Option<oneshot::Sender<crate::ForgeResult<()>>>,
}
impl<T> MessageWrapper<T> {
pub fn new(inner: T) -> Self {
Self { inner, reply_to: None }
}
pub fn with_reply(
inner: T,
reply_to: oneshot::Sender<crate::ForgeResult<()>>,
) -> Self {
Self { inner, reply_to: Some(reply_to) }
}
pub fn reply(
self,
result: crate::ForgeResult<()>,
) {
if let Some(sender) = self.reply_to {
let _ = sender.send(result);
}
}
}
#[async_trait::async_trait]
pub trait ActorManager {
type Config;
type Handle;
async fn start(config: Self::Config) -> ActorSystemResult<Self::Handle>;
async fn stop(handle: Self::Handle) -> ActorSystemResult<()>;
async fn health_check(handle: &Self::Handle) -> bool;
}
pub struct ActorMetrics {
pub messages_processed: Arc<std::sync::atomic::AtomicU64>,
pub errors_count: Arc<std::sync::atomic::AtomicU64>,
pub avg_processing_time: Arc<std::sync::atomic::AtomicU64>,
}
impl Default for ActorMetrics {
fn default() -> Self {
Self {
messages_processed: Arc::new(std::sync::atomic::AtomicU64::new(0)),
errors_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
avg_processing_time: Arc::new(std::sync::atomic::AtomicU64::new(0)),
}
}
}
impl ActorMetrics {
pub fn increment_messages(&self) {
self.messages_processed
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
pub fn increment_errors(&self) {
self.errors_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
pub fn update_processing_time(
&self,
duration_ms: u64,
) {
self.avg_processing_time
.store(duration_ms, std::sync::atomic::Ordering::Relaxed);
}
}