1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
}
}