mirror of https://github.com/bastion-rs/bastion
add name for children (#205)
add rustfmt.toml fix two warnings Co-authored-by: Mahmut Bulut <vertexclique@gmail.com>
This commit is contained in:
parent
f27dc39c9f
commit
4af8a4864b
|
@ -164,7 +164,7 @@ pub fn stats() -> &'static Stats {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn core_retrieval() -> &'static usize {
|
pub fn core_retrieval() -> &'static usize {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CORE_COUNT: usize = { placement::get_core_ids().unwrap().len() };
|
static ref CORE_COUNT: usize = placement::get_core_ids().unwrap().len();
|
||||||
}
|
}
|
||||||
|
|
||||||
&*CORE_COUNT
|
&*CORE_COUNT
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl Sleepers {
|
||||||
|
|
||||||
if !self.notified.swap(false, Ordering::SeqCst) {
|
if !self.notified.swap(false, Ordering::SeqCst) {
|
||||||
*sleep += 1;
|
*sleep += 1;
|
||||||
self.wake.wait(sleep).unwrap();
|
let _ = self.wake.wait(sleep).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,9 +47,8 @@ fn response_supervisor(supervisor: Supervisor) -> Supervisor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn input_group(children: Children) -> Children {
|
fn input_group(children: Children) -> Children {
|
||||||
children
|
children.with_name("input").with_redundancy(1).with_exec(
|
||||||
.with_redundancy(1)
|
move |ctx: BastionContext| async move {
|
||||||
.with_exec(move |ctx: BastionContext| async move {
|
|
||||||
println!("[Input] Worker started!");
|
println!("[Input] Worker started!");
|
||||||
|
|
||||||
let data = vec!["A B C", "A C C", "B C C"];
|
let data = vec!["A B C", "A C C", "B C C"];
|
||||||
|
@ -61,11 +60,13 @@ fn input_group(children: Children) -> Children {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_group(children: Children) -> Children {
|
fn process_group(children: Children) -> Children {
|
||||||
children
|
children
|
||||||
|
.with_name("process")
|
||||||
.with_redundancy(3)
|
.with_redundancy(3)
|
||||||
.with_dispatcher(
|
.with_dispatcher(
|
||||||
// Declare a dispatcher to use. All instantiated actors will be registered in
|
// Declare a dispatcher to use. All instantiated actors will be registered in
|
||||||
|
@ -94,7 +95,7 @@ fn process_group(children: Children) -> Children {
|
||||||
*value += 1;
|
*value += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("[Processing] Worker #{:?} processed data. Result: `{:?}`", ctx.current().id(), counter);
|
println!("[Processing] Worker {} #{:?} processed data. Result: `{:?}`", ctx.current().name(), ctx.current().id(), counter);
|
||||||
|
|
||||||
// Push hashmap with data to the next actor group
|
// Push hashmap with data to the next actor group
|
||||||
let group_name = "Response".to_string();
|
let group_name = "Response".to_string();
|
||||||
|
@ -113,6 +114,7 @@ fn process_group(children: Children) -> Children {
|
||||||
|
|
||||||
fn response_group(children: Children) -> Children {
|
fn response_group(children: Children) -> Children {
|
||||||
children
|
children
|
||||||
|
.with_name("response")
|
||||||
.with_redundancy(1)
|
.with_redundancy(1)
|
||||||
.with_dispatcher(
|
.with_dispatcher(
|
||||||
// We will re-use the dispatcher to make the example easier to understand
|
// We will re-use the dispatcher to make the example easier to understand
|
||||||
|
@ -137,7 +139,7 @@ fn response_group(children: Children) -> Children {
|
||||||
let message = Arc::try_unwrap(raw_message).unwrap();
|
let message = Arc::try_unwrap(raw_message).unwrap();
|
||||||
msg! { message,
|
msg! { message,
|
||||||
ref data: HashMap<&str, u32> => {
|
ref data: HashMap<&str, u32> => {
|
||||||
println!("[Response] Worker received `{:?}`", data);
|
println!("[Response] Worker {} received `{:?}`", ctx.current().name(), data);
|
||||||
|
|
||||||
for (key, value) in data.iter() {
|
for (key, value) in data.iter() {
|
||||||
let current_value = counter.entry(key).or_insert(0);
|
let current_value = counter.entry(key).or_insert(0);
|
||||||
|
|
|
@ -16,12 +16,23 @@ use std::sync::Arc;
|
||||||
pub struct ChildRef {
|
pub struct ChildRef {
|
||||||
id: BastionId,
|
id: BastionId,
|
||||||
sender: Sender,
|
sender: Sender,
|
||||||
|
name: String,
|
||||||
path: Arc<BastionPath>,
|
path: Arc<BastionPath>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChildRef {
|
impl ChildRef {
|
||||||
pub(crate) fn new(id: BastionId, sender: Sender, path: Arc<BastionPath>) -> ChildRef {
|
pub(crate) fn new(
|
||||||
ChildRef { id, sender, path }
|
id: BastionId,
|
||||||
|
sender: Sender,
|
||||||
|
name: String,
|
||||||
|
path: Arc<BastionPath>,
|
||||||
|
) -> ChildRef {
|
||||||
|
ChildRef {
|
||||||
|
id,
|
||||||
|
sender,
|
||||||
|
name,
|
||||||
|
path,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the identifier of the children group element this
|
/// Returns the identifier of the children group element this
|
||||||
|
@ -284,6 +295,11 @@ impl ChildRef {
|
||||||
pub fn path(&self) -> &Arc<BastionPath> {
|
pub fn path(&self) -> &Arc<BastionPath> {
|
||||||
&self.path
|
&self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the [`name`] of the child
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for ChildRef {
|
impl PartialEq for ChildRef {
|
||||||
|
|
|
@ -89,6 +89,8 @@ pub struct Children {
|
||||||
started: bool,
|
started: bool,
|
||||||
// List of dispatchers attached to each actor in the group.
|
// List of dispatchers attached to each actor in the group.
|
||||||
dispatchers: Vec<Arc<Box<Dispatcher>>>,
|
dispatchers: Vec<Arc<Box<Dispatcher>>>,
|
||||||
|
// The name of children
|
||||||
|
name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Children {
|
impl Children {
|
||||||
|
@ -101,6 +103,7 @@ impl Children {
|
||||||
let pre_start_msgs = Vec::new();
|
let pre_start_msgs = Vec::new();
|
||||||
let started = false;
|
let started = false;
|
||||||
let dispatchers = Vec::new();
|
let dispatchers = Vec::new();
|
||||||
|
let name = None;
|
||||||
|
|
||||||
Children {
|
Children {
|
||||||
bcast,
|
bcast,
|
||||||
|
@ -111,6 +114,7 @@ impl Children {
|
||||||
pre_start_msgs,
|
pre_start_msgs,
|
||||||
started,
|
started,
|
||||||
dispatchers,
|
dispatchers,
|
||||||
|
name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,6 +160,14 @@ impl Children {
|
||||||
&self.callbacks
|
&self.callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn name(&self) -> String {
|
||||||
|
if let Some(name) = &self.name {
|
||||||
|
name.clone()
|
||||||
|
} else {
|
||||||
|
"__Anonymous__".into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn as_ref(&self) -> ChildrenRef {
|
pub(crate) fn as_ref(&self) -> ChildrenRef {
|
||||||
trace!(
|
trace!(
|
||||||
"Children({}): Creating new ChildrenRef({}).",
|
"Children({}): Creating new ChildrenRef({}).",
|
||||||
|
@ -171,7 +183,7 @@ impl Children {
|
||||||
for (id, (sender, _)) in &self.launched {
|
for (id, (sender, _)) in &self.launched {
|
||||||
trace!("Children({}): Creating new ChildRef({}).", self.id(), id);
|
trace!("Children({}): Creating new ChildRef({}).", self.id(), id);
|
||||||
// TODO: clone or ref?
|
// TODO: clone or ref?
|
||||||
let child = ChildRef::new(id.clone(), sender.clone(), path.clone());
|
let child = ChildRef::new(id.clone(), sender.clone(), self.name(), path.clone());
|
||||||
children.push(child);
|
children.push(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,6 +196,12 @@ impl Children {
|
||||||
ChildrenRef::new(id, sender, path, children, dispatchers)
|
ChildrenRef::new(id, sender, path, children, dispatchers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the name of this children group.
|
||||||
|
pub fn with_name(mut self, name: impl Into<String>) -> Self {
|
||||||
|
self.name = Some(name.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the closure taking a [`BastionContext`] and returning a
|
/// Sets the closure taking a [`BastionContext`] and returning a
|
||||||
/// [`Future`] that will be used by every element of this children
|
/// [`Future`] that will be used by every element of this children
|
||||||
/// group.
|
/// group.
|
||||||
|
@ -237,7 +255,7 @@ impl Children {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the number of number of elements this children group will
|
/// Sets the number of elements this children group will
|
||||||
/// contain. Each element will call the closure passed in
|
/// contain. Each element will call the closure passed in
|
||||||
/// [`with_exec`] and run the returned future until it stops,
|
/// [`with_exec`] and run the returned future until it stops,
|
||||||
/// panics or another element in the group stops or panics.
|
/// panics or another element in the group stops or panics.
|
||||||
|
@ -456,7 +474,7 @@ impl Children {
|
||||||
let id = bcast.id().clone();
|
let id = bcast.id().clone();
|
||||||
let sender = bcast.sender().clone();
|
let sender = bcast.sender().clone();
|
||||||
let path = bcast.path().clone();
|
let path = bcast.path().clone();
|
||||||
let child_ref = ChildRef::new(id.clone(), sender.clone(), path);
|
let child_ref = ChildRef::new(id.clone(), sender.clone(), self.name(), path);
|
||||||
|
|
||||||
let children = self.as_ref();
|
let children = self.as_ref();
|
||||||
let supervisor = self.bcast.parent().clone().into_supervisor();
|
let supervisor = self.bcast.parent().clone().into_supervisor();
|
||||||
|
@ -670,6 +688,8 @@ impl Children {
|
||||||
|
|
||||||
pub(crate) fn launch_elems(&mut self) {
|
pub(crate) fn launch_elems(&mut self) {
|
||||||
debug!("Children({}): Launching elements.", self.id());
|
debug!("Children({}): Launching elements.", self.id());
|
||||||
|
|
||||||
|
let name = self.name();
|
||||||
for _ in 0..self.redundancy {
|
for _ in 0..self.redundancy {
|
||||||
let parent = Parent::children(self.as_ref());
|
let parent = Parent::children(self.as_ref());
|
||||||
let bcast = Broadcast::new(parent, BastionPathElement::Child(BastionId::new()));
|
let bcast = Broadcast::new(parent, BastionPathElement::Child(BastionId::new()));
|
||||||
|
@ -678,7 +698,7 @@ impl Children {
|
||||||
let id = bcast.id().clone();
|
let id = bcast.id().clone();
|
||||||
let sender = bcast.sender().clone();
|
let sender = bcast.sender().clone();
|
||||||
let path = bcast.path().clone();
|
let path = bcast.path().clone();
|
||||||
let child_ref = ChildRef::new(id.clone(), sender.clone(), path);
|
let child_ref = ChildRef::new(id.clone(), sender.clone(), name.clone(), path);
|
||||||
|
|
||||||
let children = self.as_ref();
|
let children = self.as_ref();
|
||||||
let supervisor = self.bcast.parent().clone().into_supervisor();
|
let supervisor = self.bcast.parent().clone().into_supervisor();
|
||||||
|
|
|
@ -443,7 +443,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
assert_eq!(instance.actors.contains_key(&child_ref), false);
|
assert_eq!(instance.actors.contains_key(&child_ref), false);
|
||||||
|
|
||||||
|
@ -457,7 +458,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
instance.register(&child_ref, "my::test::module".to_string());
|
instance.register(&child_ref, "my::test::module".to_string());
|
||||||
assert_eq!(instance.actors.contains_key(&child_ref), true);
|
assert_eq!(instance.actors.contains_key(&child_ref), true);
|
||||||
|
@ -473,7 +475,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
instance.notify(&child_ref, NotificationType::Register);
|
instance.notify(&child_ref, NotificationType::Register);
|
||||||
let handler_was_called = handler.was_called();
|
let handler_was_called = handler.was_called();
|
||||||
|
@ -540,7 +543,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
let dispatcher_type = DispatcherType::Named("test".to_string());
|
let dispatcher_type = DispatcherType::Named("test".to_string());
|
||||||
let local_dispatcher = Arc::new(Box::new(Dispatcher::with_type(dispatcher_type.clone())));
|
let local_dispatcher = Arc::new(Box::new(Dispatcher::with_type(dispatcher_type.clone())));
|
||||||
|
@ -561,7 +565,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
let dispatcher_type = DispatcherType::Named("test".to_string());
|
let dispatcher_type = DispatcherType::Named("test".to_string());
|
||||||
let local_dispatcher = Arc::new(Box::new(Dispatcher::with_type(dispatcher_type.clone())));
|
let local_dispatcher = Arc::new(Box::new(Dispatcher::with_type(dispatcher_type.clone())));
|
||||||
|
@ -583,7 +588,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
let dispatcher_type = DispatcherType::Named("test".to_string());
|
let dispatcher_type = DispatcherType::Named("test".to_string());
|
||||||
let handler = Box::new(CustomHandler::new(false));
|
let handler = Box::new(CustomHandler::new(false));
|
||||||
|
@ -607,7 +613,8 @@ mod tests {
|
||||||
let bastion_id = BastionId::new();
|
let bastion_id = BastionId::new();
|
||||||
let (sender, _) = mpsc::unbounded();
|
let (sender, _) = mpsc::unbounded();
|
||||||
let path = Arc::new(BastionPath::root());
|
let path = Arc::new(BastionPath::root());
|
||||||
let child_ref = ChildRef::new(bastion_id, sender, path);
|
let name = "test_name".to_string();
|
||||||
|
let child_ref = ChildRef::new(bastion_id, sender, name, path);
|
||||||
|
|
||||||
let dispatcher_type = DispatcherType::Named("test".to_string());
|
let dispatcher_type = DispatcherType::Named("test".to_string());
|
||||||
let handler = Box::new(CustomHandler::new(false));
|
let handler = Box::new(CustomHandler::new(false));
|
||||||
|
|
Loading…
Reference in New Issue