anycms-event 0.1.5

A type-safe, async event bus system for AnyCMS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//! The core [`EventBus`] implementation.
//!
//! Uses `tokio::broadcast` channels internally for efficient fan-out
//! pub/sub semantics with back-pressure support.

use std::any::Any;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use dashmap::DashMap;

use tokio::sync::broadcast;
use tokio::task::AbortHandle;
use tokio::task::JoinHandle;

use crate::error::{EventBusError, PublishErrorReason, Result};
use crate::event::Event;
use crate::registry::EventRegistry;
use crate::execution_log::ExecutionLog;
use crate::telemetry::Telemetry;

/// Type-erased event payload. Events are wrapped in `Arc<dyn Any + Send + Sync>`
/// so they can be sent through broadcast channels without serialization.
type ErasedEvent = Arc<dyn Any + Send + Sync>;

/// Callback invoked after an event is published, receiving the event name
/// and its JSON representation. Used by observers like `TriggerRuleEngine`.
type PublishCallback = Arc<dyn Fn(&str, serde_json::Value) + Send + Sync>;

/// Wrapper used on the global dispatch channel.
/// Carries the event name (used for pattern matching) alongside the type-erased payload.
#[derive(Clone)]
struct GlobalEvent {
    event_name: String,
    payload: ErasedEvent,
}

// ── Retry & Dead Letter Types ──────────────────────────────────────

/// 重试退避策略。
#[derive(Clone, Debug)]
pub enum RetryBackoff {
    /// 固定间隔。
    Fixed(Duration),
    /// 指数退避(delay = base * 2^attempt,不超过 max)。
    Exponential { base: Duration, max: Duration },
}

impl Default for RetryBackoff {
    fn default() -> Self {
        Self::Exponential {
            base: Duration::from_millis(100),
            max: Duration::from_secs(10),
        }
    }
}

/// Handler 执行重试策略。
#[derive(Clone, Debug)]
pub struct RetryPolicy {
    /// 最大重试次数(0 = 不重试,默认)。
    pub max_retries: usize,
    /// 退避策略。
    pub backoff: RetryBackoff,
    /// 每次执行超时时间。
    pub timeout_per_attempt: Duration,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_retries: 0, // 默认不重试,保持向后兼容
            backoff: RetryBackoff::default(),
            timeout_per_attempt: Duration::from_secs(30),
        }
    }
}

impl RetryPolicy {
    /// 计算第 N 次重试前的等待时间。
    pub fn delay_for_attempt(&self, attempt: usize) -> Duration {
        match &self.backoff {
            RetryBackoff::Fixed(d) => *d,
            RetryBackoff::Exponential { base, max } => {
                let exp = 2u32.saturating_pow(attempt.min(16) as u32);
                base.saturating_mul(exp as u32).min(*max)
            }
        }
    }
}

/// 死信处理器 — 当 Handler 重试耗尽后调用。
pub trait DeadLetterHandler: Send + Sync + 'static {
    /// 事件处理彻底失败时调用。
    fn on_dead_letter(&self, event_name: &str, attempts: usize, error: &str);
}

/// 默认死信处理器 — 使用 tracing::error! 记录。
pub struct LoggingDeadLetterHandler;

impl DeadLetterHandler for LoggingDeadLetterHandler {
    fn on_dead_letter(&self, event_name: &str, attempts: usize, error: &str) {
        tracing::error!(
            event = event_name,
            attempts = attempts,
            error = error,
            "Event handler failed after all retries (dead letter)"
        );
    }
}

/// A subscription handle that allows unsubscribing or graceful shutdown.
pub struct Subscription {
    /// The event name or pattern this subscription is bound to.
    pub event_name: String,
    /// Unique subscription identifier.
    pub id: usize,
    /// Abort handle for cancelling the subscriber task.
    abort_handle: AbortHandle,
    /// Reference to inner state for task cleanup on unsubscribe.
    inner: Arc<EventBusInner>,
}

