nautilus-common 0.56.0

Common functionality and machinery for the Nautilus trading engine
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Core message bus implementation.
//!
//! # Design decisions
//!
//! ## Why two routing mechanisms?
//!
//! The message bus provides typed and Any-based routing to balance performance
//! and flexibility:
//!
//! **Typed routing** optimizes for throughput on known data types:
//! - `TopicRouter<T>` for pub/sub, `EndpointMap<T>` for point-to-point.
//! - Handlers implement `Handler<T>`, receive `&T` directly.
//! - No runtime type checking enables inlining and static dispatch.
//! - Built-in routers: `QuoteTick`, `TradeTick`, `Bar`, `OrderBookDeltas`,
//!   `OrderBookDepth10`, `OrderEventAny`, `PositionEvent`, `AccountState`.
//!
//! **Any-based routing** provides flexibility for extensibility:
//! - `subscriptions`/`topics` maps with `ShareableMessageHandler`.
//! - Handlers implement `Handler<dyn Any>`, receive `&dyn Any`.
//! - Supports arbitrary message types without modifying the bus.
//! - Required for Python interop where types aren't known at compile time.
//!
//! ## Handler semantics
//!
//! **Typed handlers receive `&T` references:**
//! - Same message delivered to N handlers without cloning.
//! - Handler decides whether to clone (only if storing).
//! - Zero-cost for `Copy` types (`QuoteTick`, `TradeTick`, `Bar`).
//! - Efficient for large types (`OrderBookDeltas`).
//!
//! **Any-based handlers pay per-handler overhead:**
//! - Each handler receives `&dyn Any`, must downcast to `&T`.
//! - N handlers = N downcasts + N potential clones.
//! - Runtime type checking on every dispatch.
//!
//! ## Performance trade-off
//!
//! Typed routing is faster (see `benches/msgbus_typed.rs`, AMD Ryzen 9 7950X):
//!
//! | Scenario                    | Typed vs Any |
//! |-----------------------------|--------------|
//! | Handler dispatch (noop)     | ~10x faster  |
//! | Router with 5 subscribers   | ~3.5x faster |
//! | Router with 10 subscribers  | ~2x faster   |
//! | High volume (1M messages)   | ~7% faster   |
//!
//! Any-based routing pays for flexibility with runtime type checking. Use
//! typed routing for hot-path data; Any-based for custom types and Python.
//!
//! ## Routing paths are separate
//!
//! Typed and Any-based routing use separate data structures:
//! - `publish_quote` routes through `router_quotes`.
//! - `publish_any` routes through `topics`.
//!
//! Publishers and subscribers must use matching APIs. Mixing them causes
//! silent message loss.
//!
//! ## When to use each
//!
//! **Typed** (`publish_quote`, `subscribe_quotes`, etc.):
//! - Market data (quotes, trades, bars, order book updates).
//! - Order and position events.
//! - High-frequency data with known types.
//!
//! **Any-based** (`publish_any`, `subscribe_any`):
//! - Custom or user-defined data types.
//! - Low-frequency messages.
//! - Python callbacks.

use std::{
    any::{Any, TypeId},
    cell::RefCell,
    collections::HashMap,
    hash::{Hash, Hasher},
    rc::Rc,
};

use ahash::{AHashMap, AHashSet};
use indexmap::IndexMap;
use nautilus_core::{UUID4, correctness::FAILED};
use nautilus_model::{
    data::{
        Bar, Data, FundingRateUpdate, GreeksData, IndexPriceUpdate, MarkPriceUpdate,
        OrderBookDeltas, OrderBookDepth10, QuoteTick, TradeTick,
        option_chain::{OptionChainSlice, OptionGreeks},
    },
    events::{AccountState, OrderEventAny, PositionEvent},
    identifiers::TraderId,
    orderbook::OrderBook,
    orders::OrderAny,
    position::Position,
};
use smallvec::SmallVec;
use ustr::Ustr;

use super::{
    ShareableMessageHandler,
    matching::is_matching_backtracking,
    mstr::{Endpoint, MStr, Pattern, Topic},
    set_message_bus,
    switchboard::MessagingSwitchboard,
    typed_endpoints::{EndpointMap, IntoEndpointMap},
    typed_router::TopicRouter,
};
use crate::messages::{
    data::{DataCommand, DataResponse},
    execution::{ExecutionReport, TradingCommand},
};

/// Represents a subscription to a particular topic.
///
/// This is an internal class intended to be used by the message bus to organize
/// topics and their subscribers.
///
#[derive(Clone, Debug)]
pub struct Subscription {
    /// The shareable message handler for the subscription.
    pub handler: ShareableMessageHandler,
    /// Store a copy of the handler ID for faster equality checks.
    pub handler_id: Ustr,
    /// The pattern for the subscription.
    pub pattern: MStr<Pattern>,
    /// The priority for the subscription determines the ordering of handlers receiving
    /// messages being processed, higher priority handlers will receive messages before
    /// lower priority handlers.
    pub priority: u8,
}

