nntp-proxy 0.5.1

NNTP proxy server with per-command backend multiplexing, caching, metrics, and TUI dashboard
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Session mode state management
//!
//! This module provides a type-safe wrapper for session mode and routing mode,
//! ensuring valid state transitions and clear mode switching logic.

use crate::config::RoutingMode;
use std::sync::atomic::{AtomicU8, Ordering};

/// Session mode - determines how commands are routed
///
/// This is separate from `RoutingMode` (configuration) - it represents the
/// *current* runtime state of the session, which can change (e.g., hybrid mode).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum SessionMode {
    /// Per-command routing - each command may go to different backend
    PerCommand = 0,

    /// Stateful mode - using a dedicated backend connection
    Stateful = 1,
}

impl SessionMode {
    /// Check if this mode is per-command routing
    #[inline]
    #[must_use]
    pub const fn is_per_command(self) -> bool {
        matches!(self, Self::PerCommand)
    }

    /// Check if this mode is stateful
    #[inline]
    #[must_use]
    pub const fn is_stateful(self) -> bool {
        matches!(self, Self::Stateful)
    }

    /// Convert to u8 for atomic storage
    #[inline]
    const fn to_u8(self) -> u8 {
        self as u8
    }

    /// Convert from u8 from atomic storage
    #[inline]
    const fn from_u8(value: u8) -> Self {
        match value {
            1 => Self::Stateful,
            _ => Self::PerCommand, // 0 or any unknown value
        }
    }
}

/// Manages session mode state with support for runtime transitions
///
/// This type encapsulates the current session mode and routing mode configuration,
/// providing thread-safe mode transitions for hybrid routing.
///
/// # Design
///
/// - **Current Mode**: `AtomicU8` for lock-free concurrent reads/writes
/// - **Routing Mode**: Immutable configuration (Stateful, `PerCommand`, or Hybrid)
/// - Mode transitions are only allowed in Hybrid mode
///
/// # One-Way Transition Invariant
///
/// **CRITICAL**: In Hybrid mode, the transition from `PerCommand` → Stateful is
/// **permanent and irreversible** for the lifetime of the connection:
///
/// ```text
/// PerCommand ──stateful command──> Stateful
///     ↑                               │
///     └───────── NO WAY BACK ─────────┘
/// ```
///
/// Once `switch_to_stateful()` is called:
/// - Connection acquires a dedicated backend
/// - All subsequent commands use that backend
/// - Connection stays stateful until client disconnects
/// - New client connection starts fresh in `PerCommand` mode (if Hybrid)
///
/// # Examples
///
/// ```
/// use nntp_proxy::session::{ModeState, SessionMode};
/// use nntp_proxy::config::RoutingMode;
///
/// // Stateful mode (no transitions allowed)
/// let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
/// assert!(state.is_stateful());
/// assert!(!state.can_switch_mode());
///
/// // Hybrid mode (starts per-command, can switch)
/// let state = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
/// assert!(state.is_per_command());
/// assert!(state.can_switch_mode());
///
/// state.switch_to_stateful();
/// assert!(state.is_stateful());
/// // Now permanently stateful for this connection
/// ```
#[derive(Debug)]
pub struct ModeState {
    /// Current session mode (can change at runtime in Hybrid mode)
    mode: AtomicU8,

    /// Routing mode configuration (immutable)
    routing_mode: RoutingMode,
}

impl ModeState {
    /// Create a new mode state
    ///
    /// # Arguments
    ///
    /// * `initial_mode` - Initial session mode
    /// * `routing_mode` - Routing mode configuration
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
    /// assert!(state.is_stateful());
    /// ```
    #[inline]
    #[must_use]
    pub const fn new(initial_mode: SessionMode, routing_mode: RoutingMode) -> Self {
        Self {
            mode: AtomicU8::new(initial_mode.to_u8()),
            routing_mode,
        }
    }

    /// Get the current session mode
    ///
    /// This is a cheap atomic load operation.
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let state = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
    /// assert_eq!(state.mode(), SessionMode::PerCommand);
    /// ```
    #[inline]
    #[must_use]
    pub fn mode(&self) -> SessionMode {
        SessionMode::from_u8(self.mode.load(Ordering::Relaxed))
    }

    /// Get the routing mode configuration
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
    /// assert_eq!(state.routing_mode(), RoutingMode::Stateful);
    /// ```
    #[inline]
    #[must_use]
    pub const fn routing_mode(&self) -> RoutingMode {
        self.routing_mode
    }