impl std::fmt::Debug for Subscription {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Subscription")
            .field("event_name", &self.event_name)
            .field("id", &self.id)
            .field("is_finished", &self.abort_handle.is_finished())
            .finish()
    }
}

impl Subscription {
    /// Unsubscribe by aborting the background handler task.
    ///
    /// After calling this, the handler will no longer receive events.
    /// This is idempotent — calling it multiple times has no effect.
    pub fn unsubscribe(&self) {
        self.abort_handle.abort();
        self.inner.tasks.remove(&self.id);
    }

    /// Check if this subscription is still active.
    pub fn is_finished(&self) -> bool {
        self.abort_handle.is_finished()
    }
}

/// Thread-safe, async event bus backed by tokio broadcast channels.
///
/// # Overview
///
/// The `EventBus` provides a publish/subscribe pattern where:
/// - **Publishers** send typed events via [`EventBus::publish`].
/// - **Subscribers** register async handlers via [`EventBus::subscribe`].
/// - Events are type-erased via `Arc<dyn Any + Send + Sync>` and sent through
///   broadcast channels without serialization.
/// - Each event type gets its own dedicated broadcast channel.
/// - Pattern subscriptions use a global channel that receives all published events.
///
/// # Thread Safety
///
/// Channel bookkeeping uses `std::sync::RwLock` for fast, non-blocking
/// HashMap lookups on the hot path. The bus is wrapped in `Arc`, making it
/// safe to share across tasks. It implements `Clone` (cheap reference clone)
/// and `Send + Sync`.
///
/// # Example
///
/// ```ignore
/// use anycms_event::prelude::*;
///
/// #[derive(Clone, Debug)]
/// struct UserCreated { name: String }
///
/// impl Event for UserCreated {
///     fn event_name() -> &'static str { "user.created" }
/// }
///
/// let bus = EventBus::new();
///
/// bus.subscribe(|event: UserCreated| async move {
///     println!("New user: {}", event.name);
///     Ok(())
/// }).await?;
///
/// bus.publish(UserCreated { name: "Alice".into() }).await?;
/// ```
pub struct EventBus {
    inner: Arc<EventBusInner>,
}

struct EventBusInner {
    /// Broadcast channels keyed by event name.
    channels: DashMap<String, broadcast::Sender<ErasedEvent>>,
    /// Global channel: every published event is also sent here.
    /// Pattern subscribers listen on this channel and filter with `topic::matches`.
    global_channel: broadcast::Sender<GlobalEvent>,
    /// Registered topic patterns and the event names they match.
    topic_patterns: DashMap<String, Vec<String>>,
    /// Monotonically increasing subscription ID counter.
    next_sub_id: AtomicUsize,
    /// Capacity for new broadcast channels.
    capacity: usize,
    /// 可插拔的遥测层,用于监控发布/订阅生命周期。
    telemetry: Option<Arc<dyn Telemetry>>,
    /// 事件注册表,跟踪已注册的事件类型及其元数据。
    registry: Arc<EventRegistry>,
    /// 执行日志,用于查询事件发布和 Handler 执行的历史记录。
    execution_log: Option<Arc<ExecutionLog>>,
    /// Publish callbacks invoked after each event is published.
    /// Used by observers like `TriggerRuleEngine` that need to see all events.
    publish_callbacks: RwLock<Vec<PublishCallback>>,
    /// Track spawned subscriber tasks for graceful shutdown.
    tasks: DashMap<usize, JoinHandle<()>>,
    /// 默认重试策略。
    retry_policy: RetryPolicy,
    /// 死信处理器。
    dead_letter: Option<Arc<dyn DeadLetterHandler>>,
}

impl EventBus {
    /// Create a new event bus with the default channel capacity (1024).
    pub fn new() -> Self {
        Self::from_builder(
            1024, None, Arc::new(EventRegistry::new()), None,
            RetryPolicy::default(), None,
        )
    }

    /// Create a new event bus with the specified broadcast channel capacity.
    ///
    /// The capacity controls how many messages can be buffered before slow
    /// subscribers start being lagged (dropping old messages).
    pub fn with_capacity(capacity: usize) -> Self {
        Self::from_builder(
            capacity, None, Arc::new(EventRegistry::new()), None,
            RetryPolicy::default(), None,
        )
    }

