Skip to content

Commit db693e5

Browse files
committed
refactor(github): reuse rate-limit error
1 parent cfa4245 commit db693e5

6 files changed

Lines changed: 44 additions & 58 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,10 +2395,10 @@ layer("GitHubPullRequestCli.layer", (it) => {
23952395

23962396
const error = yield* Effect.flip(cli.getPullRequestBaseComparison(input));
23972397

2398-
assert.strictEqual(error._tag, "GitHubGraphQlBudgetPausedError");
2399-
if (error._tag !== "GitHubGraphQlBudgetPausedError") return;
2398+
assert.strictEqual(error._tag, "SourceControlRateLimitPausedError");
2399+
if (error._tag !== "SourceControlRateLimitPausedError") return;
24002400
assert.strictEqual(error.host, "github.com");
2401-
assert.strictEqual(error.resetAt, "2099-08-13T14:00:00Z");
2401+
assert.strictEqual(error.retryAt, Date.parse("2099-08-13T14:00:00Z"));
24022402
assert.strictEqual(mockedExecute.mock.calls.length, 1);
24032403
yield* TestClock.setTime(Date.parse("2100-01-01T00:00:00Z"));
24042404
}),

‎apps/server/src/pullRequest/GitHubPullRequestCli.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424

2525
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
2626
import * as GitHubGraphQlBudget from "../sourceControl/githubGraphQlBudget.ts";
27+
import * as SourceControlRateLimit from "../sourceControl/SourceControlRateLimit.ts";
2728
import {
2829
ACTOR_AVATARS_GRAPHQL_QUERY,
2930
ADD_REACTION_GRAPHQL_MUTATION,
@@ -248,7 +249,7 @@ export type GitHubPullRequestCliError =
248249
| GitHubDiffFileContentsUnavailableError
249250
| GitHubRepositorySelectorError
250251
| GitHubSubjectScopeError
251-
| GitHubGraphQlBudget.GitHubGraphQlBudgetPausedError
252+
| SourceControlRateLimit.SourceControlRateLimitPausedError
252253
| GitHubViewerLoginUnavailableError;
253254

254255
/** A large pull request can produce a multi-megabyte patch; past this it is truncated. */

‎apps/server/src/pullRequest/GitHubPullRequestProvider.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import * as Effect from "effect/Effect";
2-
import * as DateTime from "effect/DateTime";
3-
import * as Option from "effect/Option";
42
import type {
53
PullRequestActor,
64
PullRequestCapabilities,
@@ -91,12 +89,8 @@ export function gitHubProviderFailure(
9189
if (error._tag === "GitHubCliUnavailableError") return { reason: "missing-tool" };
9290
if (error._tag === "GitHubCliAuthenticationError") return { reason: "unauthenticated" };
9391
if (error._tag === "GitHubCliRateLimitError") return { reason: "rate-limited" };
94-
if (error._tag === "GitHubGraphQlBudgetPausedError") {
95-
const retryAt = Option.map(DateTime.make(error.resetAt), DateTime.toEpochMillis);
96-
return {
97-
reason: "rate-limited",
98-
...(Option.isSome(retryAt) ? { retryAt: retryAt.value } : {}),
99-
};
92+
if (error._tag === "SourceControlRateLimitPausedError") {
93+
return { reason: "rate-limited", retryAt: error.retryAt };
10094
}
10195
return { reason: "failed" };
10296
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert, it } from "@effect/vitest";
33
import * as AzureDevOpsCli from "../sourceControl/AzureDevOpsCli.ts";
44
import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
55
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
6-
import * as GitHubGraphQlBudget from "../sourceControl/githubGraphQlBudget.ts";
6+
import * as SourceControlRateLimit from "../sourceControl/SourceControlRateLimit.ts";
77
import * as GitLabCli from "../sourceControl/GitLabCli.ts";
88
import { azureDevOpsProviderFailure } from "./AzureDevOpsPullRequestProvider.ts";
99
import { bitbucketProviderFailure } from "./BitbucketPullRequestProvider.ts";
@@ -55,12 +55,13 @@ it("classifies rate limits from every pull-request provider", () => {
5555
);
5656
});
5757

58-
it("keeps GitHub GraphQL's exact reset time", () => {
58+
it("keeps GitHub's exact retry time", () => {
5959
assert.deepStrictEqual(
6060
gitHubProviderFailure(
61-
new GitHubGraphQlBudget.GitHubGraphQlBudgetPausedError({
61+
new SourceControlRateLimit.SourceControlRateLimitPausedError({
62+
provider: "github",
6263
host: "github.com",
63-
resetAt: "2026-08-15T14:00:00.000Z",
64+
retryAt: 1_786_802_400_000,
6465
}),
6566
),
6667
{ reason: "rate-limited", retryAt: 1_786_802_400_000 },

‎apps/server/src/sourceControl/githubGraphQlBudget.test.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@ describe("GitHub GraphQL budget", () => {
3333
}).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
3434
);
3535

36-
it.effect("protects the last ten percent until reset", () =>
36+
it.effect("protects the last ten percent with the shared provider cooldown error", () =>
3737
Effect.gen(function* () {
3838
yield* TestClock.setTime(BEFORE_RESET);
3939
const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
4040
yield* budget.observe("github.com", rateLimit(500));
4141

4242
const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
4343
expect(error).toMatchObject({
44-
_tag: "GitHubGraphQlBudgetPausedError",
44+
_tag: "SourceControlRateLimitPausedError",
45+
provider: "github",
4546
host: "github.com",
46-
resetAt: RESET_AT,
47+
retryAt: Date.parse(RESET_AT),
4748
});
4849
expect(error.message).toBe(
49-
`GitHub GraphQL reads for github.com are paused until ${RESET_AT}.`,
50+
"github requests to github.com are paused until the rate limit resets.",
5051
);
5152

5253
yield* TestClock.setTime(AFTER_RESET);
@@ -63,7 +64,7 @@ describe("GitHub GraphQL budget", () => {
6364
yield* budget.observe("github.com", rateLimit(0));
6465

6566
const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
66-
expect(error._tag).toBe("GitHubGraphQlBudgetPausedError");
67+
expect(error._tag).toBe("SourceControlRateLimitPausedError");
6768
expect(yield* budget.query("github.example.com", "query { viewer { login } }")).toContain(
6869
"rateLimit",
6970
);
@@ -79,8 +80,8 @@ describe("GitHub GraphQL budget", () => {
7980

8081
const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
8182
expect(error).toMatchObject({
82-
_tag: "GitHubGraphQlBudgetPausedError",
83-
resetAt: RESET_AT,
83+
_tag: "SourceControlRateLimitPausedError",
84+
retryAt: Date.parse(RESET_AT),
8485
});
8586
}).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
8687
);
@@ -94,8 +95,8 @@ describe("GitHub GraphQL budget", () => {
9495

9596
const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
9697
expect(error).toMatchObject({
97-
_tag: "GitHubGraphQlBudgetPausedError",
98-
resetAt: NEXT_RESET_AT,
98+
_tag: "SourceControlRateLimitPausedError",
99+
retryAt: Date.parse(NEXT_RESET_AT),
99100
});
100101
}).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
101102
);
@@ -109,8 +110,8 @@ describe("GitHub GraphQL budget", () => {
109110

110111
const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
111112
expect(error).toMatchObject({
112-
_tag: "GitHubGraphQlBudgetPausedError",
113-
resetAt: NEXT_RESET_AT,
113+
_tag: "SourceControlRateLimitPausedError",
114+
retryAt: Date.parse(NEXT_RESET_AT),
114115
});
115116
}).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
116117
);
@@ -153,8 +154,8 @@ describe("GitHub GraphQL budget", () => {
153154
const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
154155

155156
expect(error).toMatchObject({
156-
_tag: "GitHubGraphQlBudgetPausedError",
157-
resetAt: RESET_AT,
157+
_tag: "SourceControlRateLimitPausedError",
158+
retryAt: Date.parse(RESET_AT),
158159
});
159160
}).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
160161
);

‎apps/server/src/sourceControl/githubGraphQlBudget.ts‎

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import * as Context from "effect/Context";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
55
import * as Ref from "effect/Ref";
6-
import * as Schema from "effect/Schema";
6+
import * as Predicate from "effect/Predicate";
7+
8+
import * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
79

810
const GRAPHQL_RESERVE_RATIO = 0.1;
911
const RATE_LIMIT_SELECTION = "rateLimit { cost limit remaining resetAt }";
@@ -12,34 +14,17 @@ interface GraphQlBudgetSnapshot {
1214
readonly cost: number;
1315
readonly limit: number;
1416
readonly remaining: number;
15-
readonly resetAt: string;
1617
readonly resetAtMs: number;
1718
}
1819

19-
export class GitHubGraphQlBudgetPausedError extends Schema.TaggedErrorClass<GitHubGraphQlBudgetPausedError>()(
20-
"GitHubGraphQlBudgetPausedError",
21-
{
22-
host: Schema.String,
23-
resetAt: Schema.String,
24-
},
25-
) {
26-
get detail(): string {
27-
return `GraphQL reads for ${this.host} are paused until ${this.resetAt}.`;
28-
}
29-
30-
override get message(): string {
31-
return `GitHub ${this.detail}`;
32-
}
33-
}
34-
3520
export class GitHubGraphQlBudget extends Context.Service<
3621
GitHubGraphQlBudget,
3722
{
3823
readonly query: (
3924
host: string,
4025
document: string,
4126
options?: { readonly allowReserve: boolean },
42-
) => Effect.Effect<string, GitHubGraphQlBudgetPausedError>;
27+
) => Effect.Effect<string, SourceControlRateLimit.SourceControlRateLimitPausedError>;
4328
readonly observe: (host: string, raw: string) => Effect.Effect<void>;
4429
}
4530
>()("t3/sourceControl/githubGraphQlBudget") {}
@@ -48,14 +33,14 @@ function hostKey(host: string): string {
4833
return host.trim().toLowerCase();
4934
}
5035

51-
function isRecord(value: unknown): value is Record<string, unknown> {
52-
return typeof value === "object" && value !== null;
53-
}
54-
5536
function snapshotFrom(raw: string): GraphQlBudgetSnapshot | null {
5637
try {
5738
const parsed: unknown = JSON.parse(raw);
58-
if (!isRecord(parsed) || !isRecord(parsed.data) || !isRecord(parsed.data.rateLimit)) {
39+
if (
40+
!Predicate.isObject(parsed) ||
41+
!Predicate.isObject(parsed.data) ||
42+
!Predicate.isObject(parsed.data.rateLimit)
43+
) {
5944
return null;
6045
}
6146
const { cost, limit, remaining, resetAt } = parsed.data.rateLimit;
@@ -74,7 +59,7 @@ function snapshotFrom(raw: string): GraphQlBudgetSnapshot | null {
7459
return null;
7560
}
7661
const resetAtMs = Date.parse(resetAt);
77-
return Number.isFinite(resetAtMs) ? { cost, limit, remaining, resetAt, resetAtMs } : null;
62+
return Number.isFinite(resetAtMs) ? { cost, limit, remaining, resetAtMs } : null;
7863
} catch {
7964
return null;
8065
}
@@ -99,7 +84,7 @@ export const make = Effect.gen(function* () {
9984
function* (host, document, options) {
10085
if (!isReadOperation(document)) return document;
10186
const now = yield* Clock.currentTimeMillis;
102-
const resetAt = yield* Ref.modify(snapshots, (current) => {
87+
const retryAt = yield* Ref.modify(snapshots, (current) => {
10388
const key = hostKey(host);
10489
const snapshot = current.get(key);
10590
if (snapshot === undefined) return [null, current] as const;
@@ -110,14 +95,18 @@ export const make = Effect.gen(function* () {
11095
}
11196
const remaining = Math.max(0, snapshot.remaining - Math.max(1, snapshot.cost));
11297
if (options?.allowReserve !== true && remaining < snapshot.limit * GRAPHQL_RESERVE_RATIO) {
113-
return [snapshot.resetAt, current] as const;
98+
return [snapshot.resetAtMs, current] as const;
11499
}
115100
const next = new Map(current);
116101
next.set(key, { ...snapshot, remaining });
117102
return [null, next] as const;
118103
});
119-
if (resetAt !== null) {
120-
return yield* new GitHubGraphQlBudgetPausedError({ host, resetAt });
104+
if (retryAt !== null) {
105+
return yield* new SourceControlRateLimit.SourceControlRateLimitPausedError({
106+
provider: "github",
107+
host: hostKey(host),
108+
retryAt,
109+
});
121110
}
122111
return withRateLimit(document);
123112
},

0 commit comments

Comments
 (0)