common_meta/heartbeat/
mailbox.rsuse std::sync::Arc;
use tokio::sync::mpsc::Sender;
use crate::error::{self, Result};
use crate::instruction::{Instruction, InstructionReply};
pub type IncomingMessage = (MessageMeta, Instruction);
pub type OutgoingMessage = (MessageMeta, InstructionReply);
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MessageMeta {
pub id: u64,
pub subject: String,
pub to: String,
pub from: String,
}
impl MessageMeta {
#[cfg(any(test, feature = "testing"))]
pub fn new_test(id: u64, subject: &str, to: &str, from: &str) -> Self {
MessageMeta {
id,
subject: subject.to_string(),
to: to.to_string(),
from: from.to_string(),
}
}
}
pub struct HeartbeatMailbox {
sender: Sender<OutgoingMessage>,
}
impl HeartbeatMailbox {
pub fn new(sender: Sender<OutgoingMessage>) -> Self {
Self { sender }
}
pub async fn send(&self, message: OutgoingMessage) -> Result<()> {
self.sender.send(message).await.map_err(|e| {
error::SendMessageSnafu {
err_msg: e.to_string(),
}
.build()
})
}
}
pub type MailboxRef = Arc<HeartbeatMailbox>;