import type { Logger } from '@orchestr8/logger'; import { EventEmitter } from 'node:events'; /** * Configuration for the BoundedEventBus */ export interface EventBusConfig { /** Maximum number of events that can be queued. Default: 1000 */ maxQueueSize?: number; /** Overflow policy when queue is full. Default: 'dropOldest' */ overflowPolicy?: 'dropOldest'; /** Whether to warn when events are dropped. Default: true */ warnOnDrop?: boolean; /** Interval for calculating metrics like drop rate (ms). Default: 60000 */ metricsInterval?: number; /** Maximum listeners per event type. Default: 100 */ maxListenersPerEvent?: number; /** Enable memory tracking (has performance cost). Default: false */ enableMemoryTracking?: boolean; /** Enable backpressure handling to slow down producers. Default: false */ enableBackpressure?: boolean; /** Queue utilization threshold to trigger backpressure (0.0-1.0). Default: 0.8 */ backpressureThreshold?: number; /** Maximum delay in ms to apply during backpressure. Default: 100 */ maxBackpressureDelayMs?: number; /** Maximum events to process per batch. Default: 10 */ maxBatchSize?: number; /** Maximum time to spend processing per cycle (ms). Default: 5 */ maxProcessingTimeMs?: number; } /** * Metrics exposed by the event bus */ export interface EventBusMetrics { /** Number of events dropped due to queue overflow */ droppedCount: number; /** Timestamp of last dropped event */ lastDropTimestamp: number | null; /** Maximum queue size reached */ highWaterMark: number; /** Current queue size */ queueSize: number; /** Events dropped per minute */ dropRate: number; /** Map of event types to listener counts */ listeners: Map; /** Current backpressure status */ backpressure: { /** Whether backpressure is currently active */ isActive: boolean; /** Current utilization ratio (0.0-1.0) */ utilization: number; /** Current backpressure delay in ms */ currentDelayMs: number; /** Number of times backpressure was triggered */ triggerCount: number; }; /** Processing performance metrics */ processing: { /** Average events processed per batch */ avgBatchSize: number; /** Average processing time per batch in ms */ avgProcessingTimeMs: number; /** Total processing cycles completed */ totalCycles: number; /** Processing lag - time from emission to processing (ms) */ avgLagMs: number; }; } /** * Workflow lifecycle events */ export type WorkflowEvent = { type: 'workflow.started'; workflowId: string; timestamp: number; } | { type: 'workflow.completed'; workflowId: string; duration: number; } | { type: 'workflow.failed'; workflowId: string; error: Error; }; /** * Execution lifecycle events */ export type ExecutionEvent = { type: 'execution.queued'; executionId: string; workflowId: string; } | { type: 'execution.started'; executionId: string; } | { type: 'execution.cancelled'; executionId: string; reason: string; }; /** * Step execution events */ export type StepEvent = { type: 'step.started'; stepId: string; executionId: string; } | { type: 'step.completed'; stepId: string; output: unknown; } | { type: 'step.failed'; stepId: string; error: Error; retryable: boolean; }; /** * Resilience pattern events */ export type ResilienceEvent = { type: 'retry.attempted'; stepId: string; attempt: number; delay: number; } | { type: 'circuitBreaker.opened'; key: string; failures: number; } | { type: 'timeout.exceeded'; stepId: string; duration: number; }; /** * All orchestration events */ export type OrchestrationEvent = WorkflowEvent | ExecutionEvent | StepEvent | ResilienceEvent; /** * BoundedEventBus with queue management and overflow handling */ export declare class BoundedEventBus extends EventEmitter { private readonly config; private readonly queue; private readonly logger?; private droppedCount; private lastDropTimestamp; private highWaterMark; private lastWarnTime; private dropTimestamps; private isProcessing; private memoryEstimate; private sampleCounter; private backpressureActive; private currentBackpressureDelay; private backpressureTriggerCount; private processingCycles; private totalBatchSizes; private totalProcessingTime; private processingStartTimes; constructor(config?: EventBusConfig, logger?: Logger); /** * Emit an event synchronously (for compatibility) or asynchronously with backpressure */ emitEvent(event: OrchestrationEvent): boolean; /** * Emit an event asynchronously with backpressure handling */ emitEventAsync(event: OrchestrationEvent): Promise; /** * Emit an event synchronously (internal method) */ private emitEventSync; /** * Apply backpressure by delaying the producer when queue utilization is high */ private applyBackpressure; /** * Add event listener with type safety */ on(eventType: OrchestrationEvent['type'], listener: (event: OrchestrationEvent) => void): this; /** * Remove event listener */ off(eventType: OrchestrationEvent['type'], listener: (event: OrchestrationEvent) => void): this; /** * Get current metrics */ getMetrics(): EventBusMetrics; /** * Process queued events with batching and time-bounded processing */ private processQueue; /** * Process a batch of events with timing metrics */ private processBatch; /** * Update processing metrics for a batch of events */ private updateProcessingMetrics; /** * Emit event to listeners with error isolation */ private emitToListeners; /** * Clone event for queue isolation (hybrid approach) */ private cloneEvent; /** * Handle queue overflow according to policy */ private handleOverflow; /** * Update high water mark */ private updateHighWaterMark; /** * Calculate drop rate (events per configured interval, normalized to per minute) */ private calculateDropRate; /** * Track memory usage using sampling heuristic */ private trackMemory; /** * Estimate the size of an event in bytes */ private estimateEventSize; } //# sourceMappingURL=event-bus.d.ts.map