motorcortex-rust 0.5.0

Motorcortex Rust: a Rust client for the Motorcortex Core real-time control system (async + blocking).
Documentation
use std::time::Duration;

/// Options used to configure the connection settings.
#[derive(Clone)]
pub struct ConnectionOptions {
    /// Path to the TLS certificate used for secure communication.
    pub certificate: String,
    /// Connection timeout in milliseconds.
    pub conn_timeout_ms: u32,
    /// I/O timeout in milliseconds.
    pub io_timeout_ms: u32,
    /// Whether NNG should auto-redial the transport on drop. When
    /// `true` (default), `NNG_OPT_RECONNMINT` / `NNG_OPT_RECONNMAXT`
    /// are set on the dialer and the driver's session-restore logic
    /// kicks in on the next pipe ADD event. When `false`, dropped
    /// transports stay `Disconnected` and the user must call
    /// `connect` again explicitly.
    pub reconnect: bool,
    /// Min backoff for NNG's dialer redial (`NNG_OPT_RECONNMINT`).
    /// Ignored when `reconnect == false`.
    pub reconnect_min: Duration,
    /// Max backoff ceiling for NNG's dialer redial
    /// (`NNG_OPT_RECONNMAXT`). Ignored when `reconnect == false`.
    pub reconnect_max: Duration,
    /// How often to refresh the session token on a live connection.
    /// Each refresh is a `GetSessionToken` RPC; the fresh token is
    /// stashed in the driver for `restore_session` to use after a
    /// drop. Default 30 s matches motorcortex-python. Set to
    /// `Duration::ZERO` to disable.
    pub token_refresh_interval: Duration,
    /// Safety net for the automatic reconnect path. When `Some(n)`,
    /// the driver counts consecutive
    /// `ADD_POST → RestoreSession(token) → non-Ok` cycles and, on
    /// the `n`-th failure, disables NNG's dialer redial and
    /// publishes [`ConnectionState::Disconnected`]. A successful
    /// restore (or a reconnect that skipped restore because no
    /// token was cached) resets the counter. `None` (default) means
    /// the driver keeps trying indefinitely, matching NNG's
    /// built-in behaviour.
    ///
    /// [`ConnectionState::Disconnected`]: crate::core::ConnectionState::Disconnected
    pub max_reconnect_attempts: Option<u32>,
}

impl ConnectionOptions {
    /// Creates a new `ConnectionOptions` instance with reconnect /
    /// token-refresh defaults. Use the `with_*` setters to override
    /// the reconnect knobs.
    ///
    /// # Arguments:
    /// * `certificate` - Path to the TLS certificate.
    /// * `conn_timeout_ms` - Timeout value for connection establishment, in milliseconds.
    /// * `io_timeout_ms` - Timeout value for I/O operations, in milliseconds.
    pub fn new(certificate: String, conn_timeout_ms: u32, io_timeout_ms: u32) -> Self {
        Self {
            certificate,
            conn_timeout_ms,
            io_timeout_ms,
            reconnect: true,
            reconnect_min: Duration::from_millis(100),
            reconnect_max: Duration::from_secs(30),
            token_refresh_interval: Duration::from_secs(30),
            max_reconnect_attempts: None,
        }
    }

    /// Disable NNG auto-redial + driver session-restore logic.
    pub fn with_reconnect(mut self, enabled: bool) -> Self {
        self.reconnect = enabled;
        self
    }

    /// Override NNG's dialer backoff window.
    pub fn with_reconnect_backoff(mut self, min: Duration, max: Duration) -> Self {
        self.reconnect_min = min;
        self.reconnect_max = max;
        self
    }

    /// Override the session-token refresh cadence. `Duration::ZERO`
    /// disables the background refresh entirely.
    pub fn with_token_refresh_interval(mut self, interval: Duration) -> Self {
        self.token_refresh_interval = interval;
        self
    }

    /// Cap the number of consecutive RestoreSession failures the
    /// driver tolerates before it disables NNG's dialer and
    /// publishes [`ConnectionState::Disconnected`]. Pass `None` to
    /// keep the default "retry forever" behaviour.
    ///
    /// [`ConnectionState::Disconnected`]: crate::core::ConnectionState::Disconnected
    pub fn with_max_reconnect_attempts(mut self, attempts: Option<u32>) -> Self {
        self.max_reconnect_attempts = attempts;
        self
    }
}