Skip to content

Commit a4b6afe

Browse files
voida6claude
andauthored
fix(client-runtime): threads with many images no longer flood a remote with URL requests (#27440)
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
1 parent 7816a0f commit a4b6afe

2 files changed

Lines changed: 210 additions & 5 deletions

File tree

‎packages/client-runtime/src/state/assets.test.ts‎

Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import { describe, expect, it } from "@effect/vitest";
2-
import { type AssetCreateUrlResult, EnvironmentId } from "@t3tools/contracts";
2+
import { type AssetCreateUrlResult, EnvironmentId, WS_METHODS } from "@t3tools/contracts";
33
import * as Cause from "effect/Cause";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
46
import * as Option from "effect/Option";
7+
import * as Queue from "effect/Queue";
8+
import * as Stream from "effect/Stream";
9+
import * as SubscriptionRef from "effect/SubscriptionRef";
510
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
611

7-
import { createProjectFaviconUrlAtomFamily } from "./assets.ts";
12+
import {
13+
AVAILABLE_CONNECTION_STATE,
14+
type PreparedConnection,
15+
PrimaryConnectionTarget,
16+
type SupervisorConnectionState,
17+
} from "../connection/model.ts";
18+
import * as EnvironmentRegistry from "../connection/registry.ts";
19+
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
20+
import type * as RpcSession from "../rpc/session.ts";
21+
import {
22+
ASSET_URL_MAX_IN_FLIGHT,
23+
createAssetEnvironmentAtoms,
24+
createProjectFaviconUrlAtomFamily,
25+
} from "./assets.ts";
826

927
describe("project favicon URL cache", () => {
1028
it("does not reuse another environment, workspace, or selected icon's cached URL", () => {
@@ -38,3 +56,176 @@ describe("project favicon URL cache", () => {
3856
}
3957
});
4058
});
59+
60+
/**
61+
* Environments whose `assets.createUrl` never answers until the test releases a call.
62+
* `started` receives the environment of each call as it begins, and `inFlight` counts
63+
* calls that have begun and not yet been released or interrupted.
64+
*/
65+
const makeAssetUrlHarness = Effect.fn("TestAssetUrl.makeHarness")(function* (
66+
environmentIds: ReadonlyArray<EnvironmentId>,
67+
) {
68+
const started = yield* Queue.unbounded<EnvironmentId>();
69+
const inFlight = new Map<EnvironmentId, number>();
70+
const maxInFlight = new Map<EnvironmentId, number>();
71+
const releases = new Map<EnvironmentId, Queue.Queue<void>>();
72+
const supervisors = new Map<
73+
EnvironmentId,
74+
EnvironmentSupervisor.EnvironmentSupervisor["Service"]
75+
>();
76+
const sessions = new Map<
77+
EnvironmentId,
78+
SubscriptionRef.SubscriptionRef<Option.Option<RpcSession.RpcSession>>
79+
>();
80+
const makeSession = (environmentId: EnvironmentId) =>
81+
({
82+
client: {
83+
[WS_METHODS.assetsCreateUrl]: () =>
84+
Effect.gen(function* () {
85+
const now = (inFlight.get(environmentId) ?? 0) + 1;
86+
inFlight.set(environmentId, now);
87+
maxInFlight.set(environmentId, Math.max(maxInFlight.get(environmentId) ?? 0, now));
88+
yield* Queue.offer(started, environmentId);
89+
yield* Queue.take(releases.get(environmentId)!);
90+
return { relativeUrl: "/api/assets/token/image.png", expiresAt: 4_000_000_000_000 };
91+
}).pipe(
92+
Effect.ensuring(
93+
Effect.sync(() =>
94+
inFlight.set(environmentId, (inFlight.get(environmentId) ?? 1) - 1),
95+
),
96+
),
97+
),
98+
},
99+
}) as unknown as RpcSession.RpcSession;
100+
for (const environmentId of environmentIds) {
101+
releases.set(environmentId, yield* Queue.unbounded<void>());
102+
const session = yield* SubscriptionRef.make(Option.some(makeSession(environmentId)));
103+
sessions.set(environmentId, session);
104+
supervisors.set(
105+
environmentId,
106+
EnvironmentSupervisor.EnvironmentSupervisor.of({
107+
target: new PrimaryConnectionTarget({
108+
environmentId,
109+
label: environmentId,
110+
httpBaseUrl: "https://assets.example.test",
111+
wsBaseUrl: "wss://assets.example.test",
112+
}),
113+
state: yield* SubscriptionRef.make<SupervisorConnectionState>({
114+
...AVAILABLE_CONNECTION_STATE,
115+
desired: true,
116+
network: "online",
117+
phase: "connected",
118+
attempt: 1,
119+
generation: 1,
120+
}),
121+
session,
122+
prepared: yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none()),
123+
connect: Effect.void,
124+
disconnect: Effect.void,
125+
retryNow: Effect.void,
126+
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]),
127+
);
128+
}
129+
const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (environmentId, effect) =>
130+
Effect.provideService(
131+
effect,
132+
EnvironmentSupervisor.EnvironmentSupervisor,
133+
supervisors.get(environmentId)!,
134+
);
135+
const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = (
136+
environmentId,
137+
stream,
138+
) =>
139+
Stream.provideService(
140+
stream,
141+
EnvironmentSupervisor.EnvironmentSupervisor,
142+
supervisors.get(environmentId)!,
143+
);
144+
const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
145+
run,
146+
followStream,
147+
} as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
148+
const registry = AtomRegistry.make();
149+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
150+
const atoms = createAssetEnvironmentAtoms(
151+
Atom.runtime(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry)),
152+
);
153+
return {
154+
started,
155+
inFlight: (environmentId: EnvironmentId) => inFlight.get(environmentId) ?? 0,
156+
maxInFlight: (environmentId: EnvironmentId) => maxInFlight.get(environmentId) ?? 0,
157+
release: (environmentId: EnvironmentId) => Queue.offer(releases.get(environmentId)!, undefined),
158+
replaceSession: (environmentId: EnvironmentId) =>
159+
SubscriptionRef.set(sessions.get(environmentId)!, Option.some(makeSession(environmentId))),
160+
mountImages: (environmentId: EnvironmentId, count: number) => {
161+
for (let index = 0; index < count; index += 1) {
162+
registry.mount(
163+
atoms.createUrl({
164+
environmentId,
165+
input: { resource: { _tag: "attachment", attachmentId: `image-${index}` } },
166+
}),
167+
);
168+
}
169+
},
170+
};
171+
});
172+
173+
describe("asset URL requests", () => {
174+
const remote = EnvironmentId.make("asset-remote");
175+
const other = EnvironmentId.make("asset-other");
176+
177+
it.effect("keeps a thread of images from flooding one environment with requests", () =>
178+
Effect.scoped(
179+
Effect.gen(function* () {
180+
const harness = yield* makeAssetUrlHarness([remote]);
181+
const total = ASSET_URL_MAX_IN_FLIGHT * 3;
182+
harness.mountImages(remote, total);
183+
184+
for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
185+
yield* Queue.take(harness.started);
186+
}
187+
expect(harness.inFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
188+
189+
// Each answer admits the next waiting request.
190+
for (let call = 0; call < total; call += 1) {
191+
yield* harness.release(remote);
192+
if (call < total - ASSET_URL_MAX_IN_FLIGHT) yield* Queue.take(harness.started);
193+
}
194+
expect(harness.maxInFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
195+
}),
196+
),
197+
);
198+
199+
it.effect("limits each environment on its own", () =>
200+
Effect.scoped(
201+
Effect.gen(function* () {
202+
const harness = yield* makeAssetUrlHarness([remote, other]);
203+
harness.mountImages(remote, ASSET_URL_MAX_IN_FLIGHT + 2);
204+
for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
205+
yield* Queue.take(harness.started);
206+
}
207+
harness.mountImages(other, 1);
208+
expect(yield* Queue.take(harness.started)).toBe(other);
209+
expect(harness.inFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
210+
}),
211+
),
212+
);
213+
214+
it.effect("frees the limit when the session behind the requests is replaced", () =>
215+
Effect.scoped(
216+
Effect.gen(function* () {
217+
const harness = yield* makeAssetUrlHarness([remote]);
218+
harness.mountImages(remote, ASSET_URL_MAX_IN_FLIGHT * 2);
219+
for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
220+
yield* Queue.take(harness.started);
221+
}
222+
223+
yield* harness.replaceSession(remote);
224+
for (let call = 0; call < ASSET_URL_MAX_IN_FLIGHT; call += 1) {
225+
yield* Queue.take(harness.started);
226+
}
227+
expect(harness.maxInFlight(remote)).toBe(ASSET_URL_MAX_IN_FLIGHT);
228+
}),
229+
),
230+
);
231+
});

