pub struct BoundedSessionManager { /* private fields */ }Expand description
Wraps LocalSessionManager and limits the number of concurrent sessions.
When the limit is reached, the oldest session (by creation order) is closed before the new one is created. This prevents unbounded memory growth when many clients connect without explicitly closing their sessions.
Optionally, a rate limit can be applied to session creation via
BoundedSessionManager::with_rate_limit.
§Concurrency note
Under concurrent session creation, the live count may transiently exceed
max_sessions by at most the number of concurrent callers. The limit is
best-effort under contention; use a semaphore if exact enforcement is
required.
Implementations§
Source§impl BoundedSessionManager
impl BoundedSessionManager
Sourcepub fn new(session_config: SessionConfig, max_sessions: usize) -> Self
pub fn new(session_config: SessionConfig, max_sessions: usize) -> Self
Create a new BoundedSessionManager.
session_config— passed through to the innerLocalSessionManager.max_sessions— maximum number of concurrent sessions. When this limit is reached, the oldest session is evicted before creating a new one. Must be at least 1.
§Panics
Panics if max_sessions is 0.
Sourcepub fn with_rate_limit(self, max_creates: usize, window: Duration) -> Self
pub fn with_rate_limit(self, max_creates: usize, window: Duration) -> Self
Configure a rate limit on session creation.
At most max_creates sessions may be created within any rolling
window duration. If exceeded, BoundedSessionError::RateLimited is
returned and no eviction is performed.
§Panics
Panics if max_creates is 0. Pass no rate limit instead of 0 — a limit
of zero would silently block all session creation.
Trait Implementations§
Source§impl SessionManager for BoundedSessionManager
impl SessionManager for BoundedSessionManager
type Error = BoundedSessionError
type Transport = WorkerTransport<LocalSessionWorker>
Source§async fn create_session(
&self,
) -> Result<(SessionId, Self::Transport), Self::Error>
async fn create_session( &self, ) -> Result<(SessionId, Self::Transport), Self::Error>
Source§async fn close_session(&self, id: &SessionId) -> Result<(), Self::Error>
async fn close_session(&self, id: &SessionId) -> Result<(), Self::Error>
Mcp-Session-Id.Source§async fn initialize_session(
&self,
id: &SessionId,
message: ClientJsonRpcMessage,
) -> Result<ServerJsonRpcMessage, Self::Error>
async fn initialize_session( &self, id: &SessionId, message: ClientJsonRpcMessage, ) -> Result<ServerJsonRpcMessage, Self::Error>
initialize request) to the session.Source§async fn has_session(&self, id: &SessionId) -> Result<bool, Self::Error>
async fn has_session(&self, id: &SessionId) -> Result<bool, Self::Error>
true if a session with the given ID exists and is active.