    /// 从构建器参数创建 EventBus(内部方法)。
    pub(crate) fn from_builder(
        capacity: usize,
        telemetry: Option<Arc<dyn Telemetry>>,
        registry: Arc<EventRegistry>,
        execution_log: Option<Arc<ExecutionLog>>,
        retry_policy: RetryPolicy,
        dead_letter: Option<Arc<dyn DeadLetterHandler>>,
    ) -> Self {
        let (global_tx, _) = broadcast::channel(capacity);
        Self {
            inner: Arc::new(EventBusInner {
                channels: DashMap::new(),
                global_channel: global_tx,
                topic_patterns: DashMap::new(),
                next_sub_id: AtomicUsize::new(0),
                capacity,
                telemetry,
                registry,
                execution_log,
                publish_callbacks: RwLock::new(Vec::new()),
                tasks: DashMap::new(),
                retry_policy,
                dead_letter,
            }),
        }
    }

    /// 返回一个 [`EventBusBuilder`] 用于配置 EventBus。
    pub fn builder() -> crate::builder::EventBusBuilder {
        crate::builder::EventBusBuilder::new()
    }

    /// 获取事件注册表引用。
    ///
    /// 注册表跟踪所有已发布/订阅的事件类型及其元数据。
    pub fn registry(&self) -> &Arc<EventRegistry> {
        &self.inner.registry
    }

    /// 获取执行日志引用(如果已配置)。
    ///
    /// 执行日志记录了事件发布和 Handler 执行的历史。
    pub fn execution_log(&self) -> Option<&Arc<ExecutionLog>> {
        self.inner.execution_log.as_ref()
    }

    /// Register a publish callback that is invoked after every event is published.
    ///
    /// The callback receives the event name and its JSON representation.
    /// Events that return `None` from [`Event::to_json`] will not trigger callbacks.
    ///
    /// This is the primary mechanism for `TriggerRuleEngine` to observe all events
    /// without needing a typed subscription.
    pub fn register_publish_callback(&self, callback: PublishCallback) {
        self.inner.publish_callbacks.write().unwrap().push(callback);
    }

    /// Get or create a broadcast channel for the given event type.
    ///
    /// Uses DashMap's atomic entry API for lock-free access on the hot path.
    fn get_or_create_channel<E: Event>(&self) -> broadcast::Sender<ErasedEvent> {
        self.inner
            .channels
            .entry(E::event_name().to_string())
            .or_insert_with(|| {
                // Auto-register in registry when channel is first created
                if !self.inner.registry.contains(E::event_name()) {
                    self.inner.registry.register_simple(E::event_name(), E::topic());
                }
                broadcast::channel(self.inner.capacity).0
            })
            .clone()
    }

