pulsar_client/consumer/
raw_flow.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use pulsar_binary_protocol_spec::{
    client_channel_messages::{
        handler_reply_consumer_channel_message::HandlerReplyConsumerFlowChannelMessage,
        ConsumerSendHandlerChannelMessage,
    },
    client_half_requests::ConsumerFlowHalfRequestError,
    futures_channel::oneshot::channel,
    FlowCommand,
};
use thiserror::Error;

use super::AsyncConsumer;

#[derive(Error, Debug)]
pub enum RawFlowError {
    #[error("ConsumerChannelClosed")]
    ConsumerChannelClosed,
    #[error("RespondError {0:?}")]
    RespondError(ConsumerFlowHalfRequestError),
    #[error("ChannelClosed")]
    ChannelClosed,
}
impl AsyncConsumer {
    pub async fn raw_flow(&self, flow_command: FlowCommand) -> Result<(), RawFlowError> {
        let (sender, receiver) = channel::<HandlerReplyConsumerFlowChannelMessage>();

        self.sender
            .send(ConsumerSendHandlerChannelMessage::Flow(
                flow_command,
                sender,
            ))
            .await
            .map_err(|_| RawFlowError::ConsumerChannelClosed)?;

        match receiver.await {
            Ok(Ok(_)) => Ok(()),
            Ok(Err(err)) => Err(RawFlowError::RespondError(err)),
            Err(_) => Err(RawFlowError::ChannelClosed),
        }
    }
}