impl Subscription {
    /// Creates a new [`Subscription`] instance.
    #[must_use]
    pub fn new(
        pattern: MStr<Pattern>,
        handler: ShareableMessageHandler,
        priority: Option<u8>,
    ) -> Self {
        Self {
            handler_id: handler.0.id(),
            pattern,
            handler,
            priority: priority.unwrap_or(0),
        }
    }
}

impl PartialEq<Self> for Subscription {
    fn eq(&self, other: &Self) -> bool {
        self.pattern == other.pattern && self.handler_id == other.handler_id
    }
}

impl Eq for Subscription {}

impl PartialOrd for Subscription {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Subscription {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        other
            .priority
            .cmp(&self.priority)
            .then_with(|| self.pattern.cmp(&other.pattern))
            .then_with(|| self.handler_id.cmp(&other.handler_id))
    }
}

impl Hash for Subscription {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.pattern.hash(state);
        self.handler_id.hash(state);
    }
}

/// A generic message bus to facilitate various messaging patterns.
///
/// The bus provides both a producer and consumer API for Pub/Sub, Req/Rep, as
/// well as direct point-to-point messaging to registered endpoints.
///
/// Pub/Sub wildcard patterns for hierarchical topics are possible:
///  - `*` asterisk represents one or more characters in a pattern.
///  - `?` question mark represents a single character in a pattern.
///
/// Given a topic and pattern potentially containing wildcard characters, i.e.
/// `*` and `?`, where `?` can match any single character in the topic, and `*`
/// can match any number of characters including zero characters.
///
/// The asterisk in a wildcard matches any character zero or more times. For
/// example, `comp*` matches anything beginning with `comp` which means `comp`,
/// `complete`, and `computer` are all matched.
///
/// A question mark matches a single character once. For example, `c?mp` matches
/// `camp` and `comp`. The question mark can also be used more than once.
/// For example, `c??p` would match both of the above examples and `coop`.
#[derive(Debug)]
pub struct MessageBus {
    /// The trader ID associated with the message bus.
    pub trader_id: TraderId,
    /// The instance ID associated with the message bus.
    pub instance_id: UUID4,
    /// The name for the message bus.
    pub name: String,
    /// If the message bus is backed by a database.
    pub has_backing: bool,
    pub(crate) switchboard: MessagingSwitchboard,
    pub(crate) subscriptions: AHashSet<Subscription>,
    pub(crate) topics: IndexMap<MStr<Topic>, Vec<Subscription>>,
    pub(crate) endpoints: IndexMap<MStr<Endpoint>, ShareableMessageHandler>,
    pub(crate) correlation_index: AHashMap<UUID4, ShareableMessageHandler>,
    pub(crate) router_quotes: TopicRouter<QuoteTick>,
    pub(crate) router_trades: TopicRouter<TradeTick>,
    pub(crate) router_bars: TopicRouter<Bar>,
    pub(crate) router_deltas: TopicRouter<OrderBookDeltas>,
    pub(crate) router_depth10: TopicRouter<OrderBookDepth10>,
    pub(crate) router_book_snapshots: TopicRouter<OrderBook>,
    pub(crate) router_mark_prices: TopicRouter<MarkPriceUpdate>,
    pub(crate) router_index_prices: TopicRouter<IndexPriceUpdate>,
    pub(crate) router_funding_rates: TopicRouter<FundingRateUpdate>,
    pub(crate) router_order_events: TopicRouter<OrderEventAny>,
    pub(crate) router_position_events: TopicRouter<PositionEvent>,
    pub(crate) router_account_state: TopicRouter<AccountState>,
    pub(crate) router_orders: TopicRouter<OrderAny>,
    pub(crate) router_positions: TopicRouter<Position>,
    pub(crate) router_greeks: TopicRouter<GreeksData>,
    pub(crate) router_option_greeks: TopicRouter<OptionGreeks>,
    pub(crate) router_option_chain: TopicRouter<OptionChainSlice>,
    #[cfg(feature = "defi")]
    pub(crate) router_defi_blocks: TopicRouter<nautilus_model::defi::Block>, // nautilus-import-ok
    #[cfg(feature = "defi")]
    pub(crate) router_defi_pools: TopicRouter<nautilus_model::defi::Pool>, // nautilus-import-ok
    #[cfg(feature = "defi")]
    pub(crate) router_defi_swaps: TopicRouter<nautilus_model::defi::PoolSwap>, // nautilus-import-ok
    #[cfg(feature = "defi")]
    pub(crate) router_defi_liquidity: TopicRouter<nautilus_model::defi::PoolLiquidityUpdate>, // nautilus-import-ok
    #[cfg(feature = "defi")]
    pub(crate) router_defi_collects: TopicRouter<nautilus_model::defi::PoolFeeCollect>, // nautilus-import-ok
    #[cfg(feature = "defi")]
    pub(crate) router_defi_flash: TopicRouter<nautilus_model::defi::PoolFlash>, // nautilus-import-ok
    #[cfg(feature = "defi")]
    pub(crate) endpoints_defi_data: IntoEndpointMap<nautilus_model::defi::DefiData>, // nautilus-import-ok
    pub(crate) endpoints_quotes: EndpointMap<QuoteTick>,
    pub(crate) endpoints_trades: EndpointMap<TradeTick>,
    pub(crate) endpoints_bars: EndpointMap<Bar>,
    pub(crate) endpoints_account_state: EndpointMap<AccountState>,
    pub(crate) endpoints_trading_commands: IntoEndpointMap<TradingCommand>,
    pub(crate) endpoints_data_commands: IntoEndpointMap<DataCommand>,
    pub(crate) endpoints_data_responses: IntoEndpointMap<DataResponse>,
    pub(crate) endpoints_exec_reports: IntoEndpointMap<ExecutionReport>,
    pub(crate) endpoints_order_events: IntoEndpointMap<OrderEventAny>,
    pub(crate) endpoints_data: IntoEndpointMap<Data>,
    routers_typed: AHashMap<TypeId, Box<dyn Any>>,
    endpoints_typed: AHashMap<TypeId, Box<dyn Any>>,
}

