Skip to content

Commit cbd72e5

Browse files
fix(task-cache): avoid cross-request admission waiters (pingdotgg#25353)
1 parent 03851b8 commit cbd72e5

4 files changed

Lines changed: 81 additions & 8 deletions

File tree

‎infra/task-cache/src/Admission.test.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { expect, it } from "@effect/vitest";
22
import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import * as Fiber from "effect/Fiber";
5+
import * as Exit from "effect/Exit";
56
import * as Admission from "./Admission.ts";
67

7-
it.effect("queues uploads and releases the permit when an upload is interrupted", () =>
8+
it.effect("rejects excess uploads immediately and releases interrupted uploads", () =>
89
Effect.gen(function* () {
910
const admission = yield* Admission.Admission;
1011
const entered = yield* Deferred.make<void>();
@@ -20,11 +21,12 @@ it.effect("queues uploads and releases the permit when an upload is interrupted"
2021
)
2122
.pipe(Effect.forkChild);
2223
yield* Deferred.await(secondEntered);
23-
const third = yield* admission
24-
.withPermit(true, Effect.succeed("restored"))
25-
.pipe(Effect.forkChild);
24+
const rejected = yield* admission
25+
.withPermit(true, Effect.die("must not run"))
26+
.pipe(Effect.exit);
27+
expect(rejected).toEqual(Exit.fail(new Admission.AdmissionRejected()));
2628
yield* Fiber.interrupt(first);
27-
expect(yield* Fiber.join(third)).toBe("restored");
29+
expect(yield* admission.withPermit(true, Effect.succeed("restored"))).toBe("restored");
2830
yield* Fiber.interrupt(second);
2931
}).pipe(Effect.provide(Admission.layer)),
3032
);

‎infra/task-cache/src/Admission.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@ import * as Context from "effect/Context";
22
import * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
44
import * as Semaphore from "effect/Semaphore";
5+
import * as Option from "effect/Option";
6+
import * as Schema from "effect/Schema";
7+
8+
export class AdmissionRejected extends Schema.TaggedError<AdmissionRejected>()(
9+
"AdmissionRejected",
10+
{},
11+
) {}
512

613
export class Admission extends Context.Service<
714
Admission,
815
{
916
readonly withPermit: <A, E, R>(
1017
store: boolean,
1118
task: Effect.Effect<A, E, R>,
12-
) => Effect.Effect<A, E, R>;
19+
) => Effect.Effect<A, E | AdmissionRejected, R>;
1320
}
1421
>()("@t3tools/task-cache/Admission") {}
1522

1623
const make = Effect.gen(function* () {
17-
// Bound simultaneous upload buffers; Effect cancels queued waiters and releases permits.
24+
// Never queue fibers across Worker requests: their async contexts cannot cross request boundaries.
1825
const stores = yield* Semaphore.make(2);
1926
const reads = yield* Semaphore.make(4);
2027
return Admission.of({
2128
// The caller owns the request span; admission adds no second span per request.
2229
withPermit: Effect.fnUntraced(function* (store, task) {
23-
return yield* (store ? stores : reads).withPermits(1)(task);
30+
const result = yield* (store ? stores : reads).withPermitsIfAvailable(1)(task);
31+
return Option.isSome(result) ? result.value : yield* new AdmissionRejected();
2432
}),
2533
});
2634
});

‎infra/task-cache/src/Cache.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Deferred from "effect/Deferred";
44
import * as Fiber from "effect/Fiber";
55
import { Deadline } from "../vendor/vite-plus-remote-cache-cloudflare/src/streams.ts";
66
import * as Layer from "effect/Layer";
7+
import * as Logger from "effect/Logger";
78
import { vi } from "vite-plus/test";
89
import { Observations } from "../vendor/vite-plus-remote-cache-cloudflare/src/observations.ts";
910
import * as Admission from "./Admission.ts";
@@ -62,6 +63,66 @@ it.effect("includes successful blob downloads in exported R2 usage", () =>
6263
}),
6364
);
6465

66+
it.effect("returns 503 immediately when authenticated requests fill all read permits", () =>
67+
Effect.gen(function* () {
68+
const entered = yield* Deferred.make<void>();
69+
const logs: unknown[] = [];
70+
const get = vi.fn(() => {
71+
if (get.mock.calls.length === 4) Deferred.doneUnsafe(entered, Effect.void);
72+
return new Promise(() => {});
73+
});
74+
const bindings = {
75+
developerKeys: JSON.stringify({
76+
julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
77+
}),
78+
ARTIFACTS: { get },
79+
readLimit: { limit: async () => ({ success: true }) },
80+
} as unknown as Cache.Bindings["Service"];
81+
yield* Effect.gen(function* () {
82+
const cache = yield* Cache.Cache;
83+
const request = () =>
84+
cache.handle(
85+
new Request(
86+
"https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
87+
{
88+
headers: {
89+
authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
90+
},
91+
},
92+
),
93+
{ waitUntil: () => {} },
94+
);
95+
const pending = yield* Effect.all(
96+
Array.from({ length: 4 }, () => request().pipe(Effect.forkChild)),
97+
);
98+
yield* Deferred.await(entered);
99+
const response = yield* request();
100+
expect(response.status).toBe(503);
101+
expect(yield* Effect.promise(() => response.text())).toBe("Service unavailable");
102+
expect(logs).toContainEqual(["Cache request failed", { reason: "concurrency_limit" }]);
103+
expect(get).toHaveBeenCalledTimes(4);
104+
yield* Effect.forEach(pending, Fiber.interrupt);
105+
}).pipe(
106+
Effect.provide([
107+
Cache.layer.pipe(
108+
Layer.provide([
109+
Layer.succeed(Cache.Bindings, bindings),
110+
Admission.layer,
111+
Layer.succeed(Savings.Savings, {
112+
record: () => Effect.void,
113+
emitTotals: () => Effect.void,
114+
cleanup: Effect.void,
115+
}),
116+
]),
117+
),
118+
Logger.layer([Logger.make(({ message }) => logs.push(message))], {
119+
mergeWithExisting: false,
120+
}),
121+
]),
122+
);
123+
}),
124+
);
125+
65126
it.effect("aborts the upstream deadline before disposing it on interruption", () =>
66127
Effect.gen(function* () {
67128
const entered = yield* Deferred.make<void>();

‎infra/task-cache/src/Cache.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ const make = Effect.gen(function* () {
220220
.pipe(
221221
Effect.timeout(writing ? "2 minutes" : "15 seconds"),
222222
Effect.catchTags({
223+
AdmissionRejected: () =>
224+
Effect.fail(new CacheError({ status: 503, reason: "concurrency_limit" })),
223225
TimeoutError: () => Effect.fail(new CacheError({ status: 503, reason: "deadline" })),
224226
}),
225227
);

0 commit comments

Comments
 (0)