nautilus_common/live/
runner.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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        if s.set(sender).is_err() {
49            panic!("Data event sender can only be set once");
50        }
51    });
52}
53
54/// Gets the global execution event sender.
55///
56/// # Panics
57///
58/// Panics if the sender is uninitialized.
59#[must_use]
60pub fn get_exec_event_sender() -> tokio::sync::mpsc::UnboundedSender<ExecutionEvent> {
61    EXEC_EVENT_SENDER.with(|sender| {
62        sender
63            .get()
64            .expect("Execution event sender should be initialized by runner")
65            .clone()
66    })
67}
68
69/// Sets the global execution event sender.
70///
71/// Can only be called once per thread.
72///
73/// # Panics
74///
75/// Panics if a sender has already been set.
76pub fn set_exec_event_sender(sender: tokio::sync::mpsc::UnboundedSender<ExecutionEvent>) {
77    EXEC_EVENT_SENDER.with(|s| {
78        if s.set(sender).is_err() {
79            panic!("Execution event sender can only be set once");
80        }
81    });
82}
83
84thread_local! {
85    static DATA_EVENT_SENDER: OnceCell<tokio::sync::mpsc::UnboundedSender<DataEvent>> = const { OnceCell::new() };
86    static EXEC_EVENT_SENDER: OnceCell<tokio::sync::mpsc::UnboundedSender<ExecutionEvent>> = const { OnceCell::new() };
87}