impl Default for MessageBus {
    /// Creates a new default [`MessageBus`] instance.
    fn default() -> Self {
        Self::new(TraderId::from("TRADER-001"), UUID4::new(), None, None)
    }
}

impl MessageBus {
    /// Creates a new [`MessageBus`] instance.
    #[must_use]
    pub fn new(
        trader_id: TraderId,
        instance_id: UUID4,
        name: Option<String>,
        _config: Option<HashMap<String, serde_json::Value>>,
    ) -> Self {
        Self {
            trader_id,
            instance_id,
            name: name.unwrap_or(stringify!(MessageBus).to_owned()),
            switchboard: MessagingSwitchboard::default(),
            subscriptions: AHashSet::new(),
            topics: IndexMap::new(),
            endpoints: IndexMap::new(),
            correlation_index: AHashMap::new(),
            has_backing: false,
            router_quotes: TopicRouter::new(),
            router_trades: TopicRouter::new(),
            router_bars: TopicRouter::new(),
            router_deltas: TopicRouter::new(),
            router_depth10: TopicRouter::new(),
            router_book_snapshots: TopicRouter::new(),
            router_mark_prices: TopicRouter::new(),
            router_index_prices: TopicRouter::new(),
            router_funding_rates: TopicRouter::new(),
            router_order_events: TopicRouter::new(),
            router_position_events: TopicRouter::new(),
            router_account_state: TopicRouter::new(),
            router_orders: TopicRouter::new(),
            router_positions: TopicRouter::new(),
            router_greeks: TopicRouter::new(),
            router_option_greeks: TopicRouter::new(),
            router_option_chain: TopicRouter::new(),
            #[cfg(feature = "defi")]
            router_defi_blocks: TopicRouter::new(),
            #[cfg(feature = "defi")]
            router_defi_pools: TopicRouter::new(),
            #[cfg(feature = "defi")]
            router_defi_swaps: TopicRouter::new(),
            #[cfg(feature = "defi")]
            router_defi_liquidity: TopicRouter::new(),
            #[cfg(feature = "defi")]
            router_defi_collects: TopicRouter::new(),
            #[cfg(feature = "defi")]
            router_defi_flash: TopicRouter::new(),
            #[cfg(feature = "defi")]
            endpoints_defi_data: IntoEndpointMap::new(),
            endpoints_quotes: EndpointMap::new(),
            endpoints_trades: EndpointMap::new(),
            endpoints_bars: EndpointMap::new(),
            endpoints_account_state: EndpointMap::new(),
            endpoints_trading_commands: IntoEndpointMap::new(),
            endpoints_data_commands: IntoEndpointMap::new(),
            endpoints_data_responses: IntoEndpointMap::new(),
            endpoints_exec_reports: IntoEndpointMap::new(),
            endpoints_order_events: IntoEndpointMap::new(),
            endpoints_data: IntoEndpointMap::new(),
            routers_typed: AHashMap::new(),
            endpoints_typed: AHashMap::new(),
        }
    }