‎packages/client-runtime/src/state/assets.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Effect from "effect/Effect";
1818
import * as Option from "effect/Option";
1919
import * as Result from "effect/Result";
2020
import * as Schema from "effect/Schema";
21+
import * as Semaphore from "effect/Semaphore";
2122
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
2223

2324
import * as EnvironmentRegistry from "../connection/registry.ts";
@@ -29,6 +30,7 @@ import { createEnvironmentQueryAtomFamily } from "./runtime.ts";
2930
const ASSET_URL_REFRESH_INTERVAL_MS = 30 * 60_000;
3031
const ASSET_URL_STALE_TIME_MS = 5 * 60_000;
3132
const ASSET_URL_IDLE_TTL_MS = 60 * 60_000;
33+
export const ASSET_URL_MAX_IN_FLIGHT = 6;
3234

3335
export class InvalidAssetCollectionKeyError extends Schema.TaggedError<InvalidAssetCollectionKeyError>()(
3436
"InvalidAssetCollectionKeyError",
@@ -136,15 +138,27 @@ export function createAssetEnvironmentAtoms<R, E>(
136138
readonly httpBaseUrl: string;
137139
} | null>,
138140
) {
141+
// A thread with hundreds of images asks for hundreds of URLs at once. Over a slow remote
142+
// they would all wait on one socket and read as stalled, so each environment gets a few
143+
// at a time. A query that is interrupted, such as by a new session, gives its slot back.
144+
const mintLimits = new Map<EnvironmentId, Semaphore.Semaphore>();
145+
const mint = (environmentId: EnvironmentId, input: AssetCreateUrlInput) => {
146+
let limit = mintLimits.get(environmentId);
147+
if (limit === undefined) {
148+
limit = Semaphore.makeUnsafe(ASSET_URL_MAX_IN_FLIGHT);
149+
mintLimits.set(environmentId, limit);
150+
}
151+
return request(WS_METHODS.assetsCreateUrl, input).pipe(limit.withPermit);
152+
};
139153
const execute = Effect.fn("assets.createUrl")(function* (input: AssetCreateUrlInput) {
140-
const result = yield* request(WS_METHODS.assetsCreateUrl, input).pipe(Effect.result);
154+
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
155+
const result = yield* mint(supervisor.target.environmentId, input).pipe(Effect.result);
141156
if (Result.isSuccess(result)) return result.success;
142157
const error = result.failure;
143158
const resource = input.resource;
144159
const local = localMediaEnvironment
145160
? (yield* AtomRegistry.AtomRegistry).get(localMediaEnvironment)
146161
: null;
147-
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
148162
if (
149163
!local ||
150164
local.environmentId === supervisor.target.environmentId ||
@@ -161,7 +175,7 @@ export function createAssetEnvironmentAtoms<R, E>(
161175
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
162176
// Map the full cause so retry defects and interruption survive.
163177
const asset = yield* registry
164-
.run(local.environmentId, request(WS_METHODS.assetsCreateUrl, input))
178+
.run(local.environmentId, mint(local.environmentId, input))
165179
.pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, () => error))));
166180
return { ...asset, relativeUrl: new URL(asset.relativeUrl, local.httpBaseUrl).href };
167181
});

0 commit comments

Comments
 (0)