    /// Publish a typed event to the bus.
    ///
    /// The event is type-erased via `Arc<dyn Any + Send + Sync>` and sent
    /// through the broadcast channel associated with its event type. If there
    /// are no subscribers, the publish is a no-op (not an error).
    ///
    /// The event is also sent to the global channel for pattern subscribers.
    ///
    /// # Errors
    ///
    /// Returns [`EventBusError::PublishFailed`] if the channel returns an
    /// unexpected error.
    pub async fn publish<E: Event>(&self, event: E) -> Result<()> {
        let start = std::time::Instant::now();

        // Auto-register event in registry if not yet registered
        if !self.inner.registry.contains(E::event_name()) {
            self.inner.registry.register_simple(E::event_name(), E::topic());
        }
        self.inner.registry.increment_publish_count(E::event_name());

        // Extract JSON for publish callbacks (before moving event)
        let event_json = event.to_json();

        // Fast path: check if channel exists with DashMap
        let sender = {
            match self.inner.channels.get(E::event_name()) {
                Some(sender) if sender.receiver_count() > 0 => Some(sender.clone()),
                Some(_) => {
                    // Channel exists but no subscribers
                    tracing::debug!(
                        event = E::event_name(),
                        receivers = 0,
                        "Event published (no subscribers)"
                    );
                    // Still send to global channel below
                    None
                }
                None => {
                    // Channel doesn't exist, no subscribers ever registered
                    tracing::debug!(
                        event = E::event_name(),
                        receivers = 0,
                        "Event published (no channel)"
                    );
                    // Still send to global channel below
                    None
                }
            }
        };

        if let Some(sender) = sender {
            // Type-erase the event
            let payload: ErasedEvent = Arc::new(event.clone());
            let receiver_count = sender.receiver_count();

            // Telemetry: publish started
            if let Some(ref tel) = self.inner.telemetry {
                tel.on_publish(E::event_name(), receiver_count);
            }

            sender
                .send(payload)
                .map_err(|e| EventBusError::PublishFailed {
                    event_name: E::event_name(),
                    reason: PublishErrorReason::ChannelError(e.to_string()),
                })?;

            tracing::debug!(
                event = E::event_name(),
                receivers = receiver_count,
                "Event published"
            );

            // Telemetry: publish complete
            if let Some(ref tel) = self.inner.telemetry {
                tel.on_publish_complete(E::event_name(), start.elapsed());
            }

            // Also publish to the global channel for pattern subscribers
            let global_event = GlobalEvent {
                event_name: E::event_name().to_string(),
                payload: Arc::new(event) as ErasedEvent,
            };
            let _ = self.inner.global_channel.send(global_event);
        } else {
            // No per-event-type subscribers, but still publish to global channel
            let global_event = GlobalEvent {
                event_name: E::event_name().to_string(),
                payload: Arc::new(event) as ErasedEvent,
            };
            let _ = self.inner.global_channel.send(global_event);
        }

        // Notify publish callbacks (for trigger engine etc.)
        if let Some(json) = event_json {
            let callbacks = self.inner.publish_callbacks.read().unwrap();
            for cb in callbacks.iter() {
                cb(E::event_name(), json.clone());
            }
        }

        Ok(())
    }

    /// Subscribe to a specific event type with an async handler.
    ///
    /// The handler is spawned as a background tokio task that listens for
    /// events on the broadcast channel. If the handler falls behind,
    /// lagged messages are logged as warnings but the subscriber continues.
    ///
    /// Uses the bus-level [`RetryPolicy`] (default: no retry).
    /// For custom retry, use [`EventBus::subscribe_with_retry`].
    ///
    /// # Type Parameters
    ///
    /// - `E`: The event type to subscribe to (must implement [`Event`]).
    /// - `F`: The handler closure type.
    /// - `Fut`: The future returned by the handler closure.
    ///
    /// # Returns
    ///
    /// A [`Subscription`] handle with the event name and a unique ID.
    /// The subscription can be used to unsubscribe via [`Subscription::unsubscribe`].
    pub async fn subscribe<E, F, Fut>(&self, handler: F) -> Result<Subscription>
    where
        E: Event,
        F: Fn(E) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<()>> + Send + 'static,
    {
        self.subscribe_internal(handler, self.inner.retry_policy.clone()).await
    }

    /// Subscribe with a custom retry policy (overrides the bus default).
    pub async fn subscribe_with_retry<E, F, Fut>(
        &self,
        handler: F,
        retry_policy: RetryPolicy,
    ) -> Result<Subscription>
    where
        E: Event,
        F: Fn(E) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<()>> + Send + 'static,
    {
        self.subscribe_internal(handler, retry_policy).await
    }

