2026-02-08 13:57:26 +09:00
|
|
|
import type { ResolvedServer } from "./types"
|
2026-02-08 17:34:47 +09:00
|
|
|
import { registerLspManagerProcessCleanup } from "./lsp-manager-process-cleanup"
|
|
|
|
|
import { cleanupTempDirectoryLspClients } from "./lsp-manager-temp-directory-cleanup"
|
2026-02-08 13:57:26 +09:00
|
|
|
import { LSPClient } from "./lsp-client"
|
|
|
|
|
interface ManagedClient {
|
|
|
|
|
client: LSPClient
|
|
|
|
|
lastUsedAt: number
|
|
|
|
|
refCount: number
|
|
|
|
|
initPromise?: Promise<void>
|
|
|
|
|
isInitializing: boolean
|
2026-02-08 17:34:47 +09:00
|
|
|
initializingSince?: number
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
class LSPServerManager {
|
|
|
|
|
private static instance: LSPServerManager
|
|
|
|
|
private clients = new Map<string, ManagedClient>()
|
|
|
|
|
private cleanupInterval: ReturnType<typeof setInterval> | null = null
|
|
|
|
|
private readonly IDLE_TIMEOUT = 5 * 60 * 1000
|
2026-02-08 17:34:47 +09:00
|
|
|
private readonly INIT_TIMEOUT = 60 * 1000
|
2026-02-08 13:57:26 +09:00
|
|
|
private constructor() {
|
|
|
|
|
this.startCleanupTimer()
|
|
|
|
|
this.registerProcessCleanup()
|
|
|
|
|
}
|
|
|
|
|
private registerProcessCleanup(): void {
|
2026-02-08 17:34:47 +09:00
|
|
|
registerLspManagerProcessCleanup({
|
|
|
|
|
getClients: () => this.clients.entries(),
|
|
|
|
|
clearClients: () => {
|
|
|
|
|
this.clients.clear()
|
|
|
|
|
},
|
|
|
|
|
clearCleanupInterval: () => {
|
|
|
|
|
if (this.cleanupInterval) {
|
|
|
|
|
clearInterval(this.cleanupInterval)
|
|
|
|
|
this.cleanupInterval = null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getInstance(): LSPServerManager {
|
|
|
|
|
if (!LSPServerManager.instance) {
|
|
|
|
|
LSPServerManager.instance = new LSPServerManager()
|
|
|
|
|
}
|
|
|
|
|
return LSPServerManager.instance
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getKey(root: string, serverId: string): string {
|
|
|
|
|
return `${root}::${serverId}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private startCleanupTimer(): void {
|
|
|
|
|
if (this.cleanupInterval) return
|
|
|
|
|
this.cleanupInterval = setInterval(() => {
|
|
|
|
|
this.cleanupIdleClients()
|
|
|
|
|
}, 60000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private cleanupIdleClients(): void {
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
for (const [key, managed] of this.clients) {
|
|
|
|
|
if (managed.refCount === 0 && now - managed.lastUsedAt > this.IDLE_TIMEOUT) {
|
|
|
|
|
managed.client.stop()
|
|
|
|
|
this.clients.delete(key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getClient(root: string, server: ResolvedServer): Promise<LSPClient> {
|
|
|
|
|
const key = this.getKey(root, server.id)
|
|
|
|
|
let managed = this.clients.get(key)
|
2026-02-08 17:34:47 +09:00
|
|
|
if (managed) {
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
if (
|
|
|
|
|
managed.isInitializing &&
|
|
|
|
|
managed.initializingSince !== undefined &&
|
|
|
|
|
now - managed.initializingSince >= this.INIT_TIMEOUT
|
|
|
|
|
) {
|
|
|
|
|
// Stale init can permanently block subsequent calls (e.g., LSP process hang)
|
|
|
|
|
try {
|
|
|
|
|
await managed.client.stop()
|
|
|
|
|
} catch {}
|
|
|
|
|
this.clients.delete(key)
|
|
|
|
|
managed = undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-08 13:57:26 +09:00
|
|
|
if (managed) {
|
|
|
|
|
if (managed.initPromise) {
|
2026-02-08 17:34:47 +09:00
|
|
|
try {
|
|
|
|
|
await managed.initPromise
|
|
|
|
|
} catch {
|
|
|
|
|
// Failed init should not keep the key blocked forever.
|
|
|
|
|
try {
|
|
|
|
|
await managed.client.stop()
|
|
|
|
|
} catch {}
|
|
|
|
|
this.clients.delete(key)
|
|
|
|
|
managed = undefined
|
|
|
|
|
}
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
2026-02-08 17:34:47 +09:00
|
|
|
|
|
|
|
|
if (managed) {
|
|
|
|
|
if (managed.client.isAlive()) {
|
|
|
|
|
managed.refCount++
|
|
|
|
|
managed.lastUsedAt = Date.now()
|
|
|
|
|
return managed.client
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await managed.client.stop()
|
|
|
|
|
} catch {}
|
|
|
|
|
this.clients.delete(key)
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const client = new LSPClient(root, server)
|
|
|
|
|
const initPromise = (async () => {
|
|
|
|
|
await client.start()
|
|
|
|
|
await client.initialize()
|
|
|
|
|
})()
|
2026-02-08 17:34:47 +09:00
|
|
|
const initStartedAt = Date.now()
|
2026-02-08 13:57:26 +09:00
|
|
|
this.clients.set(key, {
|
|
|
|
|
client,
|
2026-02-08 17:34:47 +09:00
|
|
|
lastUsedAt: initStartedAt,
|
2026-02-08 13:57:26 +09:00
|
|
|
refCount: 1,
|
|
|
|
|
initPromise,
|
|
|
|
|
isInitializing: true,
|
2026-02-08 17:34:47 +09:00
|
|
|
initializingSince: initStartedAt,
|
2026-02-08 13:57:26 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-08 17:34:47 +09:00
|
|
|
try {
|
|
|
|
|
await initPromise
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.clients.delete(key)
|
|
|
|
|
try {
|
|
|
|
|
await client.stop()
|
|
|
|
|
} catch {}
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2026-02-08 13:57:26 +09:00
|
|
|
const m = this.clients.get(key)
|
|
|
|
|
if (m) {
|
|
|
|
|
m.initPromise = undefined
|
|
|
|
|
m.isInitializing = false
|
2026-02-08 17:34:47 +09:00
|
|
|
m.initializingSince = undefined
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
warmupClient(root: string, server: ResolvedServer): void {
|
|
|
|
|
const key = this.getKey(root, server.id)
|
|
|
|
|
if (this.clients.has(key)) return
|
|
|
|
|
const client = new LSPClient(root, server)
|
|
|
|
|
const initPromise = (async () => {
|
|
|
|
|
await client.start()
|
|
|
|
|
await client.initialize()
|
|
|
|
|
})()
|
|
|
|
|
|
2026-02-08 17:34:47 +09:00
|
|
|
const initStartedAt = Date.now()
|
2026-02-08 13:57:26 +09:00
|
|
|
this.clients.set(key, {
|
|
|
|
|
client,
|
2026-02-08 17:34:47 +09:00
|
|
|
lastUsedAt: initStartedAt,
|
2026-02-08 13:57:26 +09:00
|
|
|
refCount: 0,
|
|
|
|
|
initPromise,
|
|
|
|
|
isInitializing: true,
|
2026-02-08 17:34:47 +09:00
|
|
|
initializingSince: initStartedAt,
|
2026-02-08 13:57:26 +09:00
|
|
|
})
|
|
|
|
|
|
2026-02-08 17:34:47 +09:00
|
|
|
initPromise
|
|
|
|
|
.then(() => {
|
|
|
|
|
const m = this.clients.get(key)
|
|
|
|
|
if (m) {
|
|
|
|
|
m.initPromise = undefined
|
|
|
|
|
m.isInitializing = false
|
|
|
|
|
m.initializingSince = undefined
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// Warmup failures must not permanently block future initialization.
|
|
|
|
|
this.clients.delete(key)
|
|
|
|
|
void client.stop().catch(() => {})
|
|
|
|
|
})
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
releaseClient(root: string, serverId: string): void {
|
|
|
|
|
const key = this.getKey(root, serverId)
|
|
|
|
|
const managed = this.clients.get(key)
|
|
|
|
|
if (managed && managed.refCount > 0) {
|
|
|
|
|
managed.refCount--
|
|
|
|
|
managed.lastUsedAt = Date.now()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isServerInitializing(root: string, serverId: string): boolean {
|
|
|
|
|
const key = this.getKey(root, serverId)
|
|
|
|
|
const managed = this.clients.get(key)
|
|
|
|
|
return managed?.isInitializing ?? false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async stopAll(): Promise<void> {
|
|
|
|
|
for (const [, managed] of this.clients) {
|
|
|
|
|
await managed.client.stop()
|
|
|
|
|
}
|
|
|
|
|
this.clients.clear()
|
|
|
|
|
if (this.cleanupInterval) {
|
|
|
|
|
clearInterval(this.cleanupInterval)
|
|
|
|
|
this.cleanupInterval = null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async cleanupTempDirectoryClients(): Promise<void> {
|
2026-02-08 17:34:47 +09:00
|
|
|
await cleanupTempDirectoryLspClients(this.clients)
|
2026-02-08 13:57:26 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const lspManager = LSPServerManager.getInstance()
|