common_meta/heartbeat/
mailbox.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::Arc;
16
17use common_telemetry::tracing_context::TracingContext;
18use tokio::sync::mpsc::Sender;
19use tokio::sync::mpsc::error::SendError;
20
21use crate::instruction::{Instruction, InstructionReply};
22
23pub type IncomingMessage = (MessageMeta, TracingContext, Instruction);
24pub type OutgoingMessage = (MessageMeta, InstructionReply);
25
26#[derive(Debug, PartialEq, Eq, Clone)]
27pub struct MessageMeta {
28    pub id: u64,
29    pub subject: String,
30    pub to: String,
31    pub from: String,
32}
33
34impl MessageMeta {
35    #[cfg(any(test, feature = "testing"))]
36    pub fn new_test(id: u64, subject: &str, to: &str, from: &str) -> Self {
37        MessageMeta {
38            id,
39            subject: subject.to_string(),
40            to: to.to_string(),
41            from: from.to_string(),
42        }
43    }
44}
45
46pub struct HeartbeatMailbox {
47    sender: Sender<OutgoingMessage>,
48}
49
50impl HeartbeatMailbox {
51    pub fn new(sender: Sender<OutgoingMessage>) -> Self {
52        Self { sender }
53    }
54
55    pub async fn send(&self, message: OutgoingMessage) -> Result<(), SendError<OutgoingMessage>> {
56        self.sender.send(message).await
57    }
58}
59
60pub type MailboxRef = Arc<HeartbeatMailbox>;