common_meta/heartbeat/handler/
parse_mailbox_message.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 async_trait::async_trait;
16
17use crate::error::Result;
18use crate::heartbeat::handler::{
19    HandleControl, HeartbeatResponseHandler, HeartbeatResponseHandlerContext,
20};
21use crate::heartbeat::utils::mailbox_message_to_incoming_message;
22
23#[derive(Default)]
24pub struct ParseMailboxMessageHandler;
25
26#[async_trait]
27impl HeartbeatResponseHandler for ParseMailboxMessageHandler {
28    fn is_acceptable(&self, _ctx: &HeartbeatResponseHandlerContext) -> bool {
29        true
30    }
31
32    async fn handle(&self, ctx: &mut HeartbeatResponseHandlerContext) -> Result<HandleControl> {
33        if let Some(message) = &ctx.response.mailbox_message {
34            if message.payload.is_some() {
35                // mailbox_message_to_incoming_message will raise an error if payload is none
36                ctx.incoming_message = Some(mailbox_message_to_incoming_message(message.clone())?)
37            }
38        }
39
40        Ok(HandleControl::Continue)
41    }
42}