fix: prune github owner repo cache (#4081)
This commit is contained in:
@@ -15,6 +15,7 @@ vi.mock('../providers/ssh-git-dispatch', () => ({
|
||||
}))
|
||||
|
||||
import {
|
||||
_getOwnerRepoCacheSize,
|
||||
_resetOwnerRepoCache,
|
||||
classifyGhError,
|
||||
classifyListIssuesError,
|
||||
@@ -166,6 +167,29 @@ describe('github owner/repo resolution', () => {
|
||||
await expect(getOwnerRepo('/repo', 'ssh-1')).resolves.toEqual({ owner: 'remote', repo: 'orca' })
|
||||
})
|
||||
|
||||
it('prunes expired distinct owner/repo cache entries on later lookups', async () => {
|
||||
const nowSpy = vi.spyOn(Date, 'now')
|
||||
try {
|
||||
nowSpy.mockReturnValue(1_000)
|
||||
gitExecFileAsyncMock.mockResolvedValueOnce({
|
||||
stdout: 'git@github.com:stablyai/orca.git\n'
|
||||
})
|
||||
await expect(getOwnerRepo('/repo-a')).resolves.toEqual({ owner: 'stablyai', repo: 'orca' })
|
||||
expect(_getOwnerRepoCacheSize()).toBe(1)
|
||||
|
||||
nowSpy.mockReturnValue(32_000)
|
||||
gitExecFileAsyncMock.mockResolvedValueOnce({
|
||||
stdout: 'git@github.com:acme/widgets.git\n'
|
||||
})
|
||||
await expect(getOwnerRepo('/repo-b')).resolves.toEqual({ owner: 'acme', repo: 'widgets' })
|
||||
|
||||
expect(_getOwnerRepoCacheSize()).toBe(1)
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(2)
|
||||
} finally {
|
||||
nowSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('resolves PR candidates as upstream then origin and de-dupes matching slugs', async () => {
|
||||
gitExecFileAsyncMock
|
||||
.mockResolvedValueOnce({ stdout: 'git@github.com:Acme/Orca.git\n' })
|
||||
|
||||
@@ -138,6 +138,7 @@ export function ghRepoExecOptions(context: GitHubRepoContext): {
|
||||
}
|
||||
|
||||
const OWNER_REPO_CACHE_TTL_MS = 30_000
|
||||
const OWNER_REPO_CACHE_MAX_ENTRIES = 512
|
||||
|
||||
type OwnerRepoCacheEntry = {
|
||||
value: OwnerRepo | null
|
||||
@@ -151,6 +152,26 @@ export function _resetOwnerRepoCache(): void {
|
||||
ownerRepoCache.clear()
|
||||
}
|
||||
|
||||
/** @internal — exposed for tests only */
|
||||
export function _getOwnerRepoCacheSize(): number {
|
||||
return ownerRepoCache.size
|
||||
}
|
||||
|
||||
function pruneOwnerRepoCache(now: number): void {
|
||||
for (const [key, entry] of ownerRepoCache) {
|
||||
if (entry.expiresAt <= now) {
|
||||
ownerRepoCache.delete(key)
|
||||
}
|
||||
}
|
||||
while (ownerRepoCache.size > OWNER_REPO_CACHE_MAX_ENTRIES) {
|
||||
const oldestKey = ownerRepoCache.keys().next().value
|
||||
if (oldestKey === undefined) {
|
||||
return
|
||||
}
|
||||
ownerRepoCache.delete(oldestKey)
|
||||
}
|
||||
}
|
||||
|
||||
export function parseGitHubOwnerRepo(remoteUrl: string): OwnerRepo | null {
|
||||
const identity = parseGitHubRemoteIdentity(remoteUrl)
|
||||
if (!identity || identity.host.toLowerCase() !== 'github.com') {
|
||||
@@ -222,27 +243,28 @@ export async function getOwnerRepoForRemote(
|
||||
): Promise<OwnerRepo | null> {
|
||||
const context = githubRepoContext(repoPath, connectionId)
|
||||
const cacheKey = `${context.connectionId ?? 'local'}\0${context.repoPath}\0${remoteName}`
|
||||
const now = Date.now()
|
||||
pruneOwnerRepoCache(now)
|
||||
const cached = ownerRepoCache.get(cacheKey)
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
if (cached && cached.expiresAt > now) {
|
||||
return cached.value
|
||||
}
|
||||
if (cached) {
|
||||
ownerRepoCache.delete(cacheKey)
|
||||
}
|
||||
try {
|
||||
const remoteUrl = await getRemoteUrlForRepo(context, remoteName)
|
||||
const result = remoteUrl ? parseGitHubOwnerRepo(remoteUrl) : null
|
||||
if (result) {
|
||||
ownerRepoCache.set(cacheKey, {
|
||||
value: result,
|
||||
expiresAt: Date.now() + OWNER_REPO_CACHE_TTL_MS
|
||||
expiresAt: now + OWNER_REPO_CACHE_TTL_MS
|
||||
})
|
||||
pruneOwnerRepoCache(now)
|
||||
return result
|
||||
}
|
||||
} catch {
|
||||
// ignore — non-GitHub remote or no remote
|
||||
}
|
||||
ownerRepoCache.set(cacheKey, { value: null, expiresAt: Date.now() + OWNER_REPO_CACHE_TTL_MS })
|
||||
ownerRepoCache.set(cacheKey, { value: null, expiresAt: now + OWNER_REPO_CACHE_TTL_MS })
|
||||
pruneOwnerRepoCache(now)
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user