fix(server): return fresh live pull request reads (#6472)
This commit is contained in:
1 parent
ba30177b5a
commit
229b05df04
2 files changed
+51
-51
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as TestClock from "effect/testing/TestClock";
|
||||
import type {
|
||||
OrchestrationProjectShell,
|
||||
ProjectId,
|
||||
@@ -2292,6 +2293,42 @@ it.effect("answers a repeated listing from cache, and concurrent readers share o
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("returns the refreshed listing on the first read after its cache expires", () =>
|
||||
Effect.gen(function* () {
|
||||
let hostCalls = 0;
|
||||
const service = yield* makeService({
|
||||
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
|
||||
providers: [
|
||||
fakeProvider("github", {
|
||||
listChangeRequests: () => {
|
||||
hostCalls += 1;
|
||||
return Effect.succeed({
|
||||
items: [changeRequest(hostCalls, "2026-07-02T00:00:00Z")],
|
||||
truncated: false,
|
||||
continues: false,
|
||||
});
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const first = yield* service.list({ state: "open" });
|
||||
assert.deepStrictEqual(
|
||||
first.entries.map((entry) => entry.number),
|
||||
[1],
|
||||
);
|
||||
|
||||
yield* TestClock.adjust("31 seconds");
|
||||
const refreshed = yield* service.list({ state: "open" });
|
||||
|
||||
assert.strictEqual(hostCalls, 2);
|
||||
assert.deepStrictEqual(
|
||||
refreshed.entries.map((entry) => entry.number),
|
||||
[2],
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("a listing narrowed to some projects is its own cache entry", () =>
|
||||
Effect.gen(function* () {
|
||||
const asked: ReadonlyArray<string>[] = [];
|
||||
|
||||
@@ -102,16 +102,7 @@ const DIFF_CACHE_TTL = Duration.seconds(60);
|
||||
const COMMIT_DIFF_CACHE_TTL = Duration.minutes(10);
|
||||
/** Sized like the client's own stale time; a row's counts move only when somebody pushes. */
|
||||
const LIST_STATS_CACHE_TTL = Duration.seconds(60);
|
||||
/**
|
||||
* How long a cache's last success may still be served while a fresh read runs behind it.
|
||||
* Bounded by how the page actually revalidates: clients re-read on mount and once a minute
|
||||
* while open, and every one of those reads repopulates the cache in the background — so in
|
||||
* steady use a "stale" answer is at most a refresh cycle old, and the window only stretches
|
||||
* that far when nobody has looked at the page for minutes. An explicit refresh or a mutation
|
||||
* bumps the epochs and skips held answers entirely.
|
||||
*/
|
||||
const LIST_STALE_WINDOW = Duration.minutes(10);
|
||||
const DETAIL_STALE_WINDOW = Duration.minutes(5);
|
||||
/** A diff can stay interactive while its next cached value is fetched off the critical path. */
|
||||
const DIFF_STALE_WINDOW = Duration.minutes(10);
|
||||
/** How long one host's signed-in login is believed without asking its CLI again. */
|
||||
const VIEWER_CACHE_TTL = Duration.minutes(10);
|
||||
@@ -1826,30 +1817,22 @@ export const make = Effect.gen(function* () {
|
||||
const runFork = Effect.runForkWith(context);
|
||||
|
||||
/**
|
||||
* Stale answers served while a fresh one is fetched behind them. Every read here leaves the
|
||||
* process for a CLI whose wall clock is the host's — seconds on a good day, tens of them on a
|
||||
* slow network — and the short cache windows below mean almost every page visit pays that
|
||||
* clock again. The last success per key is therefore held a while longer: a read inside the
|
||||
* window answers with it at once and refreshes the cache in the background, so the next read
|
||||
* is fresh without anyone having waited on it.
|
||||
*
|
||||
* Correctness leans on the epochs: an explicit refresh or a mutation bumps them, the epoch is
|
||||
* part of every key, and a held answer under the old key is simply never asked for again — so
|
||||
* "give me truly fresh" still means exactly that.
|
||||
* The diff is not live-polled and is expensive enough to keep its stale-while-revalidate path.
|
||||
* Explicit refreshes and mutations still strand held values through the reference epoch.
|
||||
*/
|
||||
const staleWhileRevalidate = <A>(staleFor: Duration.Duration, capacity: number) => {
|
||||
const staleMs = Duration.toMillis(staleFor);
|
||||
const held = new Map<string, { readonly at: number; readonly value: A }>();
|
||||
const record = (key: string, value: A) =>
|
||||
const staleDiff = (() => {
|
||||
const staleMs = Duration.toMillis(DIFF_STALE_WINDOW);
|
||||
const held = new Map<string, { readonly at: number; readonly value: PullRequestDiffResult }>();
|
||||
const record = (key: string, value: PullRequestDiffResult) =>
|
||||
Effect.map(Clock.currentTimeMillis, (at) => {
|
||||
held.delete(key);
|
||||
if (held.size >= capacity) {
|
||||
if (held.size >= DIFF_CACHE_CAPACITY) {
|
||||
const oldest = held.keys().next().value;
|
||||
if (oldest !== undefined) held.delete(oldest);
|
||||
}
|
||||
held.set(key, { at, value });
|
||||
});
|
||||
return <E>(key: string, read: Effect.Effect<A, E>): Effect.Effect<A, E> => {
|
||||
return <E>(key: string, read: Effect.Effect<PullRequestDiffResult, E>) => {
|
||||
const recorded = read.pipe(Effect.tap((value) => record(key, value)));
|
||||
return Effect.flatMap(Clock.currentTimeMillis, (now) => {
|
||||
const snapshot = held.get(key);
|
||||
@@ -1860,7 +1843,7 @@ export const make = Effect.gen(function* () {
|
||||
return Effect.sync(() => runFork(Effect.ignore(recorded))).pipe(Effect.as(snapshot.value));
|
||||
});
|
||||
};
|
||||
};
|
||||
})();
|
||||
|
||||
// Epochs are the invalidation mechanism: a key carries its scope's epoch, so bumping the
|
||||
// epoch strands every entry made under the old one — no enumerating a cache whose keys
|
||||
@@ -1946,10 +1929,6 @@ export const make = Effect.gen(function* () {
|
||||
timeToLive: (exit) => (Exit.isSuccess(exit) ? LIST_CACHE_TTL : Duration.zero),
|
||||
},
|
||||
);
|
||||
const staleList = staleWhileRevalidate<PullRequestListResult>(
|
||||
LIST_STALE_WINDOW,
|
||||
LIST_CACHE_CAPACITY,
|
||||
);
|
||||
const list: PullRequestService["Service"]["list"] = (input) => {
|
||||
const key = JSON.stringify([
|
||||
listingsEpoch,
|
||||
@@ -1976,7 +1955,7 @@ export const make = Effect.gen(function* () {
|
||||
? null
|
||||
: Object.entries(input.cursors).toSorted(([left], [right]) => left.localeCompare(right)),
|
||||
]);
|
||||
return staleList(key, Cache.get(listCache, key));
|
||||
return Cache.get(listCache, key);
|
||||
};
|
||||
|
||||
const detailCache = yield* Cache.makeWith(
|
||||
@@ -1989,13 +1968,9 @@ export const make = Effect.gen(function* () {
|
||||
timeToLive: (exit) => (Exit.isSuccess(exit) ? DETAIL_CACHE_TTL : Duration.zero),
|
||||
},
|
||||
);
|
||||
const staleDetail = staleWhileRevalidate<PullRequestDetail>(
|
||||
DETAIL_STALE_WINDOW,
|
||||
DETAIL_CACHE_CAPACITY,
|
||||
);
|
||||
const detail: PullRequestService["Service"]["detail"] = (input) => {
|
||||
const key = JSON.stringify([refEpoch(input), input.projectId, input.repository, input.number]);
|
||||
return staleDetail(key, Cache.get(detailCache, key));
|
||||
return Cache.get(detailCache, key);
|
||||
};
|
||||
|
||||
const activityCache = yield* Cache.makeWith(
|
||||
@@ -2008,13 +1983,9 @@ export const make = Effect.gen(function* () {
|
||||
timeToLive: (exit) => (Exit.isSuccess(exit) ? DETAIL_CACHE_TTL : Duration.zero),
|
||||
},
|
||||
);
|
||||
const staleActivity = staleWhileRevalidate<PullRequestActivity>(
|
||||
DETAIL_STALE_WINDOW,
|
||||
DETAIL_CACHE_CAPACITY,
|
||||
);
|
||||
const activity: PullRequestService["Service"]["activity"] = (input) => {
|
||||
const key = JSON.stringify([refEpoch(input), input.projectId, input.repository, input.number]);
|
||||
return staleActivity(key, Cache.get(activityCache, key));
|
||||
return Cache.get(activityCache, key);
|
||||
};
|
||||
|
||||
const diffCache = yield* Cache.makeWith(
|
||||
@@ -2044,10 +2015,6 @@ export const make = Effect.gen(function* () {
|
||||
},
|
||||
},
|
||||
);
|
||||
const staleDiff = staleWhileRevalidate<PullRequestDiffResult>(
|
||||
DIFF_STALE_WINDOW,
|
||||
DIFF_CACHE_CAPACITY,
|
||||
);
|
||||
const diff: PullRequestService["Service"]["diff"] = (input) => {
|
||||
const key = JSON.stringify([
|
||||
refEpoch(input),
|
||||
@@ -2076,10 +2043,6 @@ export const make = Effect.gen(function* () {
|
||||
// shares between clients like every other read. Refs are sorted so one page's worth of rows
|
||||
// is one key however the client assembled them, and the listings epoch rides along so the
|
||||
// refresh that forgets the listing forgets its decorations with it.
|
||||
const staleListStats = staleWhileRevalidate<PullRequestListStatsResult>(
|
||||
LIST_STALE_WINDOW,
|
||||
LIST_STATS_CACHE_CAPACITY,
|
||||
);
|
||||
const listStats: PullRequestService["Service"]["listStats"] = (input) => {
|
||||
if (input.refs.length === 0) return Effect.succeed({ stats: [] });
|
||||
const key = JSON.stringify([
|
||||
@@ -2090,7 +2053,7 @@ export const make = Effect.gen(function* () {
|
||||
`${left[0]} ${left[1]} ${left[2]}`.localeCompare(`${right[0]} ${right[1]} ${right[2]}`),
|
||||
),
|
||||
]);
|
||||
return staleListStats(key, Cache.get(listStatsCache, key));
|
||||
return Cache.get(listStatsCache, key);
|
||||
};
|
||||
|
||||
const invalidate: PullRequestService["Service"]["invalidate"] = (input) =>
|
||||
|
||||
Reference in new issue
Block a user