Skip to content

Commit edb62bc

Browse files
PR Batch Testerclaude
andcommitted
fix(bitbucket): share the pull request service's pause key and trace throttled requests
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f9eb783 commit edb62bc

3 files changed

Lines changed: 35 additions & 38 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,18 +769,28 @@ it.effect("pauses background reads after a 429 until Bitbucket's retry-after pas
769769
const refused = yield* Effect.flip(lookup);
770770
assert.strictEqual(refused._tag, "BitbucketResponseError");
771771
const paused = yield* Effect.flip(lookup);
772-
assert.strictEqual(paused._tag, "SourceControlRateLimitPausedError");
772+
// Keyed by the repository host, where PullRequestService records its own backoff.
773+
assert.deepInclude(paused, {
774+
_tag: "SourceControlRateLimitPausedError",
775+
host: "bitbucket.org",
776+
retryAt: 60_000,
777+
});
773778
assert.strictEqual(execute.mock.calls.length, 1);
774779

775-
// A user waiting on an answer is not held back by the background pause.
780+
// A user waiting on an answer is not held back by the background pause, and their success
781+
// does not end it early.
776782
yield* lookup.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
777783
yield* bitbucket.request({
778784
method: "POST",
779785
url: "/repositories/acme/web/pullrequests/1/approve",
780786
});
781787
assert.strictEqual(execute.mock.calls.length, 3);
782788

783-
yield* TestClock.adjust("61 seconds");
789+
yield* TestClock.adjust("59 seconds");
790+
assert.strictEqual((yield* Effect.flip(lookup))._tag, "SourceControlRateLimitPausedError");
791+
assert.strictEqual(execute.mock.calls.length, 3);
792+
793+
yield* TestClock.adjust("1 second");
784794
yield* lookup;
785795
assert.strictEqual(execute.mock.calls.length, 4);
786796
}).pipe(Effect.provide(layer));

‎packages/source-control-bitbucket/src/server/BitbucketApi.ts‎

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,6 @@ function originOf(value: string): string | null {
607607
}
608608
}
609609

610-
function hostOf(value: string): string | null {
611-
try {
612-
return new URL(value).host;
613-
} catch {
614-
return null;
615-
}
616-
}
617-
618610
const responseError = Effect.fn("BitbucketApi.responseError")(function* (
619611
operation: BitbucketApiOperation,
620612
response: HttpClientResponse.HttpClientResponse,
@@ -652,9 +644,10 @@ export const make = Effect.gen(function* () {
652644
const fileSystem = yield* FileSystem.FileSystem;
653645
const limits = yield* SourceControlRateLimit.SourceControlRateLimit;
654646
const slots = yield* Semaphore.make(CONCURRENCY);
647+
// The repository host PullRequestService keys its own backoff under, so both share one pause.
655648
const rateLimitKey = {
656649
provider: SourceControlProviderKind.make("bitbucket"),
657-
host: hostOf(config.baseUrl) ?? config.baseUrl,
650+
host: "bitbucket.org",
658651
};
659652

660653
const apiUrl = (path: string) => `${config.baseUrl.replace(/\/+$/u, "")}${path}`;
@@ -698,32 +691,26 @@ export const make = Effect.gen(function* () {
698691
* Background reads fail at once while paused; a write or an interactive read has a user waiting
699692
* on it, so it still goes out.
700693
*/
701-
const throttled = <A, R>(
702-
method: string,
703-
effect: Effect.Effect<A, BitbucketApiError, R>,
704-
): Effect.Effect<A, BitbucketApiError, R> =>
705-
slots.withPermit(
706-
SourceControlRateLimit.Interactive.pipe(
707-
Effect.flatMap((interactive) =>
708-
limits.check(
709-
rateLimitKey,
710-
interactive || method !== "GET" ? { allowPaused: true } : undefined,
711-
),
712-
),
713-
Effect.flatMap((lease) =>
714-
effect.pipe(
715-
Effect.tap(() => limits.recordSuccess({ ...rateLimitKey, lease })),
716-
Effect.tapError((error) =>
717-
(error._tag === "BitbucketResponseError" ||
718-
error._tag === "BitbucketResponseBodyReadError") &&
719-
error.status === 429
720-
? limits.recordRateLimit({ ...rateLimitKey, lease, retryAt: error.retryAt })
721-
: Effect.void,
722-
),
723-
),
694+
const throttled = Effect.fn("BitbucketApi.throttled")(
695+
function* <A, R>(method: string, effect: Effect.Effect<A, BitbucketApiError, R>) {
696+
const interactive = yield* SourceControlRateLimit.Interactive;
697+
const lease = yield* limits.check(
698+
rateLimitKey,
699+
interactive || method !== "GET" ? { allowPaused: true } : undefined,
700+
);
701+
return yield* effect.pipe(
702+
Effect.tap(() => limits.recordSuccess({ ...rateLimitKey, lease })),
703+
Effect.tapError((error) =>
704+
(error._tag === "BitbucketResponseError" ||
705+
error._tag === "BitbucketResponseBodyReadError") &&
706+
error.status === 429
707+
? limits.recordRateLimit({ ...rateLimitKey, lease, retryAt: error.retryAt })
708+
: Effect.void,
724709
),
725-
),
726-
);
710+
);
711+
},
712+
(effect) => slots.withPermit(effect),
713+
);
727714

728715
const decodeResponse = <S extends Schema.Top>(
729716
operation: BitbucketApiOperation,

‎packages/source-control-bitbucket/src/server/BitbucketPullRequestProvider.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe("bitbucketProviderFailure", () => {
194194
it("reports a request held back by the host-wide pause as rate limited until it ends", () => {
195195
const paused = new SourceControlRateLimit.SourceControlRateLimitPausedError({
196196
provider: SourceControlProviderKind.make("bitbucket"),
197-
host: "api.bitbucket.org",
197+
host: "bitbucket.org",
198198
retryAt: 120_000,
199199
});
200200

0 commit comments

Comments
 (0)