    /// Registers message bus for the current thread.
    pub fn register_message_bus(self) -> Rc<RefCell<Self>> {
        let msgbus = Rc::new(RefCell::new(self));
        set_message_bus(msgbus.clone());
        msgbus
    }

    /// Gets or creates a typed router for custom message type `T`.
    ///
    /// # Panics
    ///
    /// Panics if the stored router type doesn't match `T` (internal bug).
    pub fn router<T: 'static>(&mut self) -> &mut TopicRouter<T> {
        self.routers_typed
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::new(TopicRouter::<T>::new()))
            .downcast_mut::<TopicRouter<T>>()
            .expect("TopicRouter type mismatch - this is a bug")
    }

    /// Gets or creates a typed endpoint map for custom message type `T`.
    ///
    /// # Panics
    ///
    /// Panics if the stored endpoint map type doesn't match `T` (internal bug).
    pub fn endpoint_map<T: 'static>(&mut self) -> &mut EndpointMap<T> {
        self.endpoints_typed
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::new(EndpointMap::<T>::new()))
            .downcast_mut::<EndpointMap<T>>()
            .expect("EndpointMap type mismatch - this is a bug")
    }

    /// Disposes of the message bus, clearing all subscriptions, endpoints,
    /// and handler references.
    pub fn dispose(&mut self) {
        self.subscriptions.clear();
        self.topics.clear();
        self.endpoints.clear();
        self.correlation_index.clear();

        self.router_quotes.clear();
        self.router_trades.clear();
        self.router_bars.clear();
        self.router_deltas.clear();
        self.router_depth10.clear();
        self.router_book_snapshots.clear();
        self.router_mark_prices.clear();
        self.router_index_prices.clear();
        self.router_funding_rates.clear();
        self.router_order_events.clear();
        self.router_position_events.clear();
        self.router_account_state.clear();
        self.router_orders.clear();
        self.router_positions.clear();
        self.router_greeks.clear();
        self.router_option_greeks.clear();
        self.router_option_chain.clear();

        #[cfg(feature = "defi")]
        {
            self.router_defi_blocks.clear();
            self.router_defi_pools.clear();
            self.router_defi_swaps.clear();
            self.router_defi_liquidity.clear();
            self.router_defi_collects.clear();
            self.router_defi_flash.clear();
            self.endpoints_defi_data.clear();
        }

        self.endpoints_quotes.clear();
        self.endpoints_trades.clear();
        self.endpoints_bars.clear();
        self.endpoints_account_state.clear();
        self.endpoints_trading_commands.clear();
        self.endpoints_data_commands.clear();
        self.endpoints_data_responses.clear();
        self.endpoints_exec_reports.clear();
        self.endpoints_order_events.clear();
        self.endpoints_data.clear();

        self.routers_typed.clear();
        self.endpoints_typed.clear();
    }

    /// Returns the memory address of this instance as a hexadecimal string.
    #[must_use]
    pub fn mem_address(&self) -> String {
        format!("{self:p}")
    }

    /// Returns a reference to the switchboard.
    #[must_use]
    pub fn switchboard(&self) -> &MessagingSwitchboard {
        &self.switchboard
    }

    /// Returns the registered endpoint addresses.
    #[must_use]
    pub fn endpoints(&self) -> Vec<&str> {
        self.endpoints.iter().map(|e| e.0.as_str()).collect()
    }

    /// Returns actively subscribed patterns.
    #[must_use]
    pub fn patterns(&self) -> Vec<&str> {
        self.subscriptions
            .iter()
            .map(|s| s.pattern.as_str())
            .collect()
    }

    /// Returns whether there are subscribers for the `topic`.
    pub fn has_subscribers<T: AsRef<str>>(&self, topic: T) -> bool {
        self.subscriptions_count(topic) > 0
    }

    /// Returns the count of subscribers for the `topic`.
    ///
    /// # Panics
    ///
    /// Panics if the `topic` is not a valid topic string.
    #[must_use]
    pub fn subscriptions_count<T: AsRef<str>>(&self, topic: T) -> usize {
        let topic = MStr::<Topic>::topic(topic).expect(FAILED);
        self.topics
            .get(&topic)
            .map_or_else(|| self.find_topic_matches(topic).len(), |subs| subs.len())
    }

    /// Returns active subscriptions.
    #[must_use]
    pub fn subscriptions(&self) -> Vec<&Subscription> {
        self.subscriptions.iter().collect()
    }

    /// Returns the handler IDs for actively subscribed patterns.
    #[must_use]
    pub fn subscription_handler_ids(&self) -> Vec<&str> {
        self.subscriptions
            .iter()
            .map(|s| s.handler_id.as_str())
            .collect()
    }

    /// Returns whether the endpoint is registered.
    ///
    /// # Panics
    ///
    /// Panics if the `endpoint` conversion to `MStr<Endpoint>` fails.
    #[must_use]
    pub fn is_registered<T: Into<MStr<Endpoint>>>(&self, endpoint: T) -> bool {
        let endpoint: MStr<Endpoint> = endpoint.into();
        self.endpoints.contains_key(&endpoint)
    }

    /// Returns whether the `handler` is subscribed to the `pattern`.
    #[must_use]
    pub fn is_subscribed<T: AsRef<str>>(
        &self,
        pattern: T,
        handler: ShareableMessageHandler,
    ) -> bool {
        let pattern = MStr::<Pattern>::pattern(pattern);
        let sub = Subscription::new(pattern, handler, None);
        self.subscriptions.contains(&sub)
    }

    /// Close the message bus which will close the sender channel and join the thread.
    ///
    /// # Errors
    ///
    /// This function never returns an error (TBD once backing database added).
    pub const fn close(&self) -> anyhow::Result<()> {
        // TODO: Integrate the backing database
        Ok(())
    }

    /// Returns the handler for the `endpoint`.
    #[must_use]
    pub fn get_endpoint(&self, endpoint: MStr<Endpoint>) -> Option<&ShareableMessageHandler> {
        self.endpoints.get(&endpoint)
    }

    /// Returns the handler for the `correlation_id`.
    #[must_use]
    pub fn get_response_handler(&self, correlation_id: &UUID4) -> Option<&ShareableMessageHandler> {
        self.correlation_index.get(correlation_id)
    }

    /// Finds the subscriptions with pattern matching the `topic`.
    pub(crate) fn find_topic_matches(&self, topic: MStr<Topic>) -> Vec<Subscription> {
        self.subscriptions
            .iter()
            .filter_map(|sub| {
                if is_matching_backtracking(topic, sub.pattern) {
                    Some(sub.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Finds the subscriptions which match the `topic` and caches the
    /// results in the `patterns` map.
    #[must_use]
    pub fn matching_subscriptions<T: Into<MStr<Topic>>>(&mut self, topic: T) -> Vec<Subscription> {
        self.inner_matching_subscriptions(topic.into())
    }

    pub(crate) fn inner_matching_subscriptions(&mut self, topic: MStr<Topic>) -> Vec<Subscription> {
        self.topics.get(&topic).cloned().unwrap_or_else(|| {
            let mut matches = self.find_topic_matches(topic);
            matches.sort();
            self.topics.insert(topic, matches.clone());
            matches
        })
    }

    /// Fills a buffer with handlers matching a topic.
    pub(crate) fn fill_matching_any_handlers(
        &mut self,
        topic: MStr<Topic>,
        buf: &mut SmallVec<[ShareableMessageHandler; 64]>,
    ) {
        if let Some(subs) = self.topics.get(&topic) {
            for sub in subs {
                buf.push(sub.handler.clone());
            }
        } else {
            let mut matches = self.find_topic_matches(topic);
            matches.sort();

            for sub in &matches {
                buf.push(sub.handler.clone());
            }

            self.topics.insert(topic, matches);
        }
    }

    /// Registers a response handler for a specific correlation ID.
    ///
    /// # Errors
    ///
    /// Returns an error if `handler` is already registered for the `correlation_id`.
    pub fn register_response_handler(
        &mut self,
        correlation_id: &UUID4,
        handler: ShareableMessageHandler,
    ) -> anyhow::Result<()> {
        if self.correlation_index.contains_key(correlation_id) {
            anyhow::bail!("Correlation ID <{correlation_id}> already has a registered handler");
        }

        self.correlation_index.insert(*correlation_id, handler);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use rand::{RngExt, SeedableRng, rngs::StdRng};
    use rstest::rstest;
    use ustr::Ustr;

    use super::*;
    use crate::msgbus::{
        self, ShareableMessageHandler, get_message_bus,
        matching::is_matching_backtracking,
        stubs::{get_call_check_handler, get_stub_shareable_handler},
        subscriptions_count_any,
    };

    #[rstest]
    fn test_new() {
        let trader_id = TraderId::default();
        let msgbus = MessageBus::new(trader_id, UUID4::new(), None, None);

        assert_eq!(msgbus.trader_id, trader_id);
        assert_eq!(msgbus.name, stringify!(MessageBus));
    }

    #[rstest]
    fn test_endpoints_when_no_endpoints() {
        let msgbus = get_message_bus();
        assert!(msgbus.borrow().endpoints().is_empty());
    }

    #[rstest]
    fn test_topics_when_no_subscriptions() {
        let msgbus = get_message_bus();
        assert!(msgbus.borrow().patterns().is_empty());
        assert!(!msgbus.borrow().has_subscribers("my-topic"));
    }

    #[rstest]
    fn test_is_subscribed_when_no_subscriptions() {
        let msgbus = get_message_bus();
        let handler = get_stub_shareable_handler(None);

        assert!(!msgbus.borrow().is_subscribed("my-topic", handler));
    }

    #[rstest]
    fn test_get_response_handler_when_no_handler() {
        let msgbus = get_message_bus();
        let msgbus_ref = msgbus.borrow();
        let handler = msgbus_ref.get_response_handler(&UUID4::new());
        assert!(handler.is_none());
    }

    #[rstest]
    fn test_get_response_handler_when_already_registered() {
        let msgbus = get_message_bus();
        let mut msgbus_ref = msgbus.borrow_mut();
        let handler = get_stub_shareable_handler(None);

        let request_id = UUID4::new();
        msgbus_ref
            .register_response_handler(&request_id, handler.clone())
            .unwrap();

        let result = msgbus_ref.register_response_handler(&request_id, handler);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_get_response_handler_when_registered() {
        let msgbus = get_message_bus();
        let mut msgbus_ref = msgbus.borrow_mut();
        let handler = get_stub_shareable_handler(None);

        let request_id = UUID4::new();
        msgbus_ref
            .register_response_handler(&request_id, handler)
            .unwrap();

        let handler = msgbus_ref.get_response_handler(&request_id).unwrap();
        assert_eq!(handler.id(), handler.id());
    }

    #[rstest]
    fn test_is_registered_when_no_registrations() {
        let msgbus = get_message_bus();
        assert!(!msgbus.borrow().is_registered("MyEndpoint"));
    }

    #[rstest]
    fn test_register_endpoint() {
        let msgbus = get_message_bus();
        let endpoint = "MyEndpoint".into();
        let handler = get_stub_shareable_handler(None);

        msgbus::register_any(endpoint, handler);

        assert_eq!(msgbus.borrow().endpoints(), vec![endpoint.to_string()]);
        assert!(msgbus.borrow().get_endpoint(endpoint).is_some());
    }

    #[rstest]
    fn test_endpoint_send() {
        let msgbus = get_message_bus();
        let endpoint = "MyEndpoint".into();
        let (handler, checker) = get_call_check_handler(None);

        msgbus::register_any(endpoint, handler);
        assert!(msgbus.borrow().get_endpoint(endpoint).is_some());
        assert!(!checker.was_called());

        // Send a message to the endpoint
        msgbus::send_any(endpoint, &"Test Message");
        assert!(checker.was_called());
    }

    #[rstest]
    fn test_deregsiter_endpoint() {
        let msgbus = get_message_bus();
        let endpoint = "MyEndpoint".into();
        let handler = get_stub_shareable_handler(None);

        msgbus::register_any(endpoint, handler);
        msgbus::deregister_any(endpoint);

        assert!(msgbus.borrow().endpoints().is_empty());
    }

    #[rstest]
    fn test_subscribe() {
        let msgbus = get_message_bus();
        let topic = "my-topic";
        let handler = get_stub_shareable_handler(None);

        msgbus::subscribe_any(topic.into(), handler, Some(1));

        assert!(msgbus.borrow().has_subscribers(topic));
        assert_eq!(msgbus.borrow().patterns(), vec![topic]);
    }

    #[rstest]
    fn test_unsubscribe() {
        let msgbus = get_message_bus();
        let topic = "my-topic";
        let handler = get_stub_shareable_handler(None);

        msgbus::subscribe_any(topic.into(), handler.clone(), None);
        msgbus::unsubscribe_any(topic.into(), &handler);

        assert!(!msgbus.borrow().has_subscribers(topic));
        assert!(msgbus.borrow().patterns().is_empty());
    }

    #[rstest]
    fn test_matching_subscriptions() {
        let msgbus = get_message_bus();
        let pattern = "my-pattern";

        let handler_id1 = Ustr::from("1");
        let handler1 = get_stub_shareable_handler(Some(handler_id1));

        let handler_id2 = Ustr::from("2");
        let handler2 = get_stub_shareable_handler(Some(handler_id2));

        let handler_id3 = Ustr::from("3");
        let handler3 = get_stub_shareable_handler(Some(handler_id3));

        let handler_id4 = Ustr::from("4");
        let handler4 = get_stub_shareable_handler(Some(handler_id4));

        msgbus::subscribe_any(pattern.into(), handler1, None);
        msgbus::subscribe_any(pattern.into(), handler2, None);
        msgbus::subscribe_any(pattern.into(), handler3, Some(1));
        msgbus::subscribe_any(pattern.into(), handler4, Some(2));

        assert_eq!(
            msgbus.borrow().patterns(),
            vec![pattern, pattern, pattern, pattern]
        );
        assert_eq!(subscriptions_count_any(pattern), 4);

        let topic = pattern;
        let subs = msgbus.borrow_mut().matching_subscriptions(topic);
        assert_eq!(subs.len(), 4);
        assert_eq!(subs[0].handler_id, handler_id4);
        assert_eq!(subs[1].handler_id, handler_id3);
        assert_eq!(subs[2].handler_id, handler_id1);
        assert_eq!(subs[3].handler_id, handler_id2);
    }

    #[rstest]
    fn test_subscription_pattern_matching() {
        let msgbus = get_message_bus();
        let handler1 = get_stub_shareable_handler(Some(Ustr::from("1")));
        let handler2 = get_stub_shareable_handler(Some(Ustr::from("2")));
        let handler3 = get_stub_shareable_handler(Some(Ustr::from("3")));

        msgbus::subscribe_any("data.quotes.*".into(), handler1, None);
        msgbus::subscribe_any("data.trades.*".into(), handler2, None);
        msgbus::subscribe_any("data.*.BINANCE.*".into(), handler3, None);
        assert_eq!(msgbus.borrow().subscriptions().len(), 3);

        let topic = "data.quotes.BINANCE.ETHUSDT";
        assert_eq!(msgbus.borrow().find_topic_matches(topic.into()).len(), 2);

        let matches = msgbus.borrow_mut().matching_subscriptions(topic);
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].handler_id, Ustr::from("3"));
        assert_eq!(matches[1].handler_id, Ustr::from("1"));
    }

    /// A simple reference model for subscription behavior.
    struct SimpleSubscriptionModel {
        /// Stores (pattern, handler_id) tuples for active subscriptions.
        subscriptions: Vec<(String, String)>,
    }

    impl SimpleSubscriptionModel {
        fn new() -> Self {
            Self {
                subscriptions: Vec::new(),
            }
        }

        fn subscribe(&mut self, pattern: &str, handler_id: &str) {
            let subscription = (pattern.to_string(), handler_id.to_string());
            if !self.subscriptions.contains(&subscription) {
                self.subscriptions.push(subscription);
            }
        }

        fn unsubscribe(&mut self, pattern: &str, handler_id: &str) -> bool {
            let subscription = (pattern.to_string(), handler_id.to_string());
            if let Some(idx) = self.subscriptions.iter().position(|s| s == &subscription) {
                self.subscriptions.remove(idx);
                true
            } else {
                false
            }
        }

        fn is_subscribed(&self, pattern: &str, handler_id: &str) -> bool {
            self.subscriptions
                .contains(&(pattern.to_string(), handler_id.to_string()))
        }

        fn matching_subscriptions(&self, topic: &str) -> Vec<(String, String)> {
            let topic = topic.into();

            self.subscriptions
                .iter()
                .filter(|(pat, _)| is_matching_backtracking(topic, pat.into()))
                .map(|(pat, id)| (pat.clone(), id.clone()))
                .collect()
        }

        fn subscription_count(&self) -> usize {
            self.subscriptions.len()
        }
    }

    #[rstest]
    fn subscription_model_fuzz_testing() {
        let mut rng = StdRng::seed_from_u64(42);

        let msgbus = get_message_bus();
        let mut model = SimpleSubscriptionModel::new();

        // Map from handler_id to handler
        let mut handlers: Vec<(String, ShareableMessageHandler)> = Vec::new();

        // Generate some patterns
        let patterns = generate_test_patterns(&mut rng);

        // Generate some handler IDs
        let handler_ids: Vec<String> = (0..50).map(|i| format!("handler_{i}")).collect();

        // Initialize handlers
        for id in &handler_ids {
            let handler = get_stub_shareable_handler(Some(Ustr::from(id)));
            handlers.push((id.clone(), handler));
        }

        let num_operations = 50_000;
        for op_num in 0..num_operations {
            let operation = rng.random_range(0..4);

            match operation {
                // Subscribe
                0 => {
                    let pattern_idx = rng.random_range(0..patterns.len());
                    let handler_idx = rng.random_range(0..handlers.len());
                    let pattern = &patterns[pattern_idx];
                    let (handler_id, handler) = &handlers[handler_idx];

                    // Apply to reference model
                    model.subscribe(pattern, handler_id);

                    // Apply to message bus
                    msgbus::subscribe_any(pattern.as_str().into(), handler.clone(), None);

                    assert_eq!(
                        model.subscription_count(),
                        msgbus.borrow().subscriptions().len()
                    );

                    assert!(
                        msgbus.borrow().is_subscribed(pattern, handler.clone()),
                        "Op {op_num}: is_subscribed should return true after subscribe"
                    );
                }

                // Unsubscribe
                1 => {
                    if model.subscription_count() > 0 {
                        let sub_idx = rng.random_range(0..model.subscription_count());
                        let (pattern, handler_id) = model.subscriptions[sub_idx].clone();

                        // Apply to reference model
                        model.unsubscribe(&pattern, &handler_id);

                        // Find handler
                        let handler = handlers
                            .iter()
                            .find(|(id, _)| id == &handler_id)
                            .map(|(_, h)| h.clone())
                            .unwrap();

                        // Apply to message bus
                        msgbus::unsubscribe_any(pattern.as_str().into(), &handler);

                        assert_eq!(
                            model.subscription_count(),
                            msgbus.borrow().subscriptions().len()
                        );
                        assert!(
                            !msgbus.borrow().is_subscribed(pattern, handler.clone()),
                            "Op {op_num}: is_subscribed should return false after unsubscribe"
                        );
                    }
                }

                // Check is_subscribed
                2 => {
                    // Get a random pattern and handler
                    let pattern_idx = rng.random_range(0..patterns.len());
                    let handler_idx = rng.random_range(0..handlers.len());
                    let pattern = &patterns[pattern_idx];
                    let (handler_id, handler) = &handlers[handler_idx];

                    let expected = model.is_subscribed(pattern, handler_id);
                    let actual = msgbus.borrow().is_subscribed(pattern, handler.clone());

                    assert_eq!(
                        expected, actual,
                        "Op {op_num}: Subscription state mismatch for pattern '{pattern}', handler '{handler_id}': expected={expected}, actual={actual}"
                    );
                }

                // Check matching_subscriptions
                3 => {
                    // Generate a topic
                    let topic = create_topic(&mut rng);

                    let actual_matches = msgbus.borrow_mut().matching_subscriptions(topic);
                    let expected_matches = model.matching_subscriptions(&topic);

                    assert_eq!(
                        expected_matches.len(),
                        actual_matches.len(),
                        "Op {}: Match count mismatch for topic '{}': expected={}, actual={}",
                        op_num,
                        topic,
                        expected_matches.len(),
                        actual_matches.len()
                    );

                    for sub in &actual_matches {
                        assert!(
                            expected_matches
                                .contains(&(sub.pattern.to_string(), sub.handler_id.to_string())),
                            "Op {}: Expected match not found: pattern='{}', handler_id='{}'",
                            op_num,
                            sub.pattern,
                            sub.handler_id
                        );
                    }
                }
                _ => unreachable!(),
            }
        }
    }

    fn generate_pattern_from_topic(topic: &str, rng: &mut StdRng) -> String {
        let mut pattern = String::new();

        for c in topic.chars() {
            let val: f64 = rng.random();
            if val < 0.1 {
                pattern.push('*');
            } else if val < 0.3 {
                pattern.push('?');
            } else if val >= 0.5 {
                pattern.push(c);
            }
        }

        pattern
    }

    fn generate_test_patterns(rng: &mut StdRng) -> Vec<String> {
        let mut patterns = vec![
            "data.*.*.*".to_string(),
            "*.*.BINANCE.*".to_string(),
            "events.order.*".to_string(),
            "data.*.*.?USDT".to_string(),
            "*.trades.*.BTC*".to_string(),
            "*.*.*.*".to_string(),
        ];

        // Add some random patterns
        for _ in 0..50 {
            match rng.random_range(0..10) {
                // Use existing pattern
                0..=1 => {
                    let idx = rng.random_range(0..patterns.len());
                    patterns.push(patterns[idx].clone());
                }
                // Generate new pattern from topic
                _ => {
                    let topic = create_topic(rng);
                    let pattern = generate_pattern_from_topic(&topic, rng);
                    patterns.push(pattern);
                }
            }
        }

        patterns
    }

    fn create_topic(rng: &mut StdRng) -> Ustr {
        let cat = ["data", "info", "order"];
        let model = ["quotes", "trades", "orderbooks", "depths"];
        let venue = ["BINANCE", "BYBIT", "OKX", "FTX", "KRAKEN"];
        let instrument = ["BTCUSDT", "ETHUSDT", "SOLUSDT", "XRPUSDT", "DOGEUSDT"];

        let cat = cat[rng.random_range(0..cat.len())];
        let model = model[rng.random_range(0..model.len())];
        let venue = venue[rng.random_range(0..venue.len())];
        let instrument = instrument[rng.random_range(0..instrument.len())];
        Ustr::from(&format!("{cat}.{model}.{venue}.{instrument}"))
    }
}