Skip to content

Commit d720210

Browse files
refactor(server): GitHub rate limits read the response headers (#16986)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 42d92ec commit d720210

10 files changed

Lines changed: 350 additions & 525 deletions

‎apps/server/scripts/measure-pr-preview.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Run with: node apps/server/scripts/measure-pr-preview.ts owner/repo 123 456
22
// Numbers form a session with shared repository-permission caches. Browser and
33
// service caches are excluded. Uses real GitHub reads, without a server or database.
4-
// Every GraphQL read carries `rateLimit`, so its cost is read off its own answer.
4+
// Each GraphQL read asks for `rateLimit` here, so its cost is read off its own answer.
55
import * as NodeServices from "@effect/platform-node/NodeServices";
66
import * as Effect from "effect/Effect";
77
import * as Console from "effect/Console";
@@ -15,7 +15,7 @@ import * as GitHubPullRequestProvider from "../src/pullRequest/GitHubPullRequest
1515
import * as GitHubApi from "../src/sourceControl/GitHubApi.ts";
1616
import * as GitHubCredentials from "../src/sourceControl/GitHubCredentials.ts";
1717
import * as ServerSettings from "../src/serverSettings.ts";
18-
import * as GitHubGraphQlBudget from "../src/sourceControl/githubGraphQlBudget.ts";
18+
import * as GitHubQuota from "../src/sourceControl/githubQuota.ts";
1919
import * as SourceControlRateLimit from "../src/sourceControl/SourceControlRateLimit.ts";
2020
import * as VcsProcess from "../src/vcs/VcsProcess.ts";
2121

@@ -33,14 +33,22 @@ const decodeCost = Schema.decodeUnknownOption(
3333
}),
3434
),
3535
);
36+
/** The query with GitHub's own count of what it cost, for a read rather than a mutation. */
37+
function withRateLimit(query: string): string {
38+
const end = query.lastIndexOf("}");
39+
return query.trimStart().startsWith("mutation") || end === -1
40+
? query
41+
: `${query.slice(0, end)}\n rateLimit { cost }\n${query.slice(end)}`;
42+
}
43+
3644
const measuredApi = Layer.effect(
3745
GitHubApi.GitHubApi,
3846
Effect.gen(function* () {
3947
const api = yield* GitHubApi.make;
4048
return GitHubApi.GitHubApi.of({
4149
...api,
4250
graphql: (input) =>
43-
api.graphql(input).pipe(
51+
api.graphql({ ...input, query: withRateLimit(input.query) }).pipe(
4452
Effect.tap((body) =>
4553
Effect.sync(() =>
4654
reads.push({
@@ -66,7 +74,7 @@ const measuredApi = Layer.effect(
6674
}),
6775
).pipe(
6876
Layer.provide(GitHubCredentials.layer.pipe(Layer.provide(ServerSettings.layerTest()))),
69-
Layer.provide(GitHubGraphQlBudget.layer),
77+
Layer.provide(GitHubQuota.layer),
7078
Layer.provide(SourceControlRateLimit.layer),
7179
Layer.provide(FetchHttpClient.layer),
7280
Layer.provide(VcsProcess.layer),

‎apps/server/src/pullRequest/GitHubPullRequestApi.test.ts‎

Lines changed: 49 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { HttpClient, HttpClientResponse } from "effect/http";
1515
import { AllowGitHubReserve } from "../sourceControl/GitHubApi.ts";
1616
import * as GitHubApi from "../sourceControl/GitHubApi.ts";
1717
import * as GitHubCredentials from "../sourceControl/GitHubCredentials.ts";
18-
import * as GitHubGraphQlBudget from "../sourceControl/githubGraphQlBudget.ts";
18+
import * as GitHubQuota from "../sourceControl/githubQuota.ts";
1919
import * as SourceControlRateLimit from "../sourceControl/SourceControlRateLimit.ts";
2020
import * as VcsProcess from "../vcs/VcsProcess.ts";
2121
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
@@ -69,26 +69,29 @@ const mockedStackMemberships = vi.fn<(call: ApiCall) => ApiAnswer>(() =>
6969
);
7070

7171
/**
72-
* A transport that answers from the mocks above but spends the real GraphQL budget, the way
73-
* GitHubApi does, so the reserve and pause behaviour is still the module's to prove.
72+
* A transport that answers from the mocks above but spends the real GraphQL quota from each
73+
* answer's headers, the way GitHubApi does, so the reserve behaviour is still the module's to prove.
7474
*/
7575
const mockApi = Layer.effect(
7676
GitHubApi.GitHubApi,
7777
Effect.gen(function* () {
78-
const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
78+
const quota = yield* GitHubQuota.GitHubQuota;
7979
return GitHubApi.GitHubApi.of({
8080
graphql: (input) =>
81-
budget
82-
.query(input.host, input.query, input.allowReserve ? { allowReserve: true } : undefined)
83-
.pipe(
84-
Effect.flatMap((query) =>
85-
(input.query.includes("query PullRequestStackMemberships")
86-
? mockedStackMemberships
87-
: mockedExecute)({ kind: "graphql", ...input, query }),
88-
),
89-
Effect.map((answer) => answer.body),
90-
Effect.tap((body) => budget.observe(input.host, body)),
81+
GitHubApi.AllowGitHubReserve.pipe(
82+
Effect.flatMap((interactive) =>
83+
quota.admit(input.host, "graphql", {
84+
allowReserve: input.allowReserve ?? interactive,
85+
}),
9186
),
87+
Effect.andThen(() =>
88+
(input.query.includes("query PullRequestStackMemberships")
89+
? mockedStackMemberships
90+
: mockedExecute)({ kind: "graphql", ...input }),
91+
),
92+
Effect.tap((answer) => quota.observe(input.host, answer.headers)),
93+
Effect.map((answer) => answer.body),
94+
),
9295
rest: (input) => mockedExecute({ kind: "rest", ...input }),
9396
credential: (host) => mockedCredential(host),
9497
});
@@ -98,7 +101,7 @@ const mockApi = Layer.effect(
98101
const layer = it.layer(
99102
GitHubPullRequestApi.layer.pipe(
100103
Layer.provideMerge(mockApi),
101-
Layer.provideMerge(GitHubGraphQlBudget.layer),
104+
Layer.provideMerge(GitHubQuota.layer),
102105
Layer.provide(NodeCrypto.layer),
103106
Layer.provide(VcsProcess.layer.pipe(Layer.provideMerge(NodeServices.layer))),
104107
),
@@ -109,8 +112,19 @@ function output(
109112
body: string,
110113
truncated = false,
111114
invalidUtf8 = false,
115+
headers: Readonly<Record<string, string>> = {},
112116
): GitHubApi.GitHubRestResponse {
113-
return { status: 200, headers: {}, body, truncated, invalidUtf8 };
117+
return { status: 200, headers, body, truncated, invalidUtf8 };
118+
}
119+
120+
/** GitHub's GraphQL quota headers, leaving `remaining` of 5,000 until `resetAt`. */
121+
function graphqlQuota(remaining: number, resetAt = "2099-08-13T14:00:00Z") {
122+
return {
123+
"x-ratelimit-resource": "graphql",
124+
"x-ratelimit-limit": "5000",
125+
"x-ratelimit-remaining": String(remaining),
126+
"x-ratelimit-reset": String(Date.parse(resetAt) / 1000),
127+
};
114128
}
115129

116130
/**
@@ -493,7 +507,7 @@ it.effect(
493507
Effect.provide(
494508
GitHubApi.layer.pipe(
495509
Layer.provide(Layer.mergeAll(credentials, http)),
496-
Layer.provide(GitHubGraphQlBudget.layer),
510+
Layer.provide(GitHubQuota.layer),
497511
Layer.provide(SourceControlRateLimit.layer),
498512
Layer.merge(VcsProcess.layer),
499513
Layer.provideMerge(NodeServices.layer),
@@ -538,15 +552,13 @@ it.effect(
538552
);
539553

540554
layer("GitHubPullRequestApi.layer", (it) => {
541-
it.effect("admits only one concurrent preview above the reserve and resumes after reset", () =>
555+
it.effect("holds background previews once an answer reports the reserve, until it resets", () =>
542556
Effect.gen(function* () {
543557
const now = yield* Clock.currentTimeMillis;
544-
const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
558+
const quota = yield* GitHubQuota.GitHubQuota;
545559
const resetAt = "2099-08-13T14:00:00Z";
546-
yield* budget.observe(
547-
"preview-budget.example",
548-
encodeJson({ data: { rateLimit: { cost: 1, limit: 5_000, remaining: 501, resetAt } } }),
549-
);
560+
yield* quota.observe("preview-budget.example", graphqlQuota(501, resetAt));
561+
// GitHub's count, not ours: the answer below reports the reserve has been reached.
550562
mockedExecute.mockReturnValue(
551563
Effect.succeed(
552564
output(
@@ -563,9 +575,11 @@ layer("GitHubPullRequestApi.layer", (it) => {
563575
author: null,
564576
},
565577
},
566-
rateLimit: { cost: 1, limit: 5_000, remaining: 500, resetAt },
567578
},
568579
}),
580+
false,
581+
false,
582+
graphqlQuota(499, resetAt),
569583
),
570584
),
571585
);
@@ -576,18 +590,9 @@ layer("GitHubPullRequestApi.layer", (it) => {
576590
host: "preview-budget.example",
577591
number: 7,
578592
};
579-
const results = yield* Effect.all(
580-
Array.from({ length: 20 }, (_, index) =>
581-
cli.getPullRequestPreview({ ...input, number: index + 1 }).pipe(Effect.result),
582-
),
583-
{ concurrency: "unbounded" },
584-
);
585-
expect(results.filter((result) => result._tag === "Success")).toHaveLength(1);
586-
for (const result of results) {
587-
if (result._tag === "Failure") {
588-
expect(result.failure._tag).toBe("SourceControlRateLimitPausedError");
589-
}
590-
}
593+
yield* cli.getPullRequestPreview(input);
594+
const held = yield* Effect.flip(cli.getPullRequestPreview({ ...input, number: 8 }));
595+
expect(held._tag).toBe("SourceControlRateLimitPausedError");
591596
expect(mockedExecute).toHaveBeenCalledTimes(1);
592597
yield* TestClock.setTime(Date.parse(resetAt));
593598
yield* cli.getPullRequestPreview(input);
@@ -3220,20 +3225,14 @@ layer("GitHubPullRequestApi.layer", (it) => {
32203225
}),
32213226
);
32223227

3223-
it.effect("accounts for the avatar lookup in the GraphQL budget", () =>
3228+
it.effect("looks up avatars by node id in one GraphQL read", () =>
32243229
Effect.gen(function* () {
32253230
mockedExecute.mockReturnValueOnce(
32263231
Effect.succeed(
32273232
output(
32283233
encodeJson({
32293234
data: {
32303235
nodes: [{ login: "octocat", avatarUrl: "https://avatars/octocat" }],
3231-
rateLimit: {
3232-
cost: 1,
3233-
limit: 5_000,
3234-
remaining: 4_999,
3235-
resetAt: "2099-08-13T14:00:00Z",
3236-
},
32373236
},
32383237
}),
32393238
),
@@ -3249,7 +3248,6 @@ layer("GitHubPullRequestApi.layer", (it) => {
32493248
});
32503249

32513250
expect(varsAt(0)).toEqual({ ids: ["MDQ6VXNlcjE="] });
3252-
expect(queryAt(0)).toContain("rateLimit { cost limit remaining resetAt }");
32533251
expect(avatars.get("octocat")).toBe("https://avatars/octocat");
32543252
}),
32553253
);
@@ -3852,22 +3850,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
38523850
Effect.gen(function* () {
38533851
const response = coreResponse();
38543852
mockedExecute.mockReturnValue(
3855-
Effect.succeed(
3856-
output(
3857-
encodeJson({
3858-
...response,
3859-
data: {
3860-
...response.data,
3861-
rateLimit: {
3862-
cost: 1,
3863-
limit: 5000,
3864-
remaining: 500,
3865-
resetAt: "2099-08-13T14:00:00Z",
3866-
},
3867-
},
3868-
}),
3869-
),
3870-
),
3853+
Effect.succeed(output(encodeJson(response), false, false, graphqlQuota(499))),
38713854
);
38723855
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
38733856
const input = {
@@ -4125,14 +4108,11 @@ layer("GitHubPullRequestApi.layer", (it) => {
41254108
viewerPermission: "READ",
41264109
pullRequest: { viewerCanUpdate: true, viewerDidAuthor: true },
41274110
},
4128-
rateLimit: {
4129-
cost: 1,
4130-
limit: 5_000,
4131-
remaining: 500,
4132-
resetAt: "2099-08-13T14:00:00Z",
4133-
},
41344111
},
41354112
}),
4113+
false,
4114+
false,
4115+
graphqlQuota(499),
41364116
),
41374117
),
41384118
);
@@ -4145,7 +4125,6 @@ layer("GitHubPullRequestApi.layer", (it) => {
41454125
} as const;
41464126

41474127
yield* cli.getViewerAccess(input);
4148-
expect(queryAt(0)).toContain("rateLimit { cost limit remaining resetAt }");
41494128

41504129
const error = yield* Effect.flip(cli.getViewerAccess(input));
41514130

@@ -4173,14 +4152,11 @@ layer("GitHubPullRequestApi.layer", (it) => {
41734152
viewerPermission: "READ",
41744153
pullRequest: { viewerCanUpdate: true, viewerDidAuthor: true },
41754154
},
4176-
rateLimit: {
4177-
cost: 1,
4178-
limit: 5_000,
4179-
remaining: 500,
4180-
resetAt: "2099-08-13T14:00:00Z",
4181-
},
41824155
},
41834156
}),
4157+
false,
4158+
false,
4159+
graphqlQuota(499),
41844160
),
41854161
),
41864162
)

0 commit comments

Comments
 (0)