motorcortex_rust/connection/connection_options.rs
1use std::time::Duration;
2
3/// Options used to configure the connection settings.
4#[derive(Clone)]
5pub struct ConnectionOptions {
6 /// Path to the TLS certificate used for secure communication.
7 pub certificate: String,
8 /// Connection timeout in milliseconds.
9 pub conn_timeout_ms: u32,
10 /// I/O timeout in milliseconds.
11 pub io_timeout_ms: u32,
12 /// Whether NNG should auto-redial the transport on drop. When
13 /// `true` (default), `NNG_OPT_RECONNMINT` / `NNG_OPT_RECONNMAXT`
14 /// are set on the dialer and the driver's session-restore logic
15 /// kicks in on the next pipe ADD event. When `false`, dropped
16 /// transports stay `Disconnected` and the user must call
17 /// `connect` again explicitly.
18 pub reconnect: bool,
19 /// Min backoff for NNG's dialer redial (`NNG_OPT_RECONNMINT`).
20 /// Ignored when `reconnect == false`.
21 pub reconnect_min: Duration,
22 /// Max backoff ceiling for NNG's dialer redial
23 /// (`NNG_OPT_RECONNMAXT`). Ignored when `reconnect == false`.
24 pub reconnect_max: Duration,
25 /// How often to refresh the session token on a live connection.
26 /// Each refresh is a `GetSessionToken` RPC; the fresh token is
27 /// stashed in the driver for `restore_session` to use after a
28 /// drop. Default 30 s matches motorcortex-python. Set to
29 /// `Duration::ZERO` to disable.
30 pub token_refresh_interval: Duration,
31 /// Safety net for the automatic reconnect path. When `Some(n)`,
32 /// the driver counts consecutive
33 /// `ADD_POST → RestoreSession(token) → non-Ok` cycles and, on
34 /// the `n`-th failure, disables NNG's dialer redial and
35 /// publishes [`ConnectionState::Disconnected`]. A successful
36 /// restore (or a reconnect that skipped restore because no
37 /// token was cached) resets the counter. `None` (default) means
38 /// the driver keeps trying indefinitely, matching NNG's
39 /// built-in behaviour.
40 ///
41 /// [`ConnectionState::Disconnected`]: crate::core::ConnectionState::Disconnected
42 pub max_reconnect_attempts: Option<u32>,
43}
44
45impl ConnectionOptions {
46 /// Creates a new `ConnectionOptions` instance with reconnect /
47 /// token-refresh defaults. Use the `with_*` setters to override
48 /// the reconnect knobs.
49 ///
50 /// # Arguments:
51 /// * `certificate` - Path to the TLS certificate.
52 /// * `conn_timeout_ms` - Timeout value for connection establishment, in milliseconds.
53 /// * `io_timeout_ms` - Timeout value for I/O operations, in milliseconds.
54 pub fn new(certificate: String, conn_timeout_ms: u32, io_timeout_ms: u32) -> Self {
55 Self {
56 certificate,
57 conn_timeout_ms,
58 io_timeout_ms,
59 reconnect: true,
60 reconnect_min: Duration::from_millis(100),
61 reconnect_max: Duration::from_secs(30),
62 token_refresh_interval: Duration::from_secs(30),
63 max_reconnect_attempts: None,
64 }
65 }
66
67 /// Disable NNG auto-redial + driver session-restore logic.
68 pub fn with_reconnect(mut self, enabled: bool) -> Self {
69 self.reconnect = enabled;
70 self
71 }
72
73 /// Override NNG's dialer backoff window.
74 pub fn with_reconnect_backoff(mut self, min: Duration, max: Duration) -> Self {
75 self.reconnect_min = min;
76 self.reconnect_max = max;
77 self
78 }
79
80 /// Override the session-token refresh cadence. `Duration::ZERO`
81 /// disables the background refresh entirely.
82 pub fn with_token_refresh_interval(mut self, interval: Duration) -> Self {
83 self.token_refresh_interval = interval;
84 self
85 }
86
87 /// Cap the number of consecutive RestoreSession failures the
88 /// driver tolerates before it disables NNG's dialer and
89 /// publishes [`ConnectionState::Disconnected`]. Pass `None` to
90 /// keep the default "retry forever" behaviour.
91 ///
92 /// [`ConnectionState::Disconnected`]: crate::core::ConnectionState::Disconnected
93 pub fn with_max_reconnect_attempts(mut self, attempts: Option<u32>) -> Self {
94 self.max_reconnect_attempts = attempts;
95 self
96 }
97}