Skip to main content

nautilus_infrastructure/python/redis/
msgbus.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
16use nautilus_common::{
17    msgbus::MessageBusBackingFactory, python::msgbus::get_global_msgbus_factory_registry,
18};
19use nautilus_core::python::to_pyruntime_err;
20use pyo3::prelude::*;
21
22use crate::redis::msgbus::{RedisMessageBusConfig, RedisMessageBusFactory};
23
24#[pymethods]
25#[pyo3_stub_gen::derive::gen_stub_pymethods]
26impl RedisMessageBusConfig {
27    /// Configuration for a Redis-backed message bus backing.
28    ///
29    /// Redis 6.2 or higher is required for correct operation.
30    #[new]
31    #[expect(clippy::too_many_arguments)]
32    #[pyo3(signature = (host=None, port=None, username=None, password=None, ssl=None, connection_timeout=None, response_timeout=None, number_of_retries=None, exponent_base=None, max_delay=None, factor=None))]
33    fn py_new(
34        host: Option<String>,
35        port: Option<u16>,
36        username: Option<String>,
37        password: Option<String>,
38        ssl: Option<bool>,
39        connection_timeout: Option<u16>,
40        response_timeout: Option<u16>,
41        number_of_retries: Option<usize>,
42        exponent_base: Option<u64>,
43        max_delay: Option<u64>,
44        factor: Option<u64>,
45    ) -> Self {
46        let default = Self::default();
47        Self {
48            host,
49            port,
50            username,
51            password,
52            ssl: ssl.unwrap_or(default.ssl),
53            connection_timeout: connection_timeout.unwrap_or(default.connection_timeout),
54            response_timeout: response_timeout.unwrap_or(default.response_timeout),
55            number_of_retries: number_of_retries.unwrap_or(default.number_of_retries),
56            exponent_base: exponent_base.unwrap_or(default.exponent_base),
57            max_delay: max_delay.unwrap_or(default.max_delay),
58            factor: factor.unwrap_or(default.factor),
59        }
60    }
61
62    #[getter]
63    fn host(&self) -> Option<&str> {
64        self.host.as_deref()
65    }
66
67    #[getter]
68    const fn port(&self) -> Option<u16> {
69        self.port
70    }
71
72    #[getter]
73    fn username(&self) -> Option<&str> {
74        self.username.as_deref()
75    }
76
77    #[getter]
78    fn password(&self) -> Option<&str> {
79        self.password.as_deref()
80    }
81
82    #[getter]
83    const fn ssl(&self) -> bool {
84        self.ssl
85    }
86
87    #[getter]
88    const fn connection_timeout(&self) -> u16 {
89        self.connection_timeout
90    }
91
92    #[getter]
93    const fn response_timeout(&self) -> u16 {
94        self.response_timeout
95    }
96
97    #[getter]
98    const fn number_of_retries(&self) -> usize {
99        self.number_of_retries
100    }
101
102    #[getter]
103    const fn exponent_base(&self) -> u64 {
104        self.exponent_base
105    }
106
107    #[getter]
108    const fn max_delay(&self) -> u64 {
109        self.max_delay
110    }
111
112    #[getter]
113    const fn factor(&self) -> u64 {
114        self.factor
115    }
116}
117
118#[derive(Debug, Clone)]
119#[pyclass(
120    name = "RedisMessageBusFactory",
121    module = "nautilus_trader.infrastructure",
122    from_py_object
123)]
124#[pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.infrastructure")]
125pub struct PyRedisMessageBusFactory {
126    inner: RedisMessageBusFactory,
127}
128
129#[pymethods]
130#[pyo3_stub_gen::derive::gen_stub_pymethods]
131impl PyRedisMessageBusFactory {
132    /// Creates a Redis message bus backing factory.
133    #[new]
134    #[pyo3(signature = (config=None))]
135    fn py_new(config: Option<RedisMessageBusConfig>) -> Self {
136        Self {
137            inner: RedisMessageBusFactory::new(config.unwrap_or_default()),
138        }
139    }
140}
141
142#[expect(clippy::needless_pass_by_value)]
143fn extract_redis_msgbus_config(
144    py: Python<'_>,
145    factory: Py<PyAny>,
146) -> PyResult<Box<dyn MessageBusBackingFactory>> {
147    Ok(Box::new(factory.extract::<RedisMessageBusConfig>(py)?))
148}
149
150#[expect(clippy::needless_pass_by_value)]
151fn extract_redis_msgbus_factory(
152    py: Python<'_>,
153    factory: Py<PyAny>,
154) -> PyResult<Box<dyn MessageBusBackingFactory>> {
155    let factory = factory.extract::<PyRedisMessageBusFactory>(py)?;
156    Ok(Box::new(factory.inner))
157}
158
159pub(in crate::python) fn register_redis_msgbus_factory() -> PyResult<()> {
160    let registry = get_global_msgbus_factory_registry();
161    registry
162        .register(
163            stringify!(RedisMessageBusConfig).to_string(),
164            extract_redis_msgbus_config,
165        )
166        .map_err(to_pyruntime_err)?;
167    registry
168        .register(
169            stringify!(RedisMessageBusFactory).to_string(),
170            extract_redis_msgbus_factory,
171        )
172        .map_err(to_pyruntime_err)
173}