feat: 会话满时 LRU 驱逐空闲会话,cloud 命令透传参数
This commit is contained in:
parent
40a243a94e
commit
8505fce51a
|
|
@ -6650,8 +6650,9 @@ async function run(): Promise<CommanderCommand> {
|
||||||
|
|
||||||
// Cloud command - manage cs-cloud daemon (download binary + forward commands)
|
// Cloud command - manage cs-cloud daemon (download binary + forward commands)
|
||||||
program
|
program
|
||||||
.command('cloud [args...]')
|
.command('cloud [args...]', { noHelp: true })
|
||||||
.description('Manage cloud daemon (register device and connect via WebSocket tunnel)')
|
.description('Manage cloud daemon (register device and connect via WebSocket tunnel)')
|
||||||
|
.allowUnknownOption()
|
||||||
.action(async (args: string[]) => {
|
.action(async (args: string[]) => {
|
||||||
const { cloudHandler } = await import('./cli/handlers/cloud.js')
|
const { cloudHandler } = await import('./cli/handlers/cloud.js')
|
||||||
await cloudHandler(args)
|
await cloudHandler(args)
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,31 @@ export class SessionManager {
|
||||||
}, checkInterval)
|
}, checkInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private evictLRU(): string | null {
|
||||||
|
let oldestId: string | null = null
|
||||||
|
let oldestTime = Infinity
|
||||||
|
for (const [id, handle] of this.sessions) {
|
||||||
|
const info = handle.getInfo()
|
||||||
|
if (info.status !== 'running') continue
|
||||||
|
if (handle.busyStatus.type !== 'idle') continue
|
||||||
|
if (info.last_active_at < oldestTime) {
|
||||||
|
oldestTime = info.last_active_at
|
||||||
|
oldestId = id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!oldestId) return null
|
||||||
|
const handle = this.sessions.get(oldestId)!
|
||||||
|
handle.kill()
|
||||||
|
this.eventBus.publishSessionEvent(oldestId, 'deleted', {
|
||||||
|
status: 'stopped',
|
||||||
|
reason: 'evicted',
|
||||||
|
})
|
||||||
|
this.sessions.delete(oldestId)
|
||||||
|
this.eventBus.unregisterSessionCwd(oldestId)
|
||||||
|
this.scheduleIndexSave()
|
||||||
|
return oldestId
|
||||||
|
}
|
||||||
|
|
||||||
getActiveCount(): number {
|
getActiveCount(): number {
|
||||||
return this.sessions.size
|
return this.sessions.size
|
||||||
}
|
}
|
||||||
|
|
@ -157,9 +182,12 @@ export class SessionManager {
|
||||||
sessionId?: string
|
sessionId?: string
|
||||||
}): Promise<SessionHandle> {
|
}): Promise<SessionHandle> {
|
||||||
if (this.maxSessions > 0 && this.sessions.size >= this.maxSessions) {
|
if (this.maxSessions > 0 && this.sessions.size >= this.maxSessions) {
|
||||||
throw new Error(
|
const evicted = this.evictLRU()
|
||||||
`Maximum concurrent sessions reached (${this.maxSessions})`,
|
if (!evicted) {
|
||||||
)
|
throw new Error(
|
||||||
|
`Maximum concurrent sessions reached (${this.maxSessions})`,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionId = opts.sessionId ?? crypto.randomUUID()
|
const sessionId = opts.sessionId ?? crypto.randomUUID()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user