Skip to content

Commit d8e23e7

Browse files
author
Vaclav Berny
committed
fix(server): reuse overflow Azure media downloads across ranges
1 parent c55bdd9 commit d8e23e7

2 files changed

Lines changed: 68 additions & 18 deletions

File tree

‎apps/server/src/assets/AzureDevOpsMedia.test.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { VcsProcessSpawnError } from "@t3tools/contracts";
77
import * as Effect from "effect/Effect";
88
import * as FileSystem from "effect/FileSystem";
99
import * as Layer from "effect/Layer";
10+
import * as TestClock from "effect/testing/TestClock";
1011
import { HttpServerResponse } from "effect/http";
1112
import * as AzureDevOpsMedia from "./AzureDevOpsMedia.ts";
1213
import { assetFileResponse } from "../http.ts";
@@ -186,22 +187,54 @@ it.effect("shares renewed capabilities and reuses range downloads under cache pr
186187
expect(yield* fs.readFileString(first.path)).toBe("GIF89a");
187188
}
188189
expect(calls).toBe(20);
190+
let overflowPath = "";
189191
yield* Effect.scoped(
190192
Effect.gen(function* () {
191193
for (let index = 0; index < 15; index++) {
192194
yield* media.get({ cwd: "/repo", url: `${base}-active-${index}.mp4` });
193195
}
194-
const transient = yield* Effect.scoped(
196+
const beforeOverflow = calls;
197+
const overflow = yield* Effect.scoped(
195198
Effect.gen(function* () {
196199
const file = yield* media.get({ cwd: "/repo", url: `${base}-overflow.mp4` });
197200
expect(yield* fs.readFileString(file.path)).toBe("GIF89a");
198201
return file;
199202
}),
200203
);
201-
expect(yield* fs.exists(transient.path)).toBe(false);
204+
overflowPath = overflow.path;
205+
expect(yield* fs.exists(overflow.path)).toBe(true);
206+
yield* Effect.scoped(
207+
Effect.gen(function* () {
208+
const repeated = yield* media.get({ cwd: "/repo", url: `${base}-overflow.mp4` });
209+
expect(repeated.path).toBe(overflow.path);
210+
const ranged = HttpServerResponse.toWeb(
211+
yield* assetFileResponse(repeated, "bytes=1-3"),
212+
);
213+
expect(ranged.status).toBe(206);
214+
expect(yield* Effect.promise(() => ranged.text())).toBe("IF8");
215+
expect(calls).toBe(beforeOverflow + 1);
216+
for (let index = 0; index < 15; index++) {
217+
yield* media.get({ cwd: "/repo", url: `${base}-overflow-active-${index}.mp4` });
218+
}
219+
const transient = yield* Effect.scoped(
220+
media.get({ cwd: "/repo", url: `${base}-uncached.mp4` }),
221+
);
222+
expect(yield* fs.exists(transient.path)).toBe(false);
223+
expect(yield* fs.readFileString(overflow.path)).toBe("GIF89a");
224+
const retained = yield* Effect.forEach(paths, (path) => fs.exists(path));
225+
expect(retained.filter(Boolean)).toHaveLength(32);
226+
}),
227+
);
202228
expect(yield* fs.readFileString(first.path)).toBe("GIF89a");
203229
}),
204230
);
231+
const beforeReuse = calls;
232+
const reused = yield* Effect.scoped(media.get({ cwd: "/repo", url: `${base}-overflow.mp4` }));
233+
expect(reused.path).toBe(overflowPath);
234+
expect(calls).toBe(beforeReuse);
235+
yield* TestClock.adjust("1 minute");
236+
expect(yield* fs.exists(overflowPath)).toBe(false);
237+
expect(yield* fs.readFileString(first.path)).toBe("GIF89a");
205238
}).pipe(Effect.scoped, Effect.provide(AzureDevOpsMedia.layer.pipe(Layer.provide(cli))));
206239
for (const path of paths) expect(yield* fs.exists(path)).toBe(false);
207240
}).pipe(Effect.provide(Layer.merge(NodeServices.layer, NodeHttpPlatform.layer))),

‎apps/server/src/assets/AzureDevOpsMedia.ts‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,27 +91,44 @@ const make = Effect.gen(function* () {
9191
idleTimeToLive: "1 minute",
9292
lookup: download,
9393
});
94+
const overflowDownloads = yield* RcMap.make({
95+
capacity: 16,
96+
idleTimeToLive: "1 minute",
97+
lookup: download,
98+
});
99+
const getCached = Effect.fn("AzureDevOpsMedia.getCached")(function* (
100+
cache: typeof downloads,
101+
key: MediaKey,
102+
) {
103+
return yield* RcMap.get(cache, key).pipe(
104+
Effect.catchTags({
105+
ExceededCapacityError: Effect.fn("AzureDevOpsMedia.overflow")(function* (error) {
106+
// Reclaim an idle slot; never displace a file held by a response stream.
107+
const idle =
108+
cache.state._tag === "Open"
109+
? Array.from(cache.state.map).find(([, entry]) => entry.refCount === 0)
110+
: undefined;
111+
if (!idle) return yield* error;
112+
yield* RcMap.invalidate(cache, idle[0]);
113+
return yield* RcMap.get(cache, key);
114+
}),
115+
}),
116+
Effect.tapError(() => RcMap.invalidate(cache, key)),
117+
);
118+
});
94119
return AzureDevOpsMedia.of({
95120
get: Effect.fn("AzureDevOpsMedia.get")(function* (input) {
96121
const key = new MediaKey({ cwd: input.cwd, url: input.url });
97-
return yield* RcMap.get(downloads, key).pipe(
122+
// Keep using overflow entries after primary slots reopen, so range requests share their bytes.
123+
const cached = (yield* RcMap.has(overflowDownloads, key))
124+
? getCached(overflowDownloads, key)
125+
: getCached(downloads, key).pipe(
126+
Effect.catchTags({ ExceededCapacityError: () => getCached(overflowDownloads, key) }),
127+
);
128+
return yield* cached.pipe(
98129
Effect.catchTags({
99-
ExceededCapacityError: Effect.fn("AzureDevOpsMedia.overflow")(function* () {
100-
// Reclaim an idle slot; never displace a file held by a response stream.
101-
const idle =
102-
downloads.state._tag === "Open"
103-
? Array.from(downloads.state.map).find(([, entry]) => entry.refCount === 0)
104-
: undefined;
105-
if (idle) {
106-
yield* RcMap.invalidate(downloads, idle[0]);
107-
return yield* RcMap.get(downloads, key).pipe(
108-
Effect.catchTags({ ExceededCapacityError: () => download(key) }),
109-
);
110-
}
111-
return yield* download(key);
112-
}),
130+
ExceededCapacityError: () => download(key),
113131
}),
114-
Effect.tapError(() => RcMap.invalidate(downloads, key)),
115132
Effect.mapError((cause) =>
116133
isAzureDevOpsMediaFetchError(cause) ? cause : new AzureDevOpsMediaFetchError({ cause }),
117134
),

0 commit comments

Comments
 (0)