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
42
43
44
45
46
47
48
49
50
use crate::async_runtime::AsyncOneshotSendExt;
use crate::raft::message::ClientWriteResult;
use crate::raft::responder::Responder;
use crate::type_config::alias::AsyncRuntimeOf;
use crate::type_config::alias::OneshotReceiverOf;
use crate::type_config::alias::OneshotSenderOf;
use crate::AsyncRuntime;
use crate::RaftTypeConfig;

/// A [`Responder`] implementation that sends the response via a oneshot channel.
///
/// This could be used when the [`Raft::client_write`] caller want to wait for the response.
///
/// [`Raft::client_write`]: `crate::raft::Raft::client_write`
pub struct OneshotResponder<C>
where C: RaftTypeConfig
{
    tx: OneshotSenderOf<C, ClientWriteResult<C>>,
}

impl<C> OneshotResponder<C>
where C: RaftTypeConfig
{
    /// Create a new instance from a [`AsyncRuntime::OneshotSender`].
    pub fn new(tx: OneshotSenderOf<C, ClientWriteResult<C>>) -> Self {
        Self { tx }
    }
}

impl<C> Responder<C> for OneshotResponder<C>
where C: RaftTypeConfig
{
    type Receiver = OneshotReceiverOf<C, ClientWriteResult<C>>;

    fn from_app_data(app_data: C::D) -> (C::D, Self, Self::Receiver)
    where Self: Sized {
        let (tx, rx) = AsyncRuntimeOf::<C>::oneshot();
        (app_data, Self { tx }, rx)
    }

    fn send(self, res: ClientWriteResult<C>) {
        let res = self.tx.send(res);

        if res.is_ok() {
            tracing::debug!("OneshotConsumer.tx.send: is_ok: {}", res.is_ok());
        } else {
            tracing::warn!("OneshotConsumer.tx.send: is_ok: {}", res.is_ok());
        }
    }
}