    /// Internal subscribe implementation with configurable retry.
    async fn subscribe_internal<E, F, Fut>(
        &self,
        handler: F,
        retry_policy: RetryPolicy,
    ) -> Result<Subscription>
    where
        E: Event,
        F: Fn(E) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<()>> + Send + 'static,
    {
        let sender = self.get_or_create_channel::<E>();
        let mut rx = sender.subscribe();

        let event_name = E::event_name().to_string();
        let id = self.inner.next_sub_id.fetch_add(1, Ordering::Relaxed);

        // Update registry subscriber count
        let sub_count = sender.receiver_count();
        self.inner.registry.set_subscriber_count(&event_name, sub_count);

        // Clone telemetry Arc for use inside the spawned task
        let telemetry = self.inner.telemetry.clone();
        let dead_letter = self.inner.dead_letter.clone();

        let handler_event_name = event_name.clone();
        let handle: JoinHandle<()> = tokio::spawn(async move {
            loop {
                match rx.recv().await {
                    Ok(payload) => {
                        match payload.downcast_ref::<E>() {
                            Some(event) => {
                                // Telemetry: handler start
                                if let Some(ref tel) = telemetry {
                                    tel.on_handler_start(&handler_event_name, id);
                                }
                                let handler_start = std::time::Instant::now();

                                // Retry loop
                                let mut last_error_str = None;
                                let mut success = false;
                                for attempt in 0..=retry_policy.max_retries {
                                    if attempt > 0 {
                                        let delay = retry_policy.delay_for_attempt(attempt - 1);
                                        tracing::debug!(
                                            event = %handler_event_name,
                                            attempt = attempt + 1,
                                            delay_ms = delay.as_millis(),
                                            "Retrying handler"
                                        );
                                        tokio::time::sleep(delay).await;
                                    }

                                    match tokio::time::timeout(
                                        retry_policy.timeout_per_attempt,
                                        handler(event.clone()),
                                    ).await {
                                        Ok(Ok(())) => {
                                            success = true;
                                            break;
                                        }
                                        Ok(Err(e)) => {
                                            last_error_str = Some(e.to_string());
                                            tracing::warn!(
                                                event = %handler_event_name,
                                                attempt = attempt + 1,
                                                max_retries = retry_policy.max_retries,
                                                error = %e,
                                                "Handler failed"
                                            );
                                        }
                                        Err(_) => {
                                            last_error_str = Some("Handler timeout".to_string());
                                            tracing::warn!(
                                                event = %handler_event_name,
                                                attempt = attempt + 1,
                                                "Handler timed out"
                                            );
                                        }
                                    }
                                }

                                let handler_elapsed = handler_start.elapsed();

                                // Dead letter if all retries exhausted
                                if !success {
                                    if let Some(ref dl) = dead_letter {
                                        dl.on_dead_letter(
                                            &handler_event_name,
                                            retry_policy.max_retries + 1,
                                            last_error_str.as_deref().unwrap_or("unknown"),
                                        );
                                    }
                                }

                                // Telemetry: handler complete
                                if let Some(ref tel) = telemetry {
                                    let err_str = if success {
                                        None
                                    } else {
                                        last_error_str
                                    };
                                    tel.on_handler_complete(
                                        &handler_event_name,
                                        id,
                                        handler_elapsed,
                                        err_str.as_deref(),
                                    );
                                }
                            }
                            None => {
                                tracing::error!(
                                    event = %handler_event_name,
                                    "Failed to downcast event"
                                );
                            }
                        }
                    }
                    Err(broadcast::error::RecvError::Lagged(n)) => {
                        tracing::warn!(
                            event = %handler_event_name,
                            lagged = n,
                            "Subscriber lagged behind"
                        );
                        // Telemetry: handler lagged
                        if let Some(ref tel) = telemetry {
                            tel.on_handler_lagged(&handler_event_name, id, n as usize);
                        }
                    }
                    Err(broadcast::error::RecvError::Closed) => {
                        tracing::debug!(
                            event = %handler_event_name,
                            "Channel closed, stopping subscriber"
                        );
                        break;
                    }
                }
            }
        });

        let abort_handle = handle.abort_handle();

        // Store JoinHandle for graceful shutdown
        self.inner.tasks.insert(id, handle);

        // Telemetry: subscriber registered
        if let Some(ref tel) = self.inner.telemetry {
            tel.on_subscribe(&event_name, id);
        }

        Ok(Subscription {
            event_name,
            id,
            abort_handle,
            inner: self.inner.clone(),
        })
    }

