|
| 1 | +import { createGitClient } from "./client.js"; |
| 2 | +import { removeLock, waitForUnlock } from "./lock-detector.js"; |
| 3 | +import { AsyncReaderWriterLock } from "./rw-lock.js"; |
| 4 | + |
| 5 | +interface RepoState { |
| 6 | + lock: AsyncReaderWriterLock; |
| 7 | + lastAccess: number; |
| 8 | +} |
| 9 | + |
| 10 | +export interface ExecuteOptions { |
| 11 | + signal?: AbortSignal; |
| 12 | + timeoutMs?: number; |
| 13 | + waitForExternalLock?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +class GitOperationManagerImpl { |
| 17 | + private repoStates = new Map<string, RepoState>(); |
| 18 | + private cleanupInterval: ReturnType<typeof setInterval> | null = null; |
| 19 | + private static readonly CLEANUP_INTERVAL_MS = 60000; |
| 20 | + private static readonly IDLE_TIMEOUT_MS = 300000; |
| 21 | + |
| 22 | + constructor() { |
| 23 | + this.cleanupInterval = setInterval( |
| 24 | + () => this.cleanupIdleRepos(), |
| 25 | + GitOperationManagerImpl.CLEANUP_INTERVAL_MS, |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + private getRepoState(repoPath: string): RepoState { |
| 30 | + let state = this.repoStates.get(repoPath); |
| 31 | + if (!state) { |
| 32 | + state = { lock: new AsyncReaderWriterLock(), lastAccess: Date.now() }; |
| 33 | + this.repoStates.set(repoPath, state); |
| 34 | + } |
| 35 | + state.lastAccess = Date.now(); |
| 36 | + return state; |
| 37 | + } |
| 38 | + |
| 39 | + private cleanupIdleRepos(): void { |
| 40 | + const now = Date.now(); |
| 41 | + for (const [repoPath, state] of this.repoStates) { |
| 42 | + if (now - state.lastAccess > GitOperationManagerImpl.IDLE_TIMEOUT_MS) { |
| 43 | + this.repoStates.delete(repoPath); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + async executeRead<T>( |
| 49 | + repoPath: string, |
| 50 | + operation: (git: ReturnType<typeof createGitClient>) => Promise<T>, |
| 51 | + options?: ExecuteOptions, |
| 52 | + ): Promise<T> { |
| 53 | + const git = createGitClient(repoPath, { |
| 54 | + abortSignal: options?.signal, |
| 55 | + }).env({ GIT_OPTIONAL_LOCKS: "0" }); |
| 56 | + return operation(git); |
| 57 | + } |
| 58 | + |
| 59 | + async executeWrite<T>( |
| 60 | + repoPath: string, |
| 61 | + operation: (git: ReturnType<typeof createGitClient>) => Promise<T>, |
| 62 | + options?: ExecuteOptions, |
| 63 | + ): Promise<T> { |
| 64 | + const state = this.getRepoState(repoPath); |
| 65 | + |
| 66 | + if (options?.waitForExternalLock !== false) { |
| 67 | + const unlocked = await waitForUnlock( |
| 68 | + repoPath, |
| 69 | + options?.timeoutMs ?? 10000, |
| 70 | + ); |
| 71 | + if (!unlocked) { |
| 72 | + throw new Error(`Git repository is locked: ${repoPath}`); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + await state.lock.acquireWrite(); |
| 77 | + try { |
| 78 | + const git = createGitClient(repoPath, { abortSignal: options?.signal }); |
| 79 | + return await operation(git); |
| 80 | + } catch (error) { |
| 81 | + if (options?.signal?.aborted) { |
| 82 | + await removeLock(repoPath).catch(() => {}); |
| 83 | + } |
| 84 | + throw error; |
| 85 | + } finally { |
| 86 | + state.lock.releaseWrite(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + destroy(): void { |
| 91 | + if (this.cleanupInterval) { |
| 92 | + clearInterval(this.cleanupInterval); |
| 93 | + this.cleanupInterval = null; |
| 94 | + } |
| 95 | + this.repoStates.clear(); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +let instance: GitOperationManagerImpl | null = null; |
| 100 | + |
| 101 | +export function getGitOperationManager(): GitOperationManagerImpl { |
| 102 | + if (!instance) { |
| 103 | + instance = new GitOperationManagerImpl(); |
| 104 | + } |
| 105 | + return instance; |
| 106 | +} |
| 107 | + |
| 108 | +export function resetGitOperationManager(): void { |
| 109 | + if (instance) { |
| 110 | + instance.destroy(); |
| 111 | + instance = null; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export type GitOperationManager = GitOperationManagerImpl; |
0 commit comments