mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
⏱️ feat: Make User Connection Idle Timeout Configurable (#10866)
This commit is contained in:
parent
fa0f2472cc
commit
e7bb987441
2 changed files with 5 additions and 3 deletions
|
|
@ -5,6 +5,7 @@ import { mcpServersRegistry as serversRegistry } from '~/mcp/registry/MCPServers
|
||||||
import { MCPConnection } from './connection';
|
import { MCPConnection } from './connection';
|
||||||
import type * as t from './types';
|
import type * as t from './types';
|
||||||
import { ConnectionsRepository } from '~/mcp/ConnectionsRepository';
|
import { ConnectionsRepository } from '~/mcp/ConnectionsRepository';
|
||||||
|
import { mcpConfig } from './mcpConfig';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for managing user-specific MCP connections with lifecycle management.
|
* Abstract base class for managing user-specific MCP connections with lifecycle management.
|
||||||
|
|
@ -20,7 +21,6 @@ export abstract class UserConnectionManager {
|
||||||
protected userConnections: Map<string, Map<string, MCPConnection>> = new Map();
|
protected userConnections: Map<string, Map<string, MCPConnection>> = new Map();
|
||||||
/** Last activity timestamp for users (not per server) */
|
/** Last activity timestamp for users (not per server) */
|
||||||
protected userLastActivity: Map<string, number> = new Map();
|
protected userLastActivity: Map<string, number> = new Map();
|
||||||
protected readonly USER_CONNECTION_IDLE_TIMEOUT = 15 * 60 * 1000; // 15 minutes (TODO: make configurable)
|
|
||||||
|
|
||||||
/** Updates the last activity timestamp for a user */
|
/** Updates the last activity timestamp for a user */
|
||||||
protected updateUserLastActivity(userId: string): void {
|
protected updateUserLastActivity(userId: string): void {
|
||||||
|
|
@ -67,7 +67,7 @@ export abstract class UserConnectionManager {
|
||||||
|
|
||||||
// Check if user is idle
|
// Check if user is idle
|
||||||
const lastActivity = this.userLastActivity.get(userId);
|
const lastActivity = this.userLastActivity.get(userId);
|
||||||
if (lastActivity && now - lastActivity > this.USER_CONNECTION_IDLE_TIMEOUT) {
|
if (lastActivity && now - lastActivity > mcpConfig.USER_CONNECTION_IDLE_TIMEOUT) {
|
||||||
logger.info(`[MCP][User: ${userId}] User idle for too long. Disconnecting all connections.`);
|
logger.info(`[MCP][User: ${userId}] User idle for too long. Disconnecting all connections.`);
|
||||||
// Disconnect all user connections
|
// Disconnect all user connections
|
||||||
try {
|
try {
|
||||||
|
|
@ -217,7 +217,7 @@ export abstract class UserConnectionManager {
|
||||||
if (currentUserId && currentUserId === userId) {
|
if (currentUserId && currentUserId === userId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (now - lastActivity > this.USER_CONNECTION_IDLE_TIMEOUT) {
|
if (now - lastActivity > mcpConfig.USER_CONNECTION_IDLE_TIMEOUT) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`[MCP][User: ${userId}] User idle for too long. Disconnecting all connections...`,
|
`[MCP][User: ${userId}] User idle for too long. Disconnecting all connections...`,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,6 @@ export const mcpConfig = {
|
||||||
OAUTH_ON_AUTH_ERROR: isEnabled(process.env.MCP_OAUTH_ON_AUTH_ERROR ?? true),
|
OAUTH_ON_AUTH_ERROR: isEnabled(process.env.MCP_OAUTH_ON_AUTH_ERROR ?? true),
|
||||||
OAUTH_DETECTION_TIMEOUT: math(process.env.MCP_OAUTH_DETECTION_TIMEOUT ?? 5000),
|
OAUTH_DETECTION_TIMEOUT: math(process.env.MCP_OAUTH_DETECTION_TIMEOUT ?? 5000),
|
||||||
CONNECTION_CHECK_TTL: math(process.env.MCP_CONNECTION_CHECK_TTL ?? 60000),
|
CONNECTION_CHECK_TTL: math(process.env.MCP_CONNECTION_CHECK_TTL ?? 60000),
|
||||||
|
/** Idle timeout (ms) after which user connections are disconnected. Default: 15 minutes */
|
||||||
|
USER_CONNECTION_IDLE_TIMEOUT: math(process.env.MCP_USER_CONNECTION_IDLE_TIMEOUT ?? 15 * 60 * 1000),
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue