use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use serde_json::Value;
use crate::broker::{Broker, ErasedHandler, SubscriptionId};
use crate::error::EventError;
use crate::event::cloud::CloudEvent;
use crate::event::domain::{DomainEvent, QueuedCommand, RequestMessage};
use crate::event::scope::EventScope;
use crate::queue::{JobId, QueueBroker};
type TypedCommandFn = Arc<
dyn Fn(Value) -> Pin<Box<dyn Future<Output = Result<Value, EventError>> + Send>> + Send + Sync,
>;
#[derive(Clone)]
struct TypedHandler {
subscription_id: SubscriptionId,
handler: TypedCommandFn,
}
pub struct EventBus {
process_broker: Arc<dyn Broker>,
app_broker: Arc<dyn Broker>,
queue_broker: Option<Arc<dyn QueueBroker>>,
typed_handlers: Arc<std::sync::RwLock<HashMap<String, TypedHandler>>>,
subscriptions: Arc<std::sync::RwLock<HashMap<SubscriptionId, EventScope>>>,
}
#[cfg(all(test, feature = "in-process"))]
mod tests {
use super::*;
use crate::broker::disruptor::InProcessBroker;
use crate::{CommandMessage, DomainEvent, Message, QueuePolicy, QueuedCommand, QueuedJob};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::sync::{Mutex, oneshot};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct TestCommand {
value: String,
}
impl Message for TestCommand {
fn message_type() -> &'static str {
"com.test.command"
}
fn source() -> &'static str {
"/test"
}
fn scope() -> EventScope {
EventScope::Process
}
}
impl RequestMessage for TestCommand {
type Response = TestResponse;
}
impl CommandMessage for TestCommand {}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct TestEvent {
value: String,
}
impl Message for TestEvent {
fn message_type() -> &'static str {
"com.test.event"
}
fn source() -> &'static str {
"/test"
}
fn scope() -> EventScope {
EventScope::Process
}
}
impl DomainEvent for TestEvent {}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct TestResponse {
value: String,
}
impl Message for TestResponse {
fn message_type() -> &'static str {
"com.test.response"
}
fn source() -> &'static str {
"/test"
}
fn scope() -> EventScope {
EventScope::Process
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct AppCommand {
value: String,
}
impl Message for AppCommand {
fn message_type() -> &'static str {
"com.test.app-command"
}
fn source() -> &'static str {
"/test"
}
fn scope() -> EventScope {
EventScope::App
}
}
impl RequestMessage for AppCommand {
type Response = AppResponse;
}
impl CommandMessage for AppCommand {}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct AppResponse {
value: String,
}
impl Message for AppResponse {
fn message_type() -> &'static str {
"com.test.app-response"
}
fn source() -> &'static str {
"/test"
}
fn scope() -> EventScope {
EventScope::App
}
}
#[derive(Default)]
struct RecordingBroker {
published: Mutex<Vec<CloudEvent>>,
}
#[async_trait]
impl Broker for RecordingBroker {
async fn publish(&self, event: CloudEvent) -> Result<(), EventError> {
self.published.lock().await.push(event);
Ok(())
}
async fn subscribe(
&self,
_event_type: &str,
_handler: ErasedHandler,
) -> Result<SubscriptionId, EventError> {
Ok(SubscriptionId::new())
}
async fn unsubscribe(&self, _id: SubscriptionId) -> Result<(), EventError> {
Ok(())
}
async fn call(
&self,
_event: CloudEvent,
_timeout: Duration,
) -> Result<CloudEvent, EventError> {
Err(EventError::Publish("recording broker cannot call".into()))
}
async fn shutdown(&self) -> Result<(), EventError> {
Ok(())
}
}
fn test_bus() -> Arc<EventBus> {
let runtime = tokio::runtime::Handle::current();
Arc::new(EventBus::new(
Arc::new(InProcessBroker::new(256, runtime.clone())),
Arc::new(InProcessBroker::new(256, runtime)),
))
}
#[derive(Default)]
struct TestQueueBroker {
next_id: AtomicUsize,
jobs: Mutex<VecDeque<QueuedJob>>,
acked: Mutex<Vec<JobId>>,
dead_lettered: AtomicUsize,
}
#[async_trait]
impl QueueBroker for TestQueueBroker {
async fn enqueue(
&self,
_queue: &str,
event: CloudEvent,
attempts: u32,
) -> Result<JobId, EventError> {
let id = JobId(
self.next_id
.fetch_add(1, Ordering::Relaxed)
.saturating_add(1)
.to_string(),
);
self.jobs.lock().await.push_back(QueuedJob {
id: id.clone(),
event,
attempts,
});
Ok(id)
}
async fn read(
&self,
_queue: &str,
_group: &str,
_consumer: &str,
count: usize,
block_ms: u64,
) -> Result<Vec<QueuedJob>, EventError> {
let mut jobs = self.jobs.lock().await;
if jobs.is_empty() {
drop(jobs);
tokio::time::sleep(Duration::from_millis(block_ms.min(10))).await;
return Ok(Vec::new());
}
Ok((0..count).filter_map(|_| jobs.pop_front()).collect())
}
async fn claim_stale(
&self,
_queue: &str,
_group: &str,
_consumer: &str,
_count: usize,
_min_idle_ms: u64,
) -> Result<Vec<QueuedJob>, EventError> {
Ok(Vec::new())
}
async fn ack(&self, _queue: &str, _group: &str, id: &JobId) -> Result<(), EventError> {
self.acked.lock().await.push(id.clone());
Ok(())
}
async fn dead_letter(
&self,
_queue: &str,
_event: CloudEvent,
_attempts: u32,
_error: String,
) -> Result<JobId, EventError> {
self.dead_lettered.fetch_add(1, Ordering::Relaxed);
Ok(JobId("dlq".to_string()))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct QueuedTestCommand {
value: String,
}
impl Message for QueuedTestCommand {
fn message_type() -> &'static str {
"com.test.queued-command"
}
fn source() -> &'static str {
"/test"
}
fn scope() -> EventScope {
EventScope::App
}
}
impl RequestMessage for QueuedTestCommand {
type Response = TestResponse;
}
impl CommandMessage for QueuedTestCommand {}
impl QueuedCommand for QueuedTestCommand {
fn queue_policy() -> QueuePolicy {
QueuePolicy {
queue: "test.queue",
consumer_group: "test-group",
max_attempts: 2,
max_in_flight: 1,
block_ms: 10,
claim_idle_ms: 100,
}
}
}
#[tokio::test]
async fn duplicate_process_command_handler_registration_returns_error() {
let bus = test_bus();
bus.handle::<TestCommand, _, _>(|cmd| async move { Ok(TestResponse { value: cmd.value }) })
.await
.unwrap();
let result = bus
.handle::<TestCommand, _, _>(|cmd| async move { Ok(TestResponse { value: cmd.value }) })
.await;
assert!(matches!(result, Err(EventError::Subscription(_))));
}
#[tokio::test]
async fn typed_process_call_obeys_timeout() {
let bus = test_bus();
bus.handle::<TestCommand, _, _>(|cmd| async move {
tokio::time::sleep(Duration::from_millis(50)).await;
Ok(TestResponse { value: cmd.value })
})
.await
.unwrap();
let result: Result<TestResponse, EventError> = bus
.call(
&TestCommand {
value: "slow".to_string(),
},
Duration::from_millis(5),
)
.await;
assert!(matches!(result, Err(EventError::Timeout(_))));
}
#[tokio::test]
async fn unsubscribe_stops_event_delivery() {
let bus = test_bus();
let calls = Arc::new(AtomicUsize::new(0));
let subscription_id = bus
.subscribe::<TestEvent, _, _>({
let calls = Arc::clone(&calls);
move |_event| {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
})
.await
.unwrap();
bus.publish(&TestEvent {
value: "before".to_string(),
})
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(calls.load(Ordering::SeqCst), 1);
bus.unsubscribe(subscription_id).await.unwrap();
bus.publish(&TestEvent {
value: "after".to_string(),
})
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn unsubscribed_typed_handler_can_be_registered_again() {
let bus = test_bus();
let subscription_id = bus
.handle::<TestCommand, _, _>(|command| async move {
Ok(TestResponse {
value: command.value,
})
})
.await
.unwrap();
bus.unsubscribe(subscription_id).await.unwrap();
bus.handle::<TestCommand, _, _>(|command| async move {
Ok(TestResponse {
value: format!("{}-new", command.value),
})
})
.await
.unwrap();
let response = bus
.call(
&TestCommand {
value: "handler".to_string(),
},
Duration::from_secs(1),
)
.await
.unwrap();
assert_eq!(response.value, "handler-new");
}
#[tokio::test]
async fn app_command_response_is_published_to_app_scope() {
let app_broker = Arc::new(RecordingBroker::default());
let runtime = tokio::runtime::Handle::current();
let bus = Arc::new(EventBus::new(
Arc::new(InProcessBroker::new(256, runtime)),
app_broker.clone(),
));
let handler = EventBus::make_command_handler::<AppCommand, _, _>(
Arc::clone(&bus),
|command| async move {
Ok(AppResponse {
value: command.value,
})
},
);
let mut request = CloudEvent::from_message(&AppCommand {
value: "app".to_string(),
})
.unwrap();
request
.extensions
.insert("reply_to".into(), Value::String("_reply.test".to_string()));
request.extensions.insert(
"correlationid".into(),
Value::String("test-correlation".to_string()),
);
handler(request).await.unwrap();
let published = app_broker.published.lock().await;
assert_eq!(published.len(), 1);
assert_eq!(published[0].scope, EventScope::App);
assert_eq!(published[0].r#type, "_reply.test");
}
#[tokio::test]
async fn queued_command_worker_consumes_and_acks_job() {
let runtime = tokio::runtime::Handle::current();
let queue = Arc::new(TestQueueBroker::default());
let bus = Arc::new(EventBus::new_with_queue(
Arc::new(InProcessBroker::new(256, runtime.clone())),
Arc::new(InProcessBroker::new(256, runtime)),
queue.clone(),
));
bus.enqueue(&QueuedTestCommand {
value: "queued".to_string(),
})
.await
.unwrap();
let (tx, rx) = oneshot::channel();
let tx = Arc::new(Mutex::new(Some(tx)));
let worker = bus
.work::<QueuedTestCommand, _, _>("test-consumer", {
let tx = Arc::clone(&tx);
move |cmd| {
let tx = Arc::clone(&tx);
async move {
if let Some(tx) = tx.lock().await.take() {
let _ = tx.send(cmd.value.clone());
}
Ok(TestResponse { value: cmd.value })
}
}
})
.await
.unwrap();
let received = tokio::time::timeout(Duration::from_millis(100), rx)
.await
.unwrap()
.unwrap();
assert_eq!(received, "queued");
for _ in 0..10 {
if queue.acked.lock().await.len() == 1 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_eq!(queue.acked.lock().await.len(), 1);
worker.abort();
}
#[tokio::test]
async fn queued_command_retries_then_dead_letters() {
let runtime = tokio::runtime::Handle::current();
let queue = Arc::new(TestQueueBroker::default());
let bus = Arc::new(EventBus::new_with_queue(
Arc::new(InProcessBroker::new(256, runtime.clone())),
Arc::new(InProcessBroker::new(256, runtime)),
queue.clone(),
));
bus.enqueue(&QueuedTestCommand {
value: "fails".to_string(),
})
.await
.unwrap();
let worker = bus
.work::<QueuedTestCommand, _, _>("test-consumer", |_command| async move {
Err(EventError::Publish("handler failed".to_string()))
})
.await
.unwrap();
for _ in 0..20 {
if queue.dead_lettered.load(Ordering::Relaxed) == 1 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_eq!(queue.dead_lettered.load(Ordering::Relaxed), 1);
assert_eq!(queue.acked.lock().await.len(), 2);
worker.abort();
}
}
impl EventBus {
pub fn new(process_broker: Arc<dyn Broker>, app_broker: Arc<dyn Broker>) -> Self {
Self {
process_broker,
app_broker,
queue_broker: None,
typed_handlers: Arc::new(std::sync::RwLock::new(HashMap::new())),
subscriptions: Arc::new(std::sync::RwLock::new(HashMap::new())),
}
}
pub fn new_with_queue(
process_broker: Arc<dyn Broker>,
app_broker: Arc<dyn Broker>,
queue_broker: Arc<dyn QueueBroker>,
) -> Self {
Self {
process_broker,
app_broker,
queue_broker: Some(queue_broker),
typed_handlers: Arc::new(std::sync::RwLock::new(HashMap::new())),
subscriptions: Arc::new(std::sync::RwLock::new(HashMap::new())),
}
}
pub async fn publish<E: DomainEvent>(&self, event: &E) -> Result<(), EventError> {
let mut cloud_event = CloudEvent::from_domain_event(event)?;
cloud_event.span = Some(tracing::Span::current());
self.publish_ce(cloud_event).await
}
pub async fn call<Req: RequestMessage>(
&self,
cmd: &Req,
timeout: Duration,
) -> Result<Req::Response, EventError> {
if Req::scope() == EventScope::Process {
let cmd_data =
serde_json::to_value(cmd).map_err(|e| EventError::Serialization(e.to_string()))?;
if let Some(handler) = self.typed_handler(Req::message_type()) {
let value = tokio::time::timeout(timeout, handler(cmd_data))
.await
.map_err(|_| EventError::Timeout("typed call timed out".into()))??;
return serde_json::from_value(value)
.map_err(|e| EventError::Deserialization(e.to_string()));
}
}
let cloud_cmd = CloudEvent::from_message(cmd)?;
let response_ce = self.call_ce(cloud_cmd, timeout).await?;
response_ce.into_message::<Req::Response>()
}
pub async fn subscribe<E, H, Fut>(&self, handler: H) -> Result<SubscriptionId, EventError>
where
E: DomainEvent,
H: Fn(E) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), EventError>> + Send + 'static,
{
let event_type = E::message_type();
let erased = Self::make_erased_handler(handler);
self.subscribe_ce(event_type, E::scope(), erased).await
}
pub async fn handle<Req, H, Fut>(
self: &Arc<Self>,
handler: H,
) -> Result<SubscriptionId, EventError>
where
Req: RequestMessage,
H: Fn(Req) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Req::Response, EventError>> + Send + 'static,
{
let event_type = Req::message_type();
let handler = Arc::new(handler);
let typed = if Req::scope() == EventScope::Process {
let h = Arc::clone(&handler);
let typed: TypedCommandFn = Arc::new(move |data: Value| {
let h = Arc::clone(&h);
Box::pin(async move {
let cmd: Req = serde_json::from_value(data)
.map_err(|e| EventError::Deserialization(e.to_string()))?;
let res = h(cmd).await?;
serde_json::to_value(res).map_err(|e| EventError::Serialization(e.to_string()))
})
});
Some(typed)
} else {
None
};
let erased = Self::make_command_handler(self.clone(), move |cmd| {
let handler = Arc::clone(&handler);
async move { handler(cmd).await }
});
let subscription_id = self.subscribe_ce(event_type, Req::scope(), erased).await?;
if let Some(typed) = typed
&& let Err(error) = self.register_typed_handler(event_type, subscription_id, typed)
{
let _ = self.unsubscribe(subscription_id).await;
return Err(error);
}
Ok(subscription_id)
}
pub async fn shutdown(&self) -> Result<(), EventError> {
let mut errors = Vec::new();
if let Err(e) = self.process_broker.shutdown().await {
errors.push(format!("process broker: {e}"));
}
if let Err(e) = self.app_broker.shutdown().await {
errors.push(format!("app broker: {e}"));
}
if errors.is_empty() {
Ok(())
} else {
Err(EventError::Shutdown(errors.join("; ")))
}
}
pub async fn enqueue<C: QueuedCommand>(&self, cmd: &C) -> Result<JobId, EventError> {
let queue = self
.queue_broker
.as_ref()
.ok_or_else(|| EventError::Publish("queue broker not configured".to_string()))?;
let event = CloudEvent::from_message(cmd)?;
queue.enqueue(C::queue_policy().queue, event, 0).await
}
pub async fn work<C, H, Fut>(
&self,
consumer_name: impl Into<String>,
handler: H,
) -> Result<tokio::task::JoinHandle<()>, EventError>
where
C: QueuedCommand,
H: Fn(C) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<C::Response, EventError>> + Send + 'static,
{
let queue =
self.queue_broker.as_ref().cloned().ok_or_else(|| {
EventError::Subscription("queue broker not configured".to_string())
})?;
let policy = C::queue_policy();
let consumer_name = consumer_name.into();
let handler = Arc::new(handler);
Ok(tokio::spawn(async move {
let semaphore = Arc::new(tokio::sync::Semaphore::new(policy.max_in_flight.max(1)));
loop {
let mut permits = Vec::with_capacity(policy.max_in_flight.max(1));
let Ok(first_permit) = semaphore.clone().acquire_owned().await else {
return;
};
permits.push(first_permit);
while permits.len() < policy.max_in_flight.max(1) {
let Ok(permit) = semaphore.clone().try_acquire_owned() else {
break;
};
permits.push(permit);
}
let jobs = match queue
.claim_stale(
policy.queue,
policy.consumer_group,
&consumer_name,
permits.len(),
policy.claim_idle_ms,
)
.await
{
Ok(jobs) if !jobs.is_empty() => jobs,
Ok(_) => match queue
.read(
policy.queue,
policy.consumer_group,
&consumer_name,
permits.len(),
policy.block_ms,
)
.await
{
Ok(jobs) => jobs,
Err(error) => {
tracing::error!(error = %error, queue = policy.queue, "queue read failed");
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
},
Err(error) => {
tracing::error!(error = %error, queue = policy.queue, "queue stale-claim failed");
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
};
for (job, permit) in jobs.into_iter().zip(permits.drain(..)) {
let queue = Arc::clone(&queue);
let handler = Arc::clone(&handler);
let policy = policy.clone();
tokio::spawn(async move {
let _permit = permit;
let event_for_retry = job.event.clone();
let handler_result = async {
let cmd: C = job.event.into_message::<C>()?;
handler(cmd).await?;
Ok::<(), EventError>(())
}
.await;
match handler_result {
Ok(()) => {
if let Err(error) = queue
.ack(policy.queue, policy.consumer_group, &job.id)
.await
{
tracing::error!(
error = %error,
queue = policy.queue,
job_id = %job.id.0,
"queue acknowledgement failed; job remains pending"
);
}
}
Err(error) => {
let next_attempt = job.attempts.saturating_add(1);
let persisted = if next_attempt >= policy.max_attempts.max(1) {
queue
.dead_letter(
policy.queue,
event_for_retry,
next_attempt,
error.to_string(),
)
.await
.map(|_| ())
} else {
queue
.enqueue(policy.queue, event_for_retry, next_attempt)
.await
.map(|_| ())
};
match persisted {
Ok(()) => {
if let Err(ack_error) = queue
.ack(policy.queue, policy.consumer_group, &job.id)
.await
{
tracing::error!(
error = %ack_error,
queue = policy.queue,
job_id = %job.id.0,
"failed to acknowledge replaced queue job"
);
}
}
Err(persist_error) => {
tracing::error!(
error = %persist_error,
handler_error = %error,
queue = policy.queue,
job_id = %job.id.0,
"failed to persist retry or dead letter; job remains pending"
);
}
}
}
}
});
}
}
}))
}
async fn publish_ce(&self, event: CloudEvent) -> Result<(), EventError> {
match event.scope {
EventScope::Process => self.process_broker.publish(event).await,
EventScope::App => self.app_broker.publish(event).await,
}
}
async fn subscribe_ce(
&self,
event_type: &str,
scope: EventScope,
handler: ErasedHandler,
) -> Result<SubscriptionId, EventError> {
let id = match scope {
EventScope::Process => self.process_broker.subscribe(event_type, handler).await?,
EventScope::App => self.app_broker.subscribe(event_type, handler).await?,
};
self.subscriptions.write().unwrap().insert(id, scope);
Ok(id)
}
pub async fn unsubscribe(&self, id: SubscriptionId) -> Result<(), EventError> {
let scope = *self.subscriptions.read().unwrap().get(&id).ok_or_else(|| {
EventError::Subscription(format!("unknown subscription id: {}", id.0))
})?;
match scope {
EventScope::Process => self.process_broker.unsubscribe(id).await?,
EventScope::App => self.app_broker.unsubscribe(id).await?,
}
self.subscriptions.write().unwrap().remove(&id);
self.typed_handlers
.write()
.unwrap()
.retain(|_, handler| handler.subscription_id != id);
Ok(())
}
async fn call_ce(
&self,
event: CloudEvent,
timeout: Duration,
) -> Result<CloudEvent, EventError> {
match event.scope {
EventScope::Process => self.process_broker.call(event, timeout).await,
EventScope::App => self.app_broker.call(event, timeout).await,
}
}
fn typed_handler(&self, event_type: &str) -> Option<TypedCommandFn> {
let handlers = self.typed_handlers.read().unwrap();
handlers.get(event_type).map(|typed| typed.handler.clone())
}
fn register_typed_handler(
&self,
event_type: &str,
subscription_id: SubscriptionId,
handler: TypedCommandFn,
) -> Result<(), EventError> {
let mut handlers = self.typed_handlers.write().unwrap();
if handlers.contains_key(event_type) {
return Err(EventError::Subscription(format!(
"handler already registered for {event_type}"
)));
}
handlers.insert(
event_type.to_string(),
TypedHandler {
subscription_id,
handler,
},
);
Ok(())
}
fn make_erased_handler<E, H, Fut>(handler: H) -> ErasedHandler
where
E: DomainEvent,
H: Fn(E) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), EventError>> + Send + 'static,
{
let handler = Arc::new(handler);
Arc::new(move |cloud_event: CloudEvent| {
let handler = Arc::clone(&handler);
Box::pin(async move {
let typed: E = cloud_event.into_message::<E>()?;
handler(typed).await
})
})
}
fn make_command_handler<Req, H, Fut>(eventbus: Arc<EventBus>, handler: H) -> ErasedHandler
where
Req: RequestMessage,
H: Fn(Req) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Req::Response, EventError>> + Send + 'static,
{
let handler = Arc::new(handler);
Arc::new(move |request: CloudEvent| {
let eventbus = Arc::clone(&eventbus);
let handler = Arc::clone(&handler);
Box::pin(async move {
let request_scope = request.scope;
let reply_to = request
.extensions
.get("reply_to")
.and_then(|v| v.as_str())
.map(String::from);
let correlation_id = request
.extensions
.get("correlationid")
.and_then(|v| v.as_str())
.map(String::from);
let cmd: Req = request.into_message::<Req>()?;
let result = handler(cmd).await?;
if let (Some(reply_to), Some(corr)) = (reply_to, correlation_id) {
let mut response = CloudEvent::from_message(&result)?;
response.r#type = reply_to;
response.scope = request_scope;
response
.extensions
.insert("correlationid".into(), Value::String(corr));
eventbus.publish_ce(response).await?;
}
Ok(())
})
})
}
}