nautilus_common/live/
runner.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Tokio-based channel senders for live trading runtime.
17//!
18//! This module provides thread-local storage for tokio mpsc channels used in live trading.
19
20use std::cell::OnceCell;
21
22use crate::messages::{DataEvent, ExecutionEvent};
23
24/// Gets the global data event sender.
25///
26/// # Panics
27///
28/// Panics if the sender is uninitialized.
29#[must_use]
30pub fn get_data_event_sender() -> tokio::sync::mpsc::UnboundedSender<DataEvent> {
31    DATA_EVENT_SENDER.with(|sender| {
32        sender
33            .get()
34            .expect("Data event sender should be initialized by runner")
35            .clone()
36    })
37}
38
39/// Sets the global data event sender.
40///
41/// Can only be called once per thread.
42///
43/// # Panics
44///
45/// Panics if a sender has already been set.
46pub fn set_data_event_sender(sender: tokio::sync::mpsc::UnboundedSender<DataEvent>) {
47    DATA_EVENT_SENDER.with(|s| {
48        assert!(
49            s.set(sender).is_ok(),
50            "Data event sender can only be set once"
51        );
52    });
53}
54
55/// Gets the global execution event sender.
56///
57/// # Panics
58///
59/// Panics if the sender is uninitialized.
60#[must_use]
61pub fn get_exec_event_sender() -> tokio::sync::mpsc::UnboundedSender<ExecutionEvent> {
62    EXEC_EVENT_SENDER.with(|sender| {
63        sender
64            .get()
65            .expect("Execution event sender should be initialized by runner")
66            .clone()
67    })
68}
69
70/// Sets the global execution event sender.
71///
72/// Can only be called once per thread.
73///
74/// # Panics
75///
76/// Panics if a sender has already been set.
77pub fn set_exec_event_sender(sender: tokio::sync::mpsc::UnboundedSender<ExecutionEvent>) {
78    EXEC_EVENT_SENDER.with(|s| {
79        assert!(
80            s.set(sender).is_ok(),
81            "Execution event sender can only be set once"
82        );
83    });
84}
85
86thread_local! {
87    static DATA_EVENT_SENDER: OnceCell<tokio::sync::mpsc::UnboundedSender<DataEvent>> = const { OnceCell::new() };
88    static EXEC_EVENT_SENDER: OnceCell<tokio::sync::mpsc::UnboundedSender<ExecutionEvent>> = const { OnceCell::new() };
89}