fix: reduce GitHub quota use with sharing enabled #11888
MergedBil0000(GitHub profile, opens in a new tab)merged 14 commits into
main fromrefs/pull/11888/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/pullRequest/PullRequestReadCache.ts line 143Resolvedby Bil0000(GitHub profile, opens in a new tab)@@ -93,10 +105,12 @@105+ const revision = (yield* Effect.forEach(scopes, (scope) =>106+ digest(scope).pipe(Effect.flatMap((key) => Cache.get(revisions, key))),107+ )).join(":");108+ const request = new Read({ key: yield* digest(key), revision, lookup: read });109+ return (yield* Cache.get(cache, request)).payload;110+ }).pipe(111+ Effect.catchTags({ PlatformError: () => read, KeyValueStoreError: () => read }),112+ Effect.uninterruptible,113+ lock.withPermits(1),114+ );93115 }),94- // Let existing reads finish before clearing, so they cannot repopulate stale entries.95- invalidate: Cache.invalidateAll(cache.inMemory).pipe(96- Effect.andThen(backing.clear),97- Effect.catch(() => {98- enabled = false;99- return Effect.logWarning("PR cache disabled after clearing failed");100- }),101- lock.withPermits(CONCURRENT_READS),102- ),116+ invalidate: (scope) =>apps/server/src/sourceControl/GitHubCli.ts line 491Resolvedby Bil0000(GitHub profile, opens in a new tab)@@ -418,0 +470,21 @@470+ )471+ )472+ return yield* executeRaw(input);473+ const credential = yield* PinnedGitHubCredential;474+ if (credential !== null && !targetsVerifiedHost(input.args, credential.host))475+ return yield* executeRaw(input);476+ const allowReserve = input.allowReserve ?? (yield* AllowGitHubReserve);477+ const host = (478+ credential?.host ??479+ commandHosts(input.args).find((host) => host !== null) ??480+ input.rateLimitHost ??481+ input.env?.GH_HOST ??482+ globalThis.process.env.GH_HOST ??483+ "github.com"484+ ).toLowerCase();485+ const key = { provider: "github" as const, host };486+ const guarded = Effect.gen(function* () {487+ const lease = yield* limits.check(key, allowReserve ? { allowPaused: true } : undefined);488+ return yield* Effect.gen(function* () {489+ yield* Cache.get(quota, `${host}\0${credential?.credentialFingerprint ?? ""}`);490+ yield* budget.query(host, "query {}", allowReserve ? { allowReserve: true } : undefined);apps/server/src/sourceControl/GitHubCli.ts line 527Resolvedby Bil0000(GitHub profile, opens in a new tab)@@ -419,5 +501,21 @@501+ return yield* guarded.pipe(502+ Effect.catchTag("SourceControlRateLimitPausedError", (cause) =>503+ Effect.fail(504+ new GitHubCliRateLimitError({505+ command: "gh",506+ cwd: input.cwd,507+ retryAt: cause.retryAt,508+ cause,509+ }),510+ ),511+ ),512+ );513+ },514+ );515+419516 return GitHubCli.of({420517 execute,421518 listOpenPullRequests: (input) =>422519 execute({423520 cwd: input.cwd,521+ ...(input.rateLimitHost === undefined ? {} : { rateLimitHost: input.rateLimitHost }),
- apps/server/src/pullRequest/PullRequestReadCache.ts line 112OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -81,12 +103,10 @@81103 const read = yield* Effect.cached(lookup);82- return yield* cache83- .get(new Read({ key: Encoding.encodeHex(digest.value), lookup: read }))84- .pipe(85- Effect.map((result) => result.payload),86- Effect.catchTags({87- PersistenceError: () => read,88- SchemaError: () => read,89- }),90- Effect.uninterruptible,91- lock.withPermits(1),92- );104+ return yield* Effect.gen(function* () {105+ const revision = (yield* Effect.forEach(scopes, (scope) =>106+ digest(scope).pipe(Effect.flatMap((key) => Cache.get(revisions, key))),107+ )).join(":");108+ const request = new Read({ key: yield* digest(key), revision, lookup: read });109+ return (yield* Cache.get(cache, request)).payload;110+ }).pipe(111+ Effect.catchTags({ PlatformError: () => read, KeyValueStoreError: () => read }),112+ Effect.uninterruptible,apps/server/src/pullRequest/PullRequestService.ts line 2428OutdatedResolvedby Bil0000(GitHub profile, opens in a new tab)@@ -2416,8 +2421,8 @@24162421 // scope re-entering `refEpochs` after eviction can never mint a key an old entry still has.24172422 let epochCounter = 0;24182423 let listingsEpoch = 0;2419- let turnRefreshEpoch = 0;24202424 const refEpochs = new Map<string, number>();2425+ const projectEpochs = new Map<ProjectId, number>();24212426 const REF_EPOCH_CAPACITY = 2_048;24222427 const refScope = (ref: PullRequestRef) =>2423- `${ref.projectId} ${ref.host?.toLowerCase() ?? ""} ${ref.repository.toLowerCase()} ${ref.number}`;2428+ JSON.stringify([ref.projectId, ref.repository.toLowerCase(), ref.number]);apps/server/src/sourceControl/GitHubCli.ts line 484Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -418,0 +463,21 @@463+ const execute: GitHubCli["Service"]["execute"] = Effect.fn("GitHubCli.execute")(464+ function* (input) {465+ const [command, action] = input.args;466+ if (467+ !(468+ (command === "pr" && (action === "list" || action === "view")) ||469+ (command === "repo" && action === "view")470+ )471+ )472+ return yield* executeRaw(input);473+ const credential = yield* PinnedGitHubCredential;474+ if (credential !== null && !targetsVerifiedHost(input.args, credential.host))475+ return yield* executeRaw(input);476+ const allowReserve = input.allowReserve ?? (yield* AllowGitHubReserve);477+ const host = (478+ credential?.host ??479+ commandHosts(input.args).find((host) => host !== null) ??480+ input.rateLimitHost ??481+ input.env?.GH_HOST ??482+ globalThis.process.env.GH_HOST ??483+ "github.com"
- apps/server/src/sourceControl/GitHubCli.ts line 660Resolvedby Bil0000(GitHub profile, opens in a new tab)@@ -548,4 +653,5 @@548653 });549654 });550655551-export const layer = Layer.effect(GitHubCli, make);656+export const layer = Layer.effect(GitHubCli, make).pipe(657+ Layer.provideMerge(GitHubGraphQlBudget.layer),
- apps/server/src/pullRequest/PullRequestService.ts lines 2862 to 2871Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2830,13 +2858,15 @@2858+ .pipe(Effect.andThen(Effect.sync(() => bumpRefEpoch(ref)))),2859+ ),2860+ Effect.ignore,2861+ );28302862 }28312863 return Effect.sync(() => {28322864 listingsEpoch = ++epochCounter;28332865 viewersByHost.clear();28342866 }).pipe(Effect.andThen(Cache.invalidateAll(viewerFlights)));28352867 };283628682837- const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = Effect.suspend(() => {2838- turnRefreshEpoch = listingsEpoch = ++epochCounter;2839- return readCache.invalidate.pipe(2840- Effect.andThen(SubscriptionRef.set(pullRequestRefreshes, turnRefreshEpoch)),2841- );2842- });2869+ const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = (projectId) =>2870+ Effect.suspend(() => {2871+ listingsEpoch = ++epochCounter;2872+ projectEpochs.set(projectId, listingsEpoch);apps/server/src/pullRequest/PullRequestService.ts line 1802OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1783,4 +1798,5 @@17831798 .pipe(17841799 // Once the authorized provider action starts, a failure may leave partial17851800 // remote updates. Validation and permission failures above changed nothing.1786- Effect.ensuring(input.stackNumber === undefined ? Effect.void : refreshAfterTurn),1801+ Effect.ensuring(1802+ input.stackNumber === undefined ? Effect.void : refreshAfterTurn(input.projectId),
- apps/server/src/pullRequest/PullRequestService.ts line 2964Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2922,4 +2962,4 @@29222962 detail: credentialCached(detail),29232963 activity: credentialCached(activity),29242964 threadComments,2925- diff,2965+ diff: credentialCached(diff),
juliusmarminge reviewed
- apps/server/src/pullRequest/PullRequestReadCache.ts line 130OutdatedResolvedby Bil0000(GitHub profile, opens in a new tab)@@ -93,10 +119,12 @@119+ KeyValueStoreError: () => read,120+ PersistenceError: () => read,121+ SchemaError: () => read,122+ }),123+ lock.withPermits(1),124+ );93125 }),94- // Let existing reads finish before clearing, so they cannot repopulate stale entries.95- invalidate: Cache.invalidateAll(cache.inMemory).pipe(96- Effect.andThen(backing.clear),97- Effect.catch(() => {98- enabled = false;99- return Effect.logWarning("PR cache disabled after clearing failed");100- }),101- lock.withPermits(CONCURRENT_READS),102- ),126+ invalidate: (scope) =>127+ Effect.gen(function* () {128+ const key = yield* digest(scope);129+ const revision = yield* crypto.randomUUIDv4;130+ yield* backing.set(`revision:${key}`, revision);
juliusmarminge merged thisf4600d7
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed