Something went wrong. Try again.
Chris’s PDS implementation
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182//! A narrow extension seam for repository changes.
use std::{future::Future, sync::Mutex, time::Duration};
use async_trait::async_trait;
use crate::{ domain::event::RepositoryEvent, error::{Error, Result},};
/// Notifications are optional once durable state has committed. Bound the whole/// batch, including resolution, so slow listeners do not hold a write response.pub(crate) async fn best_effort_notice( label: &'static str, delivery: impl Future<Output = Result<()>>,) { match tokio::time::timeout(Duration::from_secs(2), delivery).await { Ok(Ok(())) => {} Ok(Err(error)) => tracing::warn!(%error, label, "post-commit notification failed"), Err(_) => tracing::warn!(label, "post-commit notification deadline reached"), }}
#[async_trait]pub trait EventSink: Send + Sync { async fn check(&self) -> Result<()>; async fn publish(&self, event: RepositoryEvent) -> Result<()>;}
/// The default sink makes events optional without making them implicit.pub struct NoopEventSink;
#[async_trait]impl EventSink for NoopEventSink { async fn check(&self) -> Result<()> { Ok(()) }
async fn publish(&self, _event: RepositoryEvent) -> Result<()> { Ok(()) }}
/// A useful test adapter and an example of an in-process listener.#[derive(Default)]pub struct RecordingEventSink { events: Mutex<Vec<RepositoryEvent>>,}
impl RecordingEventSink { /// Return a snapshot of every event received so far. /// /// # Errors /// /// Returns an error if another thread poisoned the recording lock. pub fn events(&self) -> Result<Vec<RepositoryEvent>> { self.events .lock() .map(|events| events.clone()) .map_err(|_| Error::Internal("recording event sink lock was poisoned".to_owned())) }}
#[async_trait]impl EventSink for RecordingEventSink { async fn check(&self) -> Result<()> { self.events .lock() .map(|_| ()) .map_err(|_| Error::Internal("recording event sink lock was poisoned".to_owned())) }
async fn publish(&self, event: RepositoryEvent) -> Result<()> { self.events .lock() .map_err(|_| Error::Internal("recording event sink lock was poisoned".to_owned()))? .push(event); Ok(()) }}