Skip to content

Commit

Permalink
Updated children and supervisors to register and unregister themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v2d0g committed Oct 15, 2019
1 parent b9eec99 commit 82c73cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bastion/src/children.rs
@@ -1,3 +1,4 @@
use crate::bastion::REGISTRY;
use crate::broadcast::{BastionMessage, Broadcast, Sender};
use crate::context::{BastionContext, BastionId};
use futures::future::CatchUnwind;
Expand Down Expand Up @@ -70,11 +71,15 @@ impl Children {
}

async fn run(mut self) -> Self {
REGISTRY.add_children(&self);

loop {
match poll!(&mut self.bcast.next()) {
Poll::Ready(Some(msg)) => {
match msg {
BastionMessage::PoisonPill | BastionMessage::Dead { .. } | BastionMessage::Faulted { .. } => {
REGISTRY.remove_children(&self);

if msg.is_faulted() {
self.bcast.faulted();
} else {
Expand All @@ -88,6 +93,8 @@ impl Children {
}
}
Poll::Ready(None) => {
REGISTRY.remove_children(&self);

self.bcast.faulted();

return self;
Expand Down Expand Up @@ -129,8 +136,12 @@ impl Child {
}

async fn run(mut self) {
REGISTRY.add_child(&self);

loop {
if let Poll::Ready(res) = poll!(&mut self.exec) {
REGISTRY.remove_child(&self);

match res {
Ok(Ok(())) => self.bcast.dead(),
Ok(Err(())) | Err(_) => self.bcast.faulted(),
Expand All @@ -143,11 +154,15 @@ impl Child {
Poll::Ready(Some(msg)) => {
match msg {
BastionMessage::PoisonPill => {
REGISTRY.remove_child(&self);

self.bcast.dead();

return;
}
BastionMessage::Dead { .. } | BastionMessage::Faulted { .. } => {
REGISTRY.remove_child(&self);

self.bcast.faulted();

return;
Expand All @@ -157,6 +172,8 @@ impl Child {
}
}
Poll::Ready(None) => {
REGISTRY.remove_child(&self);

self.bcast.faulted();

return;
Expand Down
18 changes: 18 additions & 0 deletions bastion/src/registry.rs
Expand Up @@ -55,6 +55,24 @@ impl Registry {
self.registered.insert(id, registrant);
}

pub(super) fn remove_supervisor(&self, supervisor: &Supervisor) {
let id = supervisor.id();

self.registered.remove(id);
}

pub(super) fn remove_children(&self, children: &Children) {
let id = children.id();

self.registered.remove(id);
}

pub(super) fn remove_child(&self, child: &Child) {
let id = child.id();

self.registered.remove(id);
}

pub(super) fn send_supervisor(&self, id: &BastionId, msg: BastionMessage) -> Result<(), BastionMessage> {
let registrant = if let Some(registrant) = self.registered.get(id) {
registrant
Expand Down
10 changes: 9 additions & 1 deletion bastion/src/supervisor.rs
@@ -1,4 +1,4 @@
use crate::bastion::SYSTEM;
use crate::bastion::{REGISTRY, SYSTEM};
use crate::broadcast::{BastionMessage, Broadcast, Sender};
use crate::children::{Children, Closure, Message};
use crate::context::BastionId;
Expand Down Expand Up @@ -141,11 +141,15 @@ impl Supervisor {
}

pub(super) async fn run(mut self) -> Self {
REGISTRY.add_supervisor(&self);

loop {
match poll!(&mut self.bcast.next()) {
Poll::Ready(Some(msg)) => {
match msg {
BastionMessage::PoisonPill => {
REGISTRY.remove_supervisor(&self);

self.bcast.dead();

return self;
Expand All @@ -158,6 +162,8 @@ impl Supervisor {
}
BastionMessage::Faulted { id } => {
if self.recover(id).await.is_err() {
REGISTRY.remove_supervisor(&self);

self.bcast.faulted();

return self;
Expand All @@ -171,6 +177,8 @@ impl Supervisor {
}
}
Poll::Ready(None) => {
REGISTRY.remove_supervisor(&self);

self.bcast.faulted();

return self;
Expand Down

0 comments on commit 82c73cf

Please sign in to comment.