    /// Subscribe to a topic pattern with wildcard support.
    ///
    /// Patterns support:
    /// - `*` matches a single segment (e.g., `"user.*"` matches `"user.created"`)
    /// - `**` matches multiple segments (e.g., `"user.**"` matches `"user.foo.bar"`)
    /// - Exact match when no wildcards are present
    ///
    /// Pattern subscribers listen on the global channel and filter events
    /// using [`crate::topic::matches`]. This allows a single subscriber to
    /// receive events of different types that share a topic namespace.
    ///
    /// Uses the bus-level [`RetryPolicy`] (default: no retry).
    /// For custom retry, use [`EventBus::subscribe_pattern_with_retry`].
    pub async fn subscribe_pattern<E, F, Fut>(
        &self,
        pattern: &str,
        handler: F,
    ) -> Result<Subscription>
    where
        E: Event,
        F: Fn(E) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<()>> + Send + 'static,
    {
        self.subscribe_pattern_internal(pattern, handler, self.inner.retry_policy.clone()).await
    }

    /// Subscribe to a pattern with a custom retry policy.
    pub async fn subscribe_pattern_with_retry<E, F, Fut>(
        &self,
        pattern: &str,
        handler: F,
        retry_policy: RetryPolicy,
    ) -> Result<Subscription>
    where
        E: Event,
        F: Fn(E) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<()>> + Send + 'static,
    {
        self.subscribe_pattern_internal(pattern, handler, retry_policy).await
    }

    /// Internal pattern subscribe implementation with configurable retry.
    async fn subscribe_pattern_internal<E, F, Fut>(
        &self,
        pattern: &str,
        handler: F,
        retry_policy: RetryPolicy,
    ) -> Result<Subscription>
    where
        E: Event,
        F: Fn(E) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<()>> + Send + 'static,
    {
        // Register the pattern for bookkeeping
        self.inner
            .topic_patterns
            .entry(pattern.to_string())
            .or_default()
            .push(E::event_name().to_string());

        let mut rx = self.inner.global_channel.subscribe();
        let event_name = E::event_name().to_string();
        let id = self.inner.next_sub_id.fetch_add(1, Ordering::Relaxed);
        let pattern_owned = pattern.to_string();
        let handler_event_name = event_name.clone();

        // Clone telemetry Arc for use inside the spawned task
        let telemetry = self.inner.telemetry.clone();
        let dead_letter = self.inner.dead_letter.clone();

        let handle: JoinHandle<()> = tokio::spawn(async move {
            loop {
                match rx.recv().await {
                    Ok(global_event) => {
                        // Filter: only process if pattern matches
                        if !crate::topic::matches(&pattern_owned, &global_event.event_name) {
                            continue;
                        }
                        match global_event.payload.downcast_ref::<E>() {
                            Some(event) => {
                                // Telemetry: handler start
                                if let Some(ref tel) = telemetry {
                                    tel.on_handler_start(&handler_event_name, id);
                                }
                                let handler_start = std::time::Instant::now();

                                // Retry loop
                                let mut last_error_str = None;
                                let mut success = false;
                                for attempt in 0..=retry_policy.max_retries {
                                    if attempt > 0 {
                                        let delay = retry_policy.delay_for_attempt(attempt - 1);
                                        tracing::debug!(
                                            event = %handler_event_name,
                                            pattern = %pattern_owned,
                                            attempt = attempt + 1,
                                            "Retrying pattern handler"
                                        );
                                        tokio::time::sleep(delay).await;
                                    }

                                    match tokio::time::timeout(
                                        retry_policy.timeout_per_attempt,
                                        handler(event.clone()),
                                    ).await {
                                        Ok(Ok(())) => {
                                            success = true;
                                            break;
                                        }
                                        Ok(Err(e)) => {
                                            last_error_str = Some(e.to_string());
                                            tracing::warn!(
                                                event = %handler_event_name,
                                                pattern = %pattern_owned,
                                                attempt = attempt + 1,
                                                error = %e,
                                                "Pattern handler failed"
                                            );
                                        }
                                        Err(_) => {
                                            last_error_str = Some("Handler timeout".to_string());
                                        }
                                    }
                                }

                                let handler_elapsed = handler_start.elapsed();

                                if !success {
                                    if let Some(ref dl) = dead_letter {
                                        dl.on_dead_letter(
                                            &handler_event_name,
                                            retry_policy.max_retries + 1,
                                            last_error_str.as_deref().unwrap_or("unknown"),
                                        );
                                    }
                                }

                                // Telemetry: handler complete
                                if let Some(ref tel) = telemetry {
                                    let err_str = if success { None } else { last_error_str };
                                    tel.on_handler_complete(
                                        &handler_event_name,
                                        id,
                                        handler_elapsed,
                                        err_str.as_deref(),
                                    );
                                }
                            }
                            None => {
                                // Event matched the pattern name but is a different type.
                                // This is expected when multiple event types share a topic.
                            }
                        }
                    }
                    Err(broadcast::error::RecvError::Lagged(n)) => {
                        tracing::warn!(
                            pattern = %pattern_owned,
                            lagged = n,
                            "Pattern subscriber lagged behind"
                        );
                        // Telemetry: handler lagged
                        if let Some(ref tel) = telemetry {
                            tel.on_handler_lagged(&handler_event_name, id, n as usize);
                        }
                    }
                    Err(broadcast::error::RecvError::Closed) => {
                        tracing::debug!(
                            pattern = %pattern_owned,
                            "Global channel closed, stopping pattern subscriber"
                        );
                        break;
                    }
                }
            }
        });

        let abort_handle = handle.abort_handle();

        // Store JoinHandle for graceful shutdown
        self.inner.tasks.insert(id, handle);

        // Telemetry: subscriber registered
        if let Some(ref tel) = self.inner.telemetry {
            tel.on_subscribe(&event_name, id);
        }

        Ok(Subscription {
            event_name,
            id,
            abort_handle,
            inner: self.inner.clone(),
        })
    }