    /// Check if currently in per-command mode
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let state = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
    /// assert!(state.is_per_command());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_per_command(&self) -> bool {
        self.mode().is_per_command()
    }

    /// Check if currently in stateful mode
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
    /// assert!(state.is_stateful());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_stateful(&self) -> bool {
        self.mode().is_stateful()
    }

    /// Check if mode switching is allowed
    ///
    /// Mode switching is only allowed in Hybrid routing mode.
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let hybrid = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
    /// assert!(hybrid.can_switch_mode());
    ///
    /// let stateful = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
    /// assert!(!stateful.can_switch_mode());
    /// ```
    #[inline]
    #[must_use]
    pub const fn can_switch_mode(&self) -> bool {
        matches!(self.routing_mode, RoutingMode::Hybrid)
    }

    /// Switch to stateful mode (one-way transition)
    ///
    /// **IMPORTANT**: This is a **permanent, one-way transition** for this connection.
    /// Once switched from per-command to stateful mode, the connection remains
    /// stateful for its entire lifetime and **never switches back**.
    ///
    /// This transition happens in Hybrid mode when:
    /// - Client issues a stateful command (GROUP, NEXT, LAST, XOVER, etc.)
    /// - Client needs server-side state maintained across commands
    /// - Connection acquires a dedicated backend and keeps it until disconnect
    ///
    /// Only allowed in Hybrid routing mode. No-op if already stateful or
    /// if routing mode doesn't allow switching.
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let state = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
    /// assert!(state.is_per_command());
    ///
    /// // Client sends "GROUP alt.binaries.test"
    /// state.switch_to_stateful();
    /// assert!(state.is_stateful());
    ///
    /// // Connection stays stateful until client disconnects
    /// // (no way to switch back to per-command)
    /// ```
    #[inline]
    pub fn switch_to_stateful(&self) {
        if self.can_switch_mode() {
            self.mode
                .store(SessionMode::Stateful.to_u8(), Ordering::Relaxed);
        }
    }

    /// Check if this session is using per-command routing
    ///
    /// Returns true if `routing_mode` is `PerCommand` or Hybrid.
    ///
    /// # Examples
    ///
    /// ```
    /// use nntp_proxy::session::{ModeState, SessionMode};
    /// use nntp_proxy::config::RoutingMode;
    ///
    /// let per_cmd = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
    /// assert!(per_cmd.is_per_command_routing());
    ///
    /// let hybrid = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
    /// assert!(hybrid.is_per_command_routing());
    ///
    /// let stateful = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
    /// assert!(!stateful.is_per_command_routing());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_per_command_routing(&self) -> bool {
        matches!(
            self.routing_mode,
            RoutingMode::PerCommand | RoutingMode::Hybrid
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_session_mode_is_per_command() {
        assert!(SessionMode::PerCommand.is_per_command());
        assert!(!SessionMode::Stateful.is_per_command());
    }

    #[test]
    fn test_session_mode_is_stateful() {
        assert!(SessionMode::Stateful.is_stateful());
        assert!(!SessionMode::PerCommand.is_stateful());
    }

    #[test]
    fn test_session_mode_roundtrip() {
        assert_eq!(
            SessionMode::from_u8(SessionMode::PerCommand.to_u8()),
            SessionMode::PerCommand
        );
        assert_eq!(
            SessionMode::from_u8(SessionMode::Stateful.to_u8()),
            SessionMode::Stateful
        );
    }

    #[test]
    fn test_mode_state_new() {
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
        assert_eq!(state.mode(), SessionMode::PerCommand);
        assert_eq!(state.routing_mode(), RoutingMode::PerCommand);
    }

    #[test]
    fn test_mode_state_is_per_command() {
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
        assert!(state.is_per_command());
        assert!(!state.is_stateful());
    }

    #[test]
    fn test_mode_state_is_stateful() {
        let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
        assert!(state.is_stateful());
        assert!(!state.is_per_command());
    }

    #[test]
    fn test_can_switch_mode_hybrid() {
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
        assert!(state.can_switch_mode());
    }

    #[test]
    fn test_cannot_switch_mode_stateful() {
        let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
        assert!(!state.can_switch_mode());
    }

    #[test]
    fn test_cannot_switch_mode_per_command() {
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
        assert!(!state.can_switch_mode());
    }

    #[test]
    fn test_switch_to_stateful_in_hybrid() {
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
        assert!(state.is_per_command());

        state.switch_to_stateful();
        assert!(state.is_stateful());
    }

    #[test]
    fn test_switch_to_stateful_noop_in_stateful_mode() {
        let state = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
        assert!(state.is_stateful());

        state.switch_to_stateful();
        assert!(state.is_stateful());
    }

    #[test]
    fn test_switch_to_stateful_noop_in_per_command_mode() {
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
        assert!(state.is_per_command());

        state.switch_to_stateful();
        // Should remain in per-command mode
        assert!(state.is_per_command());
    }

    #[test]
    fn test_is_per_command_routing() {
        let per_cmd = ModeState::new(SessionMode::PerCommand, RoutingMode::PerCommand);
        assert!(per_cmd.is_per_command_routing());

        let hybrid = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
        assert!(hybrid.is_per_command_routing());

        let stateful = ModeState::new(SessionMode::Stateful, RoutingMode::Stateful);
        assert!(!stateful.is_per_command_routing());
    }

    #[test]
    fn test_one_way_transition_invariant() {
        // Once switched to stateful in hybrid mode, stays stateful forever
        let state = ModeState::new(SessionMode::PerCommand, RoutingMode::Hybrid);
        assert!(state.is_per_command());

        // Simulate client sending "GROUP alt.test"
        state.switch_to_stateful();
        assert!(state.is_stateful());

        // No way to switch back - would need new connection
        // (No switch_to_per_command() method exists)

        // Verify it stays stateful
        assert!(state.is_stateful());
        assert!(!state.is_per_command());

        // Can call switch_to_stateful again (no-op)
        state.switch_to_stateful();
        assert!(state.is_stateful());
    }
}