use acton_ern::Ern;
use acton_ern::ErnParser;
use crate::actor::{RestartLimiterConfig, RestartPolicy, SupervisionStrategy};
use crate::common::{BrokerRef, ParentRef};
use crate::traits::ActorHandleInterface;
#[derive(Default, Debug, Clone)]
pub struct ActorConfig {
id: Ern,
pub(crate) broker: Option<BrokerRef>,
parent: Option<ParentRef>,
inbox_capacity: Option<usize>,
restart_policy: RestartPolicy,
supervision_strategy: SupervisionStrategy,
restart_limiter_config: Option<RestartLimiterConfig>,
}
impl ActorConfig {
pub fn new(
id: Ern,
parent: Option<ParentRef>,
broker: Option<BrokerRef>,
) -> anyhow::Result<Self> {
if let Some(parent_ref) = parent {
let parent_id = ErnParser::new(parent_ref.id().to_string()).parse()?;
let child_id = parent_id + id;
Ok(Self {
id: child_id,
broker,
parent: Some(parent_ref),
inbox_capacity: None,
restart_policy: RestartPolicy::default(),
supervision_strategy: SupervisionStrategy::default(),
restart_limiter_config: None,
})
} else {
Ok(Self {
id,
broker,
parent, inbox_capacity: None,
restart_policy: RestartPolicy::default(),
supervision_strategy: SupervisionStrategy::default(),
restart_limiter_config: None,
})
}
}
#[must_use]
pub const fn with_inbox_capacity(mut self, capacity: usize) -> Self {
self.inbox_capacity = Some(capacity);
self
}
#[must_use]
pub const fn with_restart_policy(mut self, policy: RestartPolicy) -> Self {
self.restart_policy = policy;
self
}
pub fn new_with_name(name: impl Into<String>) -> anyhow::Result<Self> {
Self::new(Ern::with_root(name.into())?, None, None)
}
#[inline]
pub(crate) fn id(&self) -> Ern {
self.id.clone()
}
#[inline]
pub(crate) const fn get_broker(&self) -> Option<&BrokerRef> {
self.broker.as_ref()
}
#[inline]
pub(crate) const fn parent(&self) -> Option<&ParentRef> {
self.parent.as_ref()
}
#[inline]
pub(crate) const fn inbox_capacity(&self) -> Option<usize> {
self.inbox_capacity
}
#[inline]
pub(crate) const fn restart_policy(&self) -> RestartPolicy {
self.restart_policy
}
#[must_use]
#[deprecated(
note = "records intent only and has no runtime effect; the framework never restarts \
actors automatically. Register `mutate_on::<ChildTerminated>` on the parent and \
call `SupervisionStrategy::decide()` yourself. \
See https://github.com/govcraft/acton-reactive/issues/7"
)]
pub const fn with_supervision_strategy(mut self, strategy: SupervisionStrategy) -> Self {
self.supervision_strategy = strategy;
self
}
#[inline]
pub(crate) const fn supervision_strategy(&self) -> SupervisionStrategy {
self.supervision_strategy
}
#[must_use]
#[deprecated(
note = "records intent only and has no runtime effect; the framework never reads this \
configuration. Keep a `RestartLimiter` in the supervising actor's state and \
consult it from a `mutate_on::<ChildTerminated>` handler. \
See https://github.com/govcraft/acton-reactive/issues/7"
)]
pub const fn with_restart_limiter(mut self, config: RestartLimiterConfig) -> Self {
self.restart_limiter_config = Some(config);
self
}
#[inline]
#[allow(dead_code)] pub(crate) const fn restart_limiter_config(&self) -> Option<&RestartLimiterConfig> {
self.restart_limiter_config.as_ref()
}
}