Skip to content

Commit 547097d

Browse files
refactor(server): GitHub rate limits read the response headers, per quota
The GraphQL budget dated from `gh api graphql`, which handed back only a body: it rewrote every read query to select `rateLimit` and parsed the answer for it. GitHubApi talks HTTP now, and every answer carries x-ratelimit-limit/remaining/reset/resource. Replace githubGraphQlBudget with githubQuota, which keeps the same last- tenth reserve for interactive requests but learns each quota's balance from the headers, for REST (core) as well as GraphQL, and counts each admitted request against it until its own answer arrives. Queries go out as written. The reactive pause is keyed by quota too: an exhausted REST quota no longer pauses GraphQL reads, and the other way round. Providers that name no quota (Bitbucket, the PR service's own backoff) still pause the host. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent fd0d706 commit 547097d

9 files changed

Lines changed: 337 additions & 526 deletions

File tree

‎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: 44 additions & 60 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+
}),
86+
),
87+
Effect.andThen(() =>
88+
(input.query.includes("query PullRequestStackMemberships")
89+
? mockedStackMemberships
90+
: mockedExecute)({ kind: "graphql", ...input }),
9191
),
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),
@@ -541,12 +555,9 @@ layer("GitHubPullRequestApi.layer", (it) => {
541555
it.effect("admits only one concurrent preview above the reserve and resumes after reset", () =>
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));
550561
mockedExecute.mockReturnValue(
551562
Effect.succeed(
552563
output(
@@ -563,9 +574,11 @@ layer("GitHubPullRequestApi.layer", (it) => {
563574
author: null,
564575
},
565576
},
566-
rateLimit: { cost: 1, limit: 5_000, remaining: 500, resetAt },
567577
},
568578
}),
579+
false,
580+
false,
581+
graphqlQuota(500, resetAt),
569582
),
570583
),
571584
);
@@ -3157,20 +3170,14 @@ layer("GitHubPullRequestApi.layer", (it) => {
31573170
}),
31583171
);
31593172

3160-
it.effect("accounts for the avatar lookup in the GraphQL budget", () =>
3173+
it.effect("looks up avatars by node id in one GraphQL read", () =>
31613174
Effect.gen(function* () {
31623175
mockedExecute.mockReturnValueOnce(
31633176
Effect.succeed(
31643177
output(
31653178
encodeJson({
31663179
data: {
31673180
nodes: [{ login: "octocat", avatarUrl: "https://avatars/octocat" }],
3168-
rateLimit: {
3169-
cost: 1,
3170-
limit: 5_000,
3171-
remaining: 4_999,
3172-
resetAt: "2099-08-13T14:00:00Z",
3173-
},
31743181
},
31753182
}),
31763183
),
@@ -3186,7 +3193,6 @@ layer("GitHubPullRequestApi.layer", (it) => {
31863193
});
31873194

31883195
expect(varsAt(0)).toEqual({ ids: ["MDQ6VXNlcjE="] });
3189-
expect(queryAt(0)).toContain("rateLimit { cost limit remaining resetAt }");
31903196
expect(avatars.get("octocat")).toBe("https://avatars/octocat");
31913197
}),
31923198
);
@@ -3789,22 +3795,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
37893795
Effect.gen(function* () {
37903796
const response = coreResponse();
37913797
mockedExecute.mockReturnValue(
3792-
Effect.succeed(
3793-
output(
3794-
encodeJson({
3795-
...response,
3796-
data: {
3797-
...response.data,
3798-
rateLimit: {
3799-
cost: 1,
3800-
limit: 5000,
3801-
remaining: 500,
3802-
resetAt: "2099-08-13T14:00:00Z",
3803-
},
3804-
},
3805-
}),
3806-
),
3807-
),
3798+
Effect.succeed(output(encodeJson(response), false, false, graphqlQuota(499))),
38083799
);
38093800
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
38103801
const input = {
@@ -4062,14 +4053,11 @@ layer("GitHubPullRequestApi.layer", (it) => {
40624053
viewerPermission: "READ",
40634054
pullRequest: { viewerCanUpdate: true, viewerDidAuthor: true },
40644055
},
4065-
rateLimit: {
4066-
cost: 1,
4067-
limit: 5_000,
4068-
remaining: 500,
4069-
resetAt: "2099-08-13T14:00:00Z",
4070-
},
40714056
},
40724057
}),
4058+
false,
4059+
false,
4060+
graphqlQuota(499),
40734061
),
40744062
),
40754063
);
@@ -4082,7 +4070,6 @@ layer("GitHubPullRequestApi.layer", (it) => {
40824070
} as const;
40834071

40844072
yield* cli.getViewerAccess(input);
4085-
expect(queryAt(0)).toContain("rateLimit { cost limit remaining resetAt }");
40864073

40874074
const error = yield* Effect.flip(cli.getViewerAccess(input));
40884075

@@ -4110,14 +4097,11 @@ layer("GitHubPullRequestApi.layer", (it) => {
41104097
viewerPermission: "READ",
41114098
pullRequest: { viewerCanUpdate: true, viewerDidAuthor: true },
41124099
},
4113-
rateLimit: {
4114-
cost: 1,
4115-
limit: 5_000,
4116-
remaining: 500,
4117-
resetAt: "2099-08-13T14:00:00Z",
4118-
},
41194100
},
41204101
}),
4102+
false,
4103+
false,
4104+
graphqlQuota(499),
41214105
),
41224106
),
41234107
)

0 commit comments

Comments
 (0)