Skip to main content

nautilus_execution/python/
latency.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//! Python bindings for latency model types.
17
18use nautilus_core::{DurationNanos, python::to_pytype_err};
19use pyo3::{IntoPyObjectExt, prelude::*};
20
21use crate::models::latency::{LatencyModelAny, StaticLatencyModel};
22
23#[pymethods]
24#[pyo3_stub_gen::derive::gen_stub_pymethods]
25impl StaticLatencyModel {
26    /// Static latency model with fixed latency values.
27    ///
28    /// Models the latency for different order operations including base network latency
29    /// and specific operation latencies for insert, update, and delete operations.
30    ///
31    /// The base latency is automatically added to each operation latency, matching
32    /// Python's behavior. For example, if `base_latency_nanos = 100ms` and
33    /// `insert_latency_nanos = 200ms`, the effective insert latency will be 300ms.
34    #[new]
35    #[pyo3(signature = (
36        base_latency_nanos = 0,
37        insert_latency_nanos = 0,
38        update_latency_nanos = 0,
39        cancel_latency_nanos = 0,
40    ))]
41    fn py_new(
42        base_latency_nanos: u64,
43        insert_latency_nanos: u64,
44        update_latency_nanos: u64,
45        cancel_latency_nanos: u64,
46    ) -> Self {
47        Self::new(
48            DurationNanos::new(base_latency_nanos),
49            DurationNanos::new(insert_latency_nanos),
50            DurationNanos::new(update_latency_nanos),
51            DurationNanos::new(cancel_latency_nanos),
52        )
53    }
54
55    fn __repr__(&self) -> String {
56        format!("{self:?}")
57    }
58}
59
60/// Extracts a Python latency model object into a Rust [`LatencyModelAny`].
61///
62/// # Errors
63///
64/// Returns an error if `obj` is not a supported latency model binding.
65pub fn pyobject_to_latency_model_any(obj: &Bound<'_, PyAny>) -> PyResult<LatencyModelAny> {
66    if let Ok(m) = obj.extract::<StaticLatencyModel>() {
67        return Ok(LatencyModelAny::Static(m));
68    }
69
70    let type_name = obj.get_type().name()?;
71    Err(to_pytype_err(format!(
72        "Cannot convert {type_name} to LatencyModel"
73    )))
74}
75
76/// Converts a Rust [`LatencyModelAny`] into its Python binding object.
77///
78/// # Errors
79///
80/// Returns an error if conversion to a Python object fails.
81pub fn latency_model_any_to_pyobject(
82    py: Python<'_>,
83    model: &LatencyModelAny,
84) -> PyResult<Py<PyAny>> {
85    match model {
86        LatencyModelAny::Static(model) => model.clone().into_py_any(py),
87    }
88}