Skip to content

Commit ce47834

Browse files
committed
fix(server): expire cached pull request diffs from read start
1 parent 99822f8 commit ce47834

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4179,7 +4179,7 @@ it.effect.each(["commit", "credential"] as const)(
41794179
}),
41804180
);
41814181

4182-
it.effect.each(["expiry", "refresh"] as const)(
4182+
it.effect.each(["expiry", "commit expiry", "cache hit", "commit cache hit", "refresh"] as const)(
41834183
"keeps a loaded diff's original membership scope through an in-flight %s",
41844184
(boundary) =>
41854185
Effect.gen(function* () {
@@ -4203,17 +4203,40 @@ it.effect.each(["expiry", "refresh"] as const)(
42034203
nextCursor: null,
42044204
};
42054205
}),
4206-
getDiffFileContents: () => Effect.die("an expired file must not be read"),
4206+
getDiffFileContents: () => Effect.succeed({ oldContents: "old", newContents: "new" }),
42074207
}),
42084208
],
42094209
});
4210-
const ref = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
4210+
const ref = {
4211+
projectId: "p1" as ProjectId,
4212+
repository: "acme/web",
4213+
number: 1,
4214+
...(boundary.includes("commit") ? { commit: "a".repeat(40) } : {}),
4215+
};
42114216
const loading = yield* Effect.forkChild(service.diff(ref));
42124217
yield* Deferred.await(started);
42134218
if (boundary === "expiry") yield* TestClock.adjust("61 seconds");
4219+
else if (boundary === "commit expiry") yield* TestClock.adjust("11 minutes");
4220+
else if (boundary.includes("cache hit")) yield* TestClock.adjust("30 seconds");
42144221
else yield* service.invalidate({ reference: ref });
42154222
yield* Deferred.succeed(release, undefined);
42164223
yield* Fiber.join(loading);
4224+
if (boundary.includes("cache hit")) {
4225+
yield* service.diff(ref);
4226+
yield* service.diff(ref);
4227+
yield* TestClock.adjust(boundary.includes("commit") ? "9 minutes" : "29 seconds");
4228+
assert.deepStrictEqual(
4229+
yield* service.diffFileContents({
4230+
...ref,
4231+
changeType: "change",
4232+
oldPath: "private.ts",
4233+
newPath: "private.ts",
4234+
}),
4235+
{ oldContents: "old", newContents: "new" },
4236+
);
4237+
assert.strictEqual(diffReads, 1);
4238+
yield* TestClock.adjust(boundary.includes("commit") ? "1 minute" : "2 seconds");
4239+
}
42174240
const error = yield* service
42184241
.diffFileContents({
42194242
...ref,

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ export const make = Effect.gen(function* () {
691691
const rateLimits = yield* SourceControlRateLimit.SourceControlRateLimit;
692692
const filesViewedStore = yield* PullRequestFilesViewed.PullRequestFilesViewedRepository;
693693
const readCache = yield* PullRequestReadCache.PullRequestReadCache;
694+
const clock = yield* Clock.Clock;
694695

695696
/**
696697
* Where each project's remote is served from, by the remote's recognised base URL: a remote no
@@ -3237,10 +3238,10 @@ export const make = Effect.gen(function* () {
32373238
...(commit === null ? {} : { commit }),
32383239
} as PullRequestDiffInput).pipe(
32393240
observeRead,
3240-
Effect.map(({ value, observedAt }) => {
3241+
Effect.tap(({ value, observedAt }) => {
32413242
// Keep the loader's epoch and read time if a refresh occurs during the provider read.
32423243
diffFiles.record(JSON.stringify([reference, cursor, commit]), value, observedAt);
3243-
return value;
3244+
return Effect.void;
32443245
}),
32453246
);
32463247
},
@@ -3249,7 +3250,10 @@ export const make = Effect.gen(function* () {
32493250
timeToLive: (exit, key) => {
32503251
if (!Exit.isSuccess(exit)) return Duration.zero;
32513252
const commit = (JSON.parse(key) as ReadonlyArray<unknown>)[2];
3252-
return commit === null ? DIFF_CACHE_TTL : COMMIT_DIFF_CACHE_TTL;
3253+
const ttl = Duration.toMillis(commit === null ? DIFF_CACHE_TTL : COMMIT_DIFF_CACHE_TTL);
3254+
return Duration.millis(
3255+
Math.max(0, exit.value.observedAt + ttl - clock.currentTimeMillisUnsafe()),
3256+
);
32533257
},
32543258
},
32553259
);
@@ -3275,7 +3279,7 @@ export const make = Effect.gen(function* () {
32753279
if (held > 0) armDiffSweep();
32763280
}),
32773281
);
3278-
const diff: PullRequestService["Service"]["diff"] = (input) => {
3282+
const readDiff = (input: PullRequestDiffInput) => {
32793283
const key = JSON.stringify([
32803284
refCacheKey(input),
32813285
input.cursor ?? null,
@@ -3288,16 +3292,21 @@ export const make = Effect.gen(function* () {
32883292
Effect.tap(() =>
32893293
Cache.entries(diffCache).pipe(
32903294
Effect.flatMap((entries) =>
3291-
Effect.forEach(diffCacheEvictions(entries), (evicted) =>
3292-
Cache.invalidate(diffCache, evicted),
3295+
Effect.forEach(
3296+
diffCacheEvictions(Array.from(entries, ([key, read]) => [key, read.value] as const)),
3297+
(evicted) => Cache.invalidate(diffCache, evicted),
32933298
),
32943299
),
32953300
Effect.uninterruptible,
32963301
),
32973302
),
32983303
Effect.ensuring(Effect.sync(armDiffSweep)),
32993304
);
3300-
return staleDiff.serve(key, read);
3305+
return { key, read };
3306+
};
3307+
const diff: PullRequestService["Service"]["diff"] = (input) => {
3308+
const { key, read } = readDiff(input);
3309+
return staleDiff.serve(key, read.pipe(Effect.map((read) => read.value)));
33013310
};
33023311

33033312
const filesViewedCache = yield* Cache.makeWith(
@@ -3529,8 +3538,8 @@ export const make = Effect.gen(function* () {
35293538
const now = yield* Clock.currentTimeMillis;
35303539
const cached = held.get(key);
35313540
if (cached !== undefined && now - cached.at <= ttlMs) return cached;
3532-
const slice = yield* diff(input);
3533-
return record(key, slice, now);
3541+
const { value, observedAt } = yield* readDiff(input).read;
3542+
return record(key, value, observedAt);
35343543
});
35353544
return { read: credentialCached(read), record, fileKey };
35363545
})();

0 commit comments

Comments
 (0)