Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat: add NotificationChannel
Lars Wirzenius committed 1 year ago
commit 2414e342d1b45c4141a91631801b7b5e269189ff
parent d5159a7400703ea991407e5b0c17b9313a3ee979
2 files changed +48 -0
modified src/lib.rs
@@ -11,6 +11,7 @@ pub mod config;
pub mod db;
pub mod event;
pub mod msg;
+
pub mod notif;
pub mod pages;
pub mod queueadd;
pub mod queueproc;
added src/notif.rs
@@ -0,0 +1,47 @@
+
//! Notification channel between threads.
+

+
use std::sync::mpsc::{channel, Receiver, Sender};
+

+
/// Channel endpoint for sending notifications.
+
pub type NotificationSender = Sender<()>;
+

+
/// Channel endpoint for receiving notifications.
+
pub type NotificationReceiver = Receiver<()>;
+

+
/// Notification channel.
+
///
+
/// The notification channel allows one thread to notify another that
+
/// the other thread has some work to do. The notification carries no
+
/// other information: the receiver is supposed to know where it can
+
/// get whatever data it needs to what it needs to do.
+
///
+
/// The point of this is to make sure threads in the CI broker
+
/// exchange data only via the database, where it is persistent.
+
pub struct NotificationChannel {
+
    tx: Option<NotificationSender>,
+
    rx: Option<NotificationReceiver>,
+
}
+

+
impl Default for NotificationChannel {
+
    fn default() -> Self {
+
        let (tx, rx) = channel();
+
        Self {
+
            tx: Some(tx),
+
            rx: Some(rx),
+
        }
+
    }
+
}
+

+
impl NotificationChannel {
+
    /// Return the transmit endpoint of the notification channel. This
+
    /// can only be called once.
+
    pub fn tx(&mut self) -> NotificationSender {
+
        self.tx.take().unwrap()
+
    }
+

+
    /// Return the receive endpoint of the notification channel. This
+
    /// can only be called once.
+
    pub fn rx(&mut self) -> NotificationReceiver {
+
        self.rx.take().unwrap()
+
    }
+
}