pub struct SessionManager {
sessions: Mutex<HashMap<SessionId, ManagedSession>>,
next_id: Mutex<SessionId>,
timeout: Duration,
}Expand description
Manages all active sessions.
Provides thread-safe access to create, retrieve, and remove sessions. Sessions persist for a configurable timeout after all clients disconnect.
Fields§
§sessions: Mutex<HashMap<SessionId, ManagedSession>>Map of session IDs to managed sessions.
next_id: Mutex<SessionId>Counter for generating unique session IDs.
timeout: DurationSession timeout duration.
Implementations§
Source§impl SessionManager
impl SessionManager
Sourcepub fn create_session(&self) -> Arc<Session> ⓘ
pub fn create_session(&self) -> Arc<Session> ⓘ
Create a new session and return a reference to it.
Sourcepub fn mcp_session(&self) -> Arc<Session> ⓘ
pub fn mcp_session(&self) -> Arc<Session> ⓘ
Returns the session shared by all MCP clients, creating it if there is none.
It’s never counted as connected. Instead, SessionManager::cleanup_expired_sessions removes it
once no request has been sent through it for DEFAULT_SESSION_TIMEOUT_SECS, if it has no open
packs and no unfinished jobs.
fn create_session_internal(&self, is_mcp: bool) -> Arc<Session> ⓘ
Sourcefn insert_session(
&self,
sessions: &mut HashMap<SessionId, ManagedSession>,
is_mcp: bool,
) -> Arc<Session> ⓘ
fn insert_session( &self, sessions: &mut HashMap<SessionId, ManagedSession>, is_mcp: bool, ) -> Arc<Session> ⓘ
Creates a session and adds it to the provided sessions, which must be the locked SessionManager::sessions.
Sourcepub fn get_or_create_session(
&self,
session_id: Option<SessionId>,
) -> (Arc<Session>, bool)
pub fn get_or_create_session( &self, session_id: Option<SessionId>, ) -> (Arc<Session>, bool)
Get an existing session by ID, or create a new one if the ID doesn’t exist.
If session_id is Some, attempts to retrieve that session.
If the session doesn’t exist or session_id is None, creates a new session.
Returns the session and whether it was newly created.
Sourcepub fn get_session(&self, id: SessionId) -> Option<Arc<Session>>
pub fn get_session(&self, id: SessionId) -> Option<Arc<Session>>
Get a session by ID without incrementing the connection count.
Sourcepub fn client_disconnected(&self, id: SessionId)
pub fn client_disconnected(&self, id: SessionId)
Mark a session as disconnected by a client.
If no more clients are connected, starts the timeout countdown.
SessionManager::cleanup_expired_sessions removes the session after the timeout unless a client reconnects.
Sourcepub fn cleanup_expired_sessions(&self) -> bool
pub fn cleanup_expired_sessions(&self) -> bool
Perform cleanup of expired sessions.
This should be called periodically.
§Returns
If a WebSocket session was removed.
Sourcepub fn exit_if_no_sessions(&self)
pub fn exit_if_no_sessions(&self)
Stops the server process if there are no sessions left.
Called after a WebSocket session is removed. Removing the MCP session never stops the process, so a server only used by MCP clients keeps running.
Sourcepub fn remove_session(&self, id: SessionId) -> Option<Arc<Session>>
pub fn remove_session(&self, id: SessionId) -> Option<Arc<Session>>
Remove a session immediately.
Sourcepub fn session_count(&self) -> usize
pub fn session_count(&self) -> usize
Get the number of active sessions.
Sourcepub fn session_ids(&self) -> Vec<SessionId> ⓘ
pub fn session_ids(&self) -> Vec<SessionId> ⓘ
Get all active session IDs.
Sourcepub fn get_sessions_info(&self) -> Vec<SessionInfo>
pub fn get_sessions_info(&self) -> Vec<SessionInfo>
Get information about all active sessions.
Returns a vector of SessionInfo structs containing session state snapshots
for use by session management tools.
Sourcepub fn start_cleanup_task(manager: Arc<Self>)
pub fn start_cleanup_task(manager: Arc<Self>)
Start a background task that periodically cleans up expired sessions, and stops the server when none are left.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for SessionManager
impl RefUnwindSafe for SessionManager
impl Send for SessionManager
impl Sync for SessionManager
impl Unpin for SessionManager
impl UnsafeUnpin for SessionManager
impl UnwindSafe for SessionManager
Blanket Implementations§
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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 moreimpl<T> MaybeSend for T
impl<T> MaybeSync for T
§impl<T> NoneValue for Twhere
T: Default,
impl<T> NoneValue for Twhere
T: Default,
type NoneType = T
§fn null_value() -> T
fn null_value() -> T
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.