mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 06:21:59 +09:00
Merge PR #4682 into release/3.71.1
This commit is contained in:
commit
d3c5244eae
3 files changed
+83
No files matched your search
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* #4674: every initialize created a session, and the server only ever answers
|
||||
* through the newest one (currentSession). Superseded sessions were never
|
||||
* addressed again but held a slot until the 30-minute expiry, so 100
|
||||
* short-lived clients exhausted maxSessions and the next initialize failed.
|
||||
*/
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { createMCPServer, createSessionManager } from '../src/index.js';
|
||||
import type { ILogger } from '../src/types.js';
|
||||
|
||||
const logger: ILogger = { debug() {}, info() {}, warn() {}, error() {} } as unknown as ILogger;
|
||||
|
||||
describe('SessionManager.reserveCapacity (#4674)', () => {
|
||||
it('does nothing while there is room', () => {
|
||||
const manager = createSessionManager(logger, { maxSessions: 2 });
|
||||
const a = manager.createSession('http');
|
||||
expect(manager.reserveCapacity()).toBeUndefined();
|
||||
expect(manager.getSession(a.id)).toBeDefined();
|
||||
manager.destroy();
|
||||
});
|
||||
|
||||
it('closes the least recently active session other than the kept one', () => {
|
||||
const manager = createSessionManager(logger, { maxSessions: 2 });
|
||||
const oldest = manager.createSession('http');
|
||||
const kept = manager.createSession('http');
|
||||
oldest.lastActivityAt = new Date(Date.now() - 60_000);
|
||||
kept.lastActivityAt = new Date(Date.now() - 120_000); // older still, but protected
|
||||
|
||||
expect(manager.reserveCapacity(kept.id)).toBe(oldest.id);
|
||||
expect(manager.getSession(oldest.id)).toBeUndefined();
|
||||
expect(manager.getSession(kept.id)).toBeDefined();
|
||||
expect(() => manager.createSession('http')).not.toThrow();
|
||||
manager.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('MCPServer initialize past maxSessions (#4674)', () => {
|
||||
let server: ReturnType<typeof createMCPServer>;
|
||||
afterEach(async () => { await server.stop(); });
|
||||
|
||||
it('keeps accepting short-lived clients and still serves requests', async () => {
|
||||
server = createMCPServer({ name: 'Test Server', transport: 'in-process' }, logger);
|
||||
const handle = (request: Record<string, unknown>) =>
|
||||
(server as unknown as { handleRequest(r: unknown): Promise<any> }).handleRequest({ jsonrpc: '2.0', ...request });
|
||||
|
||||
for (let i = 0; i < 105; i++) {
|
||||
const res = await handle({
|
||||
id: i,
|
||||
method: 'initialize',
|
||||
params: { protocolVersion: '2025-11-25', capabilities: {}, clientInfo: { name: `client-${i}`, version: '1' } },
|
||||
});
|
||||
expect(res.error, `initialize #${i}`).toBeUndefined();
|
||||
}
|
||||
|
||||
expect(server.getSessions().length).toBeLessThanOrEqual(100);
|
||||
const list = await handle({ id: 'list', method: 'tools/list', params: {} });
|
||||
expect(list.error).toBeUndefined();
|
||||
expect(Array.isArray(list.result?.tools)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -515,6 +515,11 @@ export class MCPServer extends EventEmitter implements IMCPServer {
|
||||
);
|
||||
}
|
||||
|
||||
// #4674: the server answers every client through one currentSession, so a
|
||||
// session it has superseded is never addressed again; it only holds a slot
|
||||
// until expiry. Short-lived clients exhausted maxSessions that way. Retire
|
||||
// the least recently active superseded session instead of refusing.
|
||||
this.sessionManager.reserveCapacity(this.currentSession?.id);
|
||||
const session = this.sessionManager.createSession(this.config.transport);
|
||||
this.sessionManager.initializeSession(session.id, params);
|
||||
this.currentSession = session;
|
||||
@@ -1045,6 +1050,7 @@ export class MCPServer extends EventEmitter implements IMCPServer {
|
||||
return this.currentSession;
|
||||
}
|
||||
|
||||
this.sessionManager.reserveCapacity();
|
||||
const session = this.sessionManager.createSession(this.config.transport);
|
||||
this.currentSession = session;
|
||||
return session;
|
||||
|
||||
@@ -201,6 +201,23 @@ export class SessionManager extends EventEmitter {
|
||||
return this.closeSession(sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* When at capacity, close the least recently active session other than
|
||||
* `keepId` so one more can be created. Returns the closed session's id, or
|
||||
* undefined when there was room or nothing else to close (#4674).
|
||||
*/
|
||||
reserveCapacity(keepId?: string): string | undefined {
|
||||
if (this.sessions.size < this.config.maxSessions) return undefined;
|
||||
let oldest: MCPSession | undefined;
|
||||
for (const session of this.sessions.values()) {
|
||||
if (session.id === keepId) continue;
|
||||
if (!oldest || session.lastActivityAt < oldest.lastActivityAt) oldest = session;
|
||||
}
|
||||
if (!oldest) return undefined;
|
||||
this.closeSession(oldest.id, 'Evicted to make room for a new session');
|
||||
return oldest.id;
|
||||
}
|
||||
|
||||
getSessionMetrics(): SessionMetrics {
|
||||
let authenticated = 0;
|
||||
let active = 0;
|
||||
|
||||
Reference in new issue
Block a user