pub struct PeerSessionManager { /* private fields */ }Expand description
Manages the lifecycle of peer sessions with per-peer caps and idle eviction.
§Index maintenance
peer_sessions is a secondary index mapping each peer_id to its list of
session_ids. Closed entries are not removed from this index; instead,
query methods filter by session state on the fly. This makes the index
append-only and avoids the bookkeeping cost of removal.
Implementations§
Source§impl PeerSessionManager
impl PeerSessionManager
Sourcepub fn new(config: SessionManagerConfig) -> Self
pub fn new(config: SessionManagerConfig) -> Self
Create a new manager with the supplied configuration.
Sourcepub fn open_session(
&mut self,
peer_id: &str,
direction: SessionDirection,
current_tick: u64,
) -> Result<u64, String>
pub fn open_session( &mut self, peer_id: &str, direction: SessionDirection, current_tick: u64, ) -> Result<u64, String>
Open a new session for peer_id.
Checks global active capacity first, then per-peer active capacity.
§Errors
"global session limit reached"— the total number of active sessions would exceedSessionManagerConfig::global_max_sessions."per-peer session limit reached"— this peer already hasSessionManagerConfig::max_sessions_per_peeractive sessions.
Sourcepub fn transition(
&mut self,
session_id: u64,
new_state: PeerSessionState,
) -> Result<(), String>
pub fn transition( &mut self, session_id: u64, new_state: PeerSessionState, ) -> Result<(), String>
Advance a session’s lifecycle state.
§Errors
"session not found"— no session withsession_idexists."invalid transition"— the session is already in theClosedterminal state and cannot transition further.
Sourcepub fn record_activity(
&mut self,
session_id: u64,
bytes_sent: u64,
bytes_recv: u64,
current_tick: u64,
) -> bool
pub fn record_activity( &mut self, session_id: u64, bytes_sent: u64, bytes_recv: u64, current_tick: u64, ) -> bool
Record I/O activity for a session.
Accumulates bytes_sent and bytes_recv onto the session totals and
updates last_active_tick to current_tick.
Returns false if no session with session_id exists (no-op).
Sourcepub fn evict_idle(&mut self, current_tick: u64) -> Vec<u64>
pub fn evict_idle(&mut self, current_tick: u64) -> Vec<u64>
Mark idle sessions as Closed and return their IDs.
A session is considered idle when
idle_ticks(current_tick) >= config.session_idle_timeout_ticks.
Only non-Closed sessions are evaluated.
Sourcepub fn sessions_for_peer(&self, peer_id: &str) -> Vec<&PeerSessionEntry>
pub fn sessions_for_peer(&self, peer_id: &str) -> Vec<&PeerSessionEntry>
Return references to all sessions (including closed) associated with peer_id.
Sourcepub fn active_count_for_peer(&self, peer_id: &str) -> usize
pub fn active_count_for_peer(&self, peer_id: &str) -> usize
Count of non-Closed sessions for peer_id.
Sourcepub fn get_session(&self, session_id: u64) -> Option<&PeerSessionEntry>
pub fn get_session(&self, session_id: u64) -> Option<&PeerSessionEntry>
Look up a session by its ID.
Sourcepub fn stats(&self) -> SessionManagerStats
pub fn stats(&self) -> SessionManagerStats
Compute a statistics snapshot reflecting the current state of all sessions.
Auto Trait Implementations§
impl Freeze for PeerSessionManager
impl RefUnwindSafe for PeerSessionManager
impl Send for PeerSessionManager
impl Sync for PeerSessionManager
impl Unpin for PeerSessionManager
impl UnsafeUnpin for PeerSessionManager
impl UnwindSafe for PeerSessionManager
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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