Skip to main content

ModeState

Struct ModeState 

Source
pub struct ModeState { /* private fields */ }
Expand description

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:

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

Implementations§

Source§

impl ModeState

Source

pub const fn new(initial_mode: SessionMode, routing_mode: RoutingMode) -> Self

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());
Source

pub fn mode(&self) -> SessionMode

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);
Source

pub const fn routing_mode(&self) -> RoutingMode

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);
Source

pub fn is_per_command(&self) -> bool

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());
Source

pub fn is_stateful(&self) -> bool

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());
Source

pub const fn can_switch_mode(&self) -> bool

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());
Source

pub fn switch_to_stateful(&self)

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)
Source

pub const fn is_per_command_routing(&self) -> bool

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());

Trait Implementations§

Source§

impl Debug for ModeState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scope for T

Source§

fn with<F, R>(self, f: F) -> R
where Self: Sized, F: FnOnce(Self) -> R,

Scoped with ownership.
Source§

fn with_ref<F, R>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Scoped with reference.
Source§

fn with_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Scoped with mutable reference.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Value for T
where T: Send + Sync + 'static,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more