    /// Shut down the event bus by aborting all subscriber tasks and clearing channels.
    ///
    /// All active subscriber tasks are immediately aborted. This is useful
    /// for a fast shutdown during application termination.
    pub fn shutdown(&self) {
        // Abort all subscriber tasks
        let task_ids: Vec<usize> = self.inner.tasks.iter().map(|e| *e.key()).collect();
        for id in task_ids {
            if let Some((_, handle)) = self.inner.tasks.remove(&id) {
                handle.abort();
            }
        }
        // Clear per-event channels so any remaining receivers get Closed
        self.inner.channels.clear();
    }

    /// Gracefully shut down the event bus, waiting for subscriber tasks to complete.
    ///
    /// Clears channels first so no new events arrive, then waits for all
    /// subscriber tasks to finish. If the timeout elapses before all tasks
    /// complete, returns the number of tasks that may still be running.
    ///
    /// Returns `0` on success (all tasks completed within the timeout).
    pub async fn shutdown_graceful(&self, timeout: std::time::Duration) -> usize {
        // Clear channels first so no new events arrive
        self.inner.channels.clear();

        // Collect all task handles
        let tasks: Vec<(usize, JoinHandle<()>)> = {
            let mut handles = Vec::new();
            let keys: Vec<usize> = self.inner.tasks.iter().map(|e| *e.key()).collect();
            for key in keys {
                if let Some(entry) = self.inner.tasks.remove(&key) {
                    handles.push(entry);
                }
            }
            handles
        };

        let total = tasks.len();
        let result = tokio::time::timeout(timeout, async {
            for (_, handle) in tasks {
                let _ = handle.await;
            }
        })
        .await;

        if result.is_ok() {
            0
        } else {
            // Timeout — some tasks may still be running
            total
        }
    }
}

impl Default for EventBus {
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for EventBus {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

// Note: EventBus is Send + Sync because Arc<EventBusInner> is Send + Sync.
// EventBusInner is Sync because RwLock<HashMap<...>> and AtomicUsize are Sync.