Chaining communication between actors
As we can see in Figure 15.2, our order book actor is running and accepting orders. The order book actor then sends a message to the tracker actor, updating the state once the BUY
order is processed. This means that our actor needs to manage two channels. To handle two channels, inside the src/actors.rs
file, we need to import the tracker message with the following code:
use tokio::sync::{mpsc, oneshot, mpsc::Sender}; use crate::order_tracker::TrackerMessage;
Now, we must hold two channels, resulting in our OrderBookActor
struct having the following fields:
pub struct OrderBookActor { pub receiver: mpsc::Receiver<Message>, pub sender: mpsc::Sender<TrackerMessage>, pub total_invested: f32, pub investment_cap: f32 }
Here, the fields are essentially the same, but we are holding onto a sender that sends messages to the tracker. We...