openraft 0.10.2

Advanced Raft consensus
Documentation
use std::future::Future;

use crate::RaftTypeConfig;
use crate::async_runtime::MpscReceiver;
use crate::async_runtime::MpscSender;
use crate::async_runtime::SendError;
use crate::async_runtime::TryRecvError;
use crate::async_runtime::mpsc::PeekableReceiver;
use crate::core::notification::Notification;
use crate::type_config::alias::MpscReceiverOf;
use crate::type_config::alias::MpscSenderOf;

/// The notification channel and self-notifications buffered during command execution.
///
/// Commands enqueue locally because [`RaftCore`] is the channel's only receiver. After each
/// command, it selects the [`Self::send_pending`] future alongside receiving and handling
/// notifications until the entire batch has entered the channel.
///
/// [`RaftCore`]: crate::core::RaftCore
pub(crate) struct NotificationChannel<C>
where C: RaftTypeConfig
{
    sender: MpscSenderOf<C, Notification<C>>,

    receiver: PeekableReceiver<MpscReceiverOf<C, Notification<C>>, Notification<C>>,

    /// Self-notifications generated by the current command, oldest first.
    pending: Vec<Notification<C>>,
}

impl<C> NotificationChannel<C>
where C: RaftTypeConfig
{
    pub(crate) fn new(sender: MpscSenderOf<C, Notification<C>>, receiver: MpscReceiverOf<C, Notification<C>>) -> Self {
        Self {
            sender,
            receiver: receiver.peekable(),
            pending: Vec::new(),
        }
    }

    /// Returns a sender for a producer that runs independently of command execution.
    pub(crate) fn sender(&self) -> MpscSenderOf<C, Notification<C>> {
        self.sender.clone()
    }

    /// Buffers a notification generated by the command being run.
    pub(crate) fn enqueue(&mut self, notification: Notification<C>) {
        self.pending.push(notification);
    }

    /// Returns whether the current command has buffered self-notifications.
    pub(crate) fn has_pending(&self) -> bool {
        !self.pending.is_empty()
    }

    /// Returns a future that sends the current batch into the channel, oldest first.
    ///
    /// The future owns the batch and sender, leaving the receiver available to free capacity.
    /// Pin it once and select pinned references so receiving never cancels a pending send.
    /// The caller must await completion before running another command.
    pub(crate) fn send_pending(&mut self) -> impl Future<Output = Result<(), SendError<Notification<C>>>> + use<C> {
        let sender = self.sender();
        let pending = std::mem::take(&mut self.pending);
        async move {
            for notification in pending {
                sender.send(notification).await?;
            }
            Ok(())
        }
    }

    pub(crate) fn try_recv(&mut self) -> Result<Notification<C>, TryRecvError> {
        self.receiver.try_recv()
    }

    pub(crate) async fn recv(&mut self) -> Option<Notification<C>> {
        self.receiver.recv().await
    }

    /// Inspects the next notification in the channel without consuming it or waiting.
    pub(crate) fn try_peek(&mut self) -> Result<&Notification<C>, TryRecvError> {
        self.receiver.try_peek()
    }
}

#[cfg(test)]
mod notification_channel_test;