Skip to content

Commit

Permalink
fixup! Add on approach overview toward actors
Browse files Browse the repository at this point in the history
  • Loading branch information
didier-wenzek committed Nov 16, 2022
1 parent 2a10aca commit b705940
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions design/thin-edge-actors-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ impl A {
peer.say("that").await;
}
}

struct B { state: u64 }

impl B {
pub async fn say(&mut self, arg: &str) {
self.state += 1;
println!("{}: {}", self.state, arg);
}
}
```

Moving to an actor model introduces two ideas:
Expand All @@ -255,7 +264,7 @@ Moving to an actor model introduces two ideas:
* the peers clone the channel sender and send messages for processing.

```rust
struct ActorA { state: AState, messages: Receiver<AMessage>, peer: Sender<BMessage> }
struct ActorA { state: u64, messages: Receiver<AMessage>, peer: Sender<BMessage> }

#[derive(Clone, Debug)]
enum AMessage {
Expand All @@ -272,14 +281,34 @@ impl A {
self.state += 1;

// send messages to self.peer, triggering asynchronous operations
let _ = self.peer.send("this").await;
let _ = self.peer.send(BMessage::Say("this".to_string())).await;
}
DoThat(arg) => {
// update self.state
self.state += 1;

// send messages to self.peer, triggering asynchronous operations
let _ = self.peer.send("that").await;
let _ = self.peer.send(BMessage::Say("that".to_string())).await;
}
}
}
}
}

struct ActorB { state: u64, messages: Receiver<BMessage> }

#[derive(Clone, Debug)]
enum BMessage {
Say(String),
}

impl B {
pub async fn run(mut self) {
while let Some(message) = self.messages.recv().await {
match message {
Say(arg) => {
self.state += 1;
println!("{}: {}", self.state, &arg);
}
}
}
Expand Down Expand Up @@ -339,6 +368,38 @@ impl A {
self.peer.say("that").await;
}
}

struct ActorHandlerB {
sender: Sender<BMessage>
}

impl ActorHandlerB {
pub async fn say(&mut self, arg: &str) {
let _ = self.sender.send(BMessage::Say(arg.to_string())).await;
}
}

struct ActorB { state: u64, messages: Receiver<BMessage> }

#[derive(Clone, Debug)]
enum BMessage {
Say(String),
}

impl B {
pub async fn run(mut self) {
while let Some(message) = self.messages.recv().await {
match message {
Say(arg) => self.say(arg),
}
}
}

async fn say(&mut self, arg: String) {
self.state += 1;
println!("{}: {}", self.state, &arg);
}
}
```

These three variants of the same example highlight several points.
Expand Down

0 comments on commit b705940

Please sign in to comment.