Skip to main content

acton_reactive/actor/
actor_config.rs

1/*
2 * Copyright (c) 2024. Govcraft
3 *
4 * Licensed under either of
5 *   * Apache License, Version 2.0 (the "License");
6 *     you may not use this file except in compliance with the License.
7 *     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8 *   * MIT license: http://opensource.org/licenses/MIT
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the applicable License for the specific language governing permissions and
14 * limitations under that License.
15 */
16
17use acton_ern::Ern;
18use acton_ern::ErnParser;
19
20use crate::actor::{RestartLimiterConfig, RestartPolicy, SupervisionStrategy};
21use crate::common::{BrokerRef, ParentRef};
22use crate::traits::ActorHandleInterface;
23
24/// Configuration parameters required to initialize a new actor.
25///
26/// This struct encapsulates the essential settings for creating an actor instance,
27/// including its unique identity, its relationship within the actor hierarchy (parent),
28/// and its connection to the system message broker.
29///
30/// The actor's identity is represented by an [`Ern`](acton_ern::Ern), which supports
31/// hierarchical naming. If a `parent` actor is specified during configuration, the
32/// final `Ern` of the new actor will be derived by appending its base `id` to the
33/// parent's `Ern`.
34#[derive(Default, Debug, Clone)]
35pub struct ActorConfig {
36    /// The unique identifier (`Ern`) for the actor.
37    /// If created under a parent, this will be the fully resolved hierarchical ID.
38    id: Ern,
39    /// Optional handle to the system message broker.
40    pub(crate) broker: Option<BrokerRef>,
41    /// Optional handle to the actor's parent (supervisor).
42    parent: Option<ParentRef>,
43    /// Optional custom inbox capacity for this actor.
44    /// If `None`, uses the global default from configuration.
45    inbox_capacity: Option<usize>,
46    /// The restart policy for this actor when supervised.
47    /// Defaults to `RestartPolicy::Permanent`.
48    restart_policy: RestartPolicy,
49    /// The supervision strategy for managing child actors.
50    /// Defaults to `SupervisionStrategy::OneForOne`.
51    supervision_strategy: SupervisionStrategy,
52    /// Optional restart limiter configuration for this actor.
53    /// When `Some`, the supervisor will use this configuration to limit
54    /// restart frequency and apply exponential backoff.
55    restart_limiter_config: Option<RestartLimiterConfig>,
56}
57
58impl ActorConfig {
59    /// Creates a new `ActorConfig` instance, potentially deriving a hierarchical ID.
60    ///
61    /// This constructor configures a new actor. If a `parent` handle is provided,
62    /// the actor's final `id` (`Ern`) is constructed by appending the provided `id`
63    /// segment to the parent's `Ern`. If no `parent` is provided, the `id` is used directly.
64    ///
65    /// # Arguments
66    ///
67    /// * `id` - The base identifier (`Ern`) for the actor. If `parent` is `Some`, this
68    ///   acts as the final segment appended to the parent's ID. If `parent` is `None`,
69    ///   this becomes the actor's root ID.
70    /// * `parent` - An optional [`ParentRef`] (handle) to the supervising actor.
71    /// * `broker` - An optional [`BrokerRef`] (handle) to the system message broker.
72    ///
73    /// # Returns
74    ///
75    /// Returns a `Result` containing the configured `ActorConfig` instance.
76    ///
77    /// # Errors
78    ///
79    /// Returns an error if parsing the parent's ID string into an `Ern` fails when
80    /// constructing a hierarchical ID.
81    pub fn new(
82        id: Ern,
83        parent: Option<ParentRef>,
84        broker: Option<BrokerRef>,
85    ) -> anyhow::Result<Self> {
86        if let Some(parent_ref) = parent {
87            // Use a different variable name to avoid shadowing
88            // Get the parent ERN
89            let parent_id = ErnParser::new(parent_ref.id().to_string()).parse()?;
90            let child_id = parent_id + id;
91            Ok(Self {
92                id: child_id,
93                broker,
94                parent: Some(parent_ref),
95                inbox_capacity: None,
96                restart_policy: RestartPolicy::default(),
97                supervision_strategy: SupervisionStrategy::default(),
98                restart_limiter_config: None,
99            })
100        } else {
101            Ok(Self {
102                id,
103                broker,
104                parent, // parent is None here
105                inbox_capacity: None,
106                restart_policy: RestartPolicy::default(),
107                supervision_strategy: SupervisionStrategy::default(),
108                restart_limiter_config: None,
109            })
110        }
111    }
112
113    /// Sets a custom inbox capacity for this actor.
114    ///
115    /// This allows overriding the global default inbox capacity on a per-actor basis.
116    /// High-throughput actors may benefit from larger capacities, while low-throughput
117    /// actors can use smaller capacities to conserve memory.
118    ///
119    /// # Arguments
120    ///
121    /// * `capacity` - The inbox channel capacity for this actor.
122    ///
123    /// # Returns
124    ///
125    /// Returns `self` for method chaining.
126    #[must_use]
127    pub const fn with_inbox_capacity(mut self, capacity: usize) -> Self {
128        self.inbox_capacity = Some(capacity);
129        self
130    }
131
132    /// Sets the restart policy for this actor when supervised.
133    ///
134    /// The restart policy determines how the supervisor handles actor termination:
135    /// - [`RestartPolicy::Permanent`]: Always restart (except during parent shutdown)
136    /// - [`RestartPolicy::Temporary`]: Never restart
137    /// - [`RestartPolicy::Transient`]: Restart only on abnormal termination (panic, inbox closed)
138    ///
139    /// # Arguments
140    ///
141    /// * `policy` - The restart policy to use for this actor.
142    ///
143    /// # Returns
144    ///
145    /// Returns `self` for method chaining.
146    #[must_use]
147    pub const fn with_restart_policy(mut self, policy: RestartPolicy) -> Self {
148        self.restart_policy = policy;
149        self
150    }
151
152    /// Creates a new `ActorConfig` for a top-level actor with a root identifier.
153    ///
154    /// This is a convenience function for creating an `ActorConfig` for an actor
155    /// that has no parent (i.e., it's a root actor in the hierarchy). The provided
156    /// `name` is used to create a root [`Ern`](acton_ern::Ern).
157    ///
158    /// # Arguments
159    ///
160    /// * `name` - A string-like value that will be used as the root name for the actor's `Ern`.
161    ///
162    /// # Returns
163    ///
164    /// Returns a `Result` containing the new `ActorConfig` instance with no parent or broker.
165    ///
166    /// # Errors
167    ///
168    /// Returns an error if creating the root `Ern` from the provided `name` fails
169    /// (e.g., if the name is invalid according to `Ern` rules).
170    pub fn new_with_name(name: impl Into<String>) -> anyhow::Result<Self> {
171        Self::new(Ern::with_root(name.into())?, None, None)
172    }
173
174    /// Returns a clone of the actor's unique identifier (`Ern`).
175    #[inline]
176    pub(crate) fn id(&self) -> Ern {
177        self.id.clone()
178    }
179
180    /// Returns a reference to the optional broker handle.
181    #[inline]
182    pub(crate) const fn get_broker(&self) -> Option<&BrokerRef> {
183        self.broker.as_ref()
184    }
185
186    /// Returns a reference to the optional parent handle.
187    #[inline]
188    pub(crate) const fn parent(&self) -> Option<&ParentRef> {
189        self.parent.as_ref()
190    }
191
192    /// Returns the optional custom inbox capacity for this actor.
193    ///
194    /// If `None`, the actor should use the global default from configuration.
195    #[inline]
196    pub(crate) const fn inbox_capacity(&self) -> Option<usize> {
197        self.inbox_capacity
198    }
199
200    /// Returns the restart policy for this actor.
201    #[inline]
202    pub(crate) const fn restart_policy(&self) -> RestartPolicy {
203        self.restart_policy
204    }
205
206    /// Sets the supervision strategy for managing child actors.
207    ///
208    /// The supervision strategy determines how the supervisor handles child terminations:
209    /// - [`SupervisionStrategy::OneForOne`]: Restart only the failed child
210    /// - [`SupervisionStrategy::OneForAll`]: Restart all children when one fails
211    /// - [`SupervisionStrategy::RestForOne`]: Restart the failed child and all children started after it
212    ///
213    /// # Arguments
214    ///
215    /// * `strategy` - The supervision strategy to use for this actor.
216    ///
217    /// # Returns
218    ///
219    /// Returns `self` for method chaining.
220    #[must_use]
221    pub const fn with_supervision_strategy(mut self, strategy: SupervisionStrategy) -> Self {
222        self.supervision_strategy = strategy;
223        self
224    }
225
226    /// Returns the supervision strategy for this actor.
227    #[inline]
228    pub(crate) const fn supervision_strategy(&self) -> SupervisionStrategy {
229        self.supervision_strategy
230    }
231
232    /// Sets the restart limiter configuration for this actor.
233    ///
234    /// The restart limiter controls how frequently an actor can be restarted
235    /// and applies exponential backoff between restart attempts:
236    /// - Tracks restarts within a sliding time window
237    /// - Limits the maximum number of restarts within that window
238    /// - Applies exponential backoff delays between restarts
239    ///
240    /// When the restart limit is exceeded, the supervisor should escalate
241    /// the failure to its parent rather than continuing to restart.
242    ///
243    /// # Arguments
244    ///
245    /// * `config` - The [`RestartLimiterConfig`] specifying limits and backoff parameters.
246    ///
247    /// # Returns
248    ///
249    /// Returns `self` for method chaining.
250    #[must_use]
251    pub const fn with_restart_limiter(mut self, config: RestartLimiterConfig) -> Self {
252        self.restart_limiter_config = Some(config);
253        self
254    }
255
256    /// Returns the optional restart limiter configuration for this actor.
257    ///
258    /// Used by the supervision system when creating restart limiters for child actors.
259    #[inline]
260    #[allow(dead_code)] // Will be used when supervision fully integrates restart limiting
261    pub(crate) const fn restart_limiter_config(&self) -> Option<&RestartLimiterConfig> {
262        self.restart_limiter_config.as_ref()
263    }
264}