Skip to content

Technical Component Specification: EventBus

Overview

The EventBus serves as a central event system to be used with the OpsML server to record events. It provides asynchronous event distribution using Tokio's broadcast channel, and it enables decoupled communication between different parts of the system, particularly for audit logging and monitoring.

Component Definition

#[derive(Clone)]
pub struct EventBus {
    tx: broadcast::Sender<Event>,
}

pub enum Event {
    Audit(AuditEvent),
    // Extensible for future event types
}

Core Responsibilities

  1. Event Distribution
  2. Asynchronous event broadcasting
  3. Multiple subscriber support
  4. Non-blocking event publishing
  5. Event type management

  6. Stream Management

  7. Stream-based event subscription
  8. Automatic error filtering
  9. Broadcast channel capacity control
  10. Subscriber lifecycle management

  11. Event Type Handling

  12. Support for different event types
  13. Type-safe event distribution
  14. Event filtering capabilities
  15. Extensible event system

Key Methods

Constructor

pub fn new(capacity: usize) -> Self {
    let (tx, _) = broadcast::channel(capacity);
    Self { tx }
}

Core Operations

pub fn publish(&self, event: Event)
pub fn subscribe(&self) -> impl Stream<Item = Event>

Dependencies

  • External Crates
  • tokio: Async runtime and broadcast channel
  • futures: Stream trait implementations
  • tokio-stream: Stream wrappers
  • tracing: Logging and instrumentation

  • Internal Components

  • Event: Event type enum
  • AuditEvent: Audit event structure
  • EventError: Error handling

Error Handling

  • Silent error handling for send operations
  • Stream filtering for failed receives
  • Debug logging for event operations
  • Custom EventError type

Thread Safety

  • Thread-safe event broadcasting
  • Clone-able event bus instance
  • Safe multi-subscriber support
  • Atomic broadcast operations

Performance Considerations

  1. Channel Capacity
  2. Configurable buffer size

  3. Concurrency

  4. Non-blocking operations
  5. Multiple concurrent subscribers
  6. Efficient event distribution

  7. Resource Management

  8. Automatic cleanup of dropped subscribers (via Tokio)
  9. Pattern matching for event types

Future Considerations

  1. Event persistence options
  2. Priority-based event handling
  3. Event batching capabilities
  4. Enhanced filtering mechanisms
  5. Subscriber backpressure handling
  6. Event replay functionality

Version: 1.0
Last Updated: 2025-04-04
Component Owner: Steven Forrester