Fix git probe fanout during concurrent refreshes (#4670)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -137,6 +137,30 @@ describe('github owner/repo resolution', () => {
|
||||
await expect(getIssueOwnerRepo('/repo')).resolves.toEqual({ owner: 'stablyai', repo: 'orca' })
|
||||
})
|
||||
|
||||
it('coalesces concurrent missing remote probes for the same repo and remote', async () => {
|
||||
gitExecFileAsyncMock.mockImplementation(async () => {
|
||||
await Promise.resolve()
|
||||
throw new Error("error: No such remote 'upstream'")
|
||||
})
|
||||
|
||||
await expect(
|
||||
Promise.all([
|
||||
getOwnerRepoForRemote('/repo', 'upstream'),
|
||||
getOwnerRepoForRemote('/repo', 'upstream'),
|
||||
getOwnerRepoForRemote('/repo', 'upstream'),
|
||||
getOwnerRepoForRemote('/repo', 'upstream')
|
||||
])
|
||||
).resolves.toEqual([null, null, null, null])
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(1)
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['remote', 'get-url', 'upstream'], {
|
||||
cwd: '/repo'
|
||||
})
|
||||
|
||||
await expect(getOwnerRepoForRemote('/repo', 'upstream')).resolves.toBeNull()
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('resolves SSH repo remotes through the registered SSH git provider', async () => {
|
||||
const sshProvider = {
|
||||
exec: vi.fn().mockResolvedValue({ stdout: 'git@github.com:stablyai/orca.git\n', stderr: '' })
|
||||
|
||||
@@ -146,10 +146,12 @@ type OwnerRepoCacheEntry = {
|
||||
}
|
||||
|
||||
const ownerRepoCache = new Map<string, OwnerRepoCacheEntry>()
|
||||
const ownerRepoInFlight = new Map<string, Promise<OwnerRepo | null>>()
|
||||
|
||||
/** @internal — exposed for tests only */
|
||||
export function _resetOwnerRepoCache(): void {
|
||||
ownerRepoCache.clear()
|
||||
ownerRepoInFlight.clear()
|
||||
}
|
||||
|
||||
/** @internal — exposed for tests only */
|
||||
@@ -249,6 +251,32 @@ export async function getOwnerRepoForRemote(
|
||||
if (cached && cached.expiresAt > now) {
|
||||
return cached.value
|
||||
}
|
||||
|
||||
const inFlight = ownerRepoInFlight.get(cacheKey)
|
||||
if (inFlight) {
|
||||
return inFlight
|
||||
}
|
||||
|
||||
// Why: startup can resolve issue sources, PR candidates, and repo metadata
|
||||
// for the same repo concurrently. Coalesce missing-remote probes so a stable
|
||||
// absent upstream does not spawn identical `git remote get-url` processes.
|
||||
const probe = resolveOwnerRepoForRemote(context, remoteName, cacheKey)
|
||||
ownerRepoInFlight.set(cacheKey, probe)
|
||||
try {
|
||||
return await probe
|
||||
} finally {
|
||||
if (ownerRepoInFlight.get(cacheKey) === probe) {
|
||||
ownerRepoInFlight.delete(cacheKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveOwnerRepoForRemote(
|
||||
context: GitHubRepoContext,
|
||||
remoteName: string,
|
||||
cacheKey: string
|
||||
): Promise<OwnerRepo | null> {
|
||||
const now = Date.now()
|
||||
try {
|
||||
const remoteUrl = await getRemoteUrlForRepo(context, remoteName)
|
||||
const result = remoteUrl ? parseGitHubOwnerRepo(remoteUrl) : null
|
||||
|
||||
Reference in New Issue
Block a user