fix(server): bound VCS status broadcaster state

This commit is contained in:
Ben Davis committed 2026-06-26 11:18:24 -07:00
1 parent 3d0f074442
commit ed0591335e
2 files changed
+126 -28

No files matched your search

@@ -130,6 +130,40 @@ describe("VcsStatusBroadcaster", () => {
}).pipe(Effect.provide(makeTestLayer(state)));
});
it.effect("evicts the least recently used status after reaching cache capacity", () => {
const state = {
currentLocalStatus: baseLocalStatus,
currentRemoteStatus: baseRemoteStatus,
localStatusCalls: 0,
remoteStatusCalls: 0,
localInvalidationCalls: 0,
remoteInvalidationCalls: 0,
};
return Effect.gen(function* () {
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
for (let index = 0; index < VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY; index += 1) {
yield* broadcaster.getStatus({ cwd: `/repo-${index}` });
}
assert.equal(state.localStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY);
assert.equal(state.remoteStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY);
yield* broadcaster.getStatus({ cwd: "/repo-0" });
yield* broadcaster.getStatus({ cwd: "/repo-overflow" });
yield* broadcaster.getStatus({ cwd: "/repo-0" });
assert.equal(state.localStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 1);
assert.equal(state.remoteStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 1);
yield* broadcaster.getStatus({ cwd: "/repo-1" });
assert.equal(state.localStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 2);
assert.equal(state.remoteStatusCalls, VcsStatusBroadcaster.VCS_STATUS_CACHE_CAPACITY + 2);
}).pipe(Effect.provide(makeTestLayer(state)));
});
it.effect("refreshes the cached snapshot after explicit invalidation", () => {
const state = {
currentLocalStatus: baseLocalStatus,
@@ -726,6 +760,43 @@ describe("VcsStatusBroadcaster", () => {
});
});
it.effect("evicts the cached status after the last stream subscriber disconnects", () => {
const state = {
currentLocalStatus: baseLocalStatus,
currentRemoteStatus: baseRemoteStatus,
localStatusCalls: 0,
remoteStatusCalls: 0,
localInvalidationCalls: 0,
remoteInvalidationCalls: 0,
};
return Effect.gen(function* () {
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
const remoteUpdated = yield* Deferred.make<VcsStatusStreamEvent>();
const scope = yield* Scope.make();
yield* Stream.runForEach(
broadcaster.streamStatus(
{ cwd: "/repo" },
{ automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) },
),
(event) =>
event._tag === "remoteUpdated"
? Deferred.succeed(remoteUpdated, event).pipe(Effect.ignore)
: Effect.void,
).pipe(Effect.forkIn(scope));
yield* Deferred.await(remoteUpdated);
assert.equal(state.localStatusCalls, 1);
assert.equal(state.remoteStatusCalls, 1);
yield* Scope.close(scope, Exit.void);
yield* broadcaster.getStatus({ cwd: "/repo" });
assert.equal(state.localStatusCalls, 2);
assert.equal(state.remoteStatusCalls, 2);
}).pipe(Effect.provide(makeTestLayer(state)));
});
it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
const state = {
currentLocalStatus: baseLocalStatus,
+55 -28
View File
@@ -29,6 +29,7 @@ const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30);
const VCS_STATUS_REFRESH_FAILURE_MAX_DELAY = Duration.minutes(15);
const MAX_FAILURE_DIAGNOSTIC_VALUES = 8;
const MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH = 128;
export const VCS_STATUS_CACHE_CAPACITY = 128;
function boundedDiagnosticValue(value: string): string {
return value.slice(0, MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH);
@@ -188,13 +189,32 @@ export const make = Effect.gen(function* () {
const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
Scope.close(scope, Exit.void),
);
const cacheRef = yield* Ref.make(new Map<string, CachedVcsStatus>());
const statusCache = new Map<string, CachedVcsStatus>();
const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
const setCachedStatus = (cwd: string, status: CachedVcsStatus) => {
statusCache.delete(cwd);
statusCache.set(cwd, status);
while (statusCache.size > VCS_STATUS_CACHE_CAPACITY) {
const oldestCwd = statusCache.keys().next().value;
if (oldestCwd === undefined) {
break;
}
statusCache.delete(oldestCwd);
}
};
const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (
cwd: string,
) {
return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null));
return yield* Effect.sync(() => {
const cached = statusCache.get(cwd) ?? null;
if (cached) {
setCachedStatus(cwd, cached);
}
return cached;
});
});
const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")(
@@ -203,14 +223,13 @@ export const make = Effect.gen(function* () {
fingerprint: fingerprintStatusPart(local),
value: local,
} satisfies CachedValue<VcsStatusLocalResult>;
const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
const previous = cache.get(cwd) ?? { local: null, remote: null };
const nextCache = new Map(cache);
nextCache.set(cwd, {
const shouldPublish = yield* Effect.sync(() => {
const previous = statusCache.get(cwd) ?? { local: null, remote: null };
setCachedStatus(cwd, {
...previous,
local: nextLocal,
});
return [previous.local?.fingerprint !== nextLocal.fingerprint, nextCache] as const;
return previous.local?.fingerprint !== nextLocal.fingerprint;
});
if (options?.publish && shouldPublish) {
@@ -233,14 +252,13 @@ export const make = Effect.gen(function* () {
fingerprint: fingerprintStatusPart(remote),
value: remote,
} satisfies CachedValue<VcsStatusRemoteResult | null>;
const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
const previous = cache.get(cwd) ?? { local: null, remote: null };
const nextCache = new Map(cache);
nextCache.set(cwd, {
const shouldPublish = yield* Effect.sync(() => {
const previous = statusCache.get(cwd) ?? { local: null, remote: null };
setCachedStatus(cwd, {
...previous,
remote: nextRemote,
});
return [previous.remote?.fingerprint !== nextRemote.fingerprint, nextCache] as const;
return previous.remote?.fingerprint !== nextRemote.fingerprint;
});
if (options?.publish && shouldPublish) {
@@ -271,18 +289,16 @@ export const make = Effect.gen(function* () {
fingerprint: fingerprintStatusPart(remote),
value: remote,
} satisfies CachedValue<VcsStatusRemoteResult | null>;
const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
const previous = cache.get(cwd) ?? { local: null, remote: null };
const nextCache = new Map(cache);
nextCache.set(cwd, {
const shouldPublish = yield* Effect.sync(() => {
const previous = statusCache.get(cwd) ?? { local: null, remote: null };
setCachedStatus(cwd, {
local: nextLocal,
remote: nextRemote,
});
return [
return (
previous.local?.fingerprint !== nextLocal.fingerprint ||
previous.remote?.fingerprint !== nextRemote.fingerprint,
nextCache,
] as const;
previous.remote?.fingerprint !== nextRemote.fingerprint
);
});
if (options?.publish && shouldPublish) {
@@ -506,6 +522,16 @@ export const make = Effect.gen(function* () {
if (pollerToInterrupt) {
yield* Fiber.interrupt(pollerToInterrupt).pipe(Effect.ignore);
yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => {
if (activePollers.has(cwd)) {
return Effect.succeed([undefined, activePollers] as const);
}
return Effect.sync(() => {
statusCache.delete(cwd);
return [undefined, activePollers] as const;
});
});
}
});
@@ -517,15 +543,16 @@ export const make = Effect.gen(function* () {
const initialLocal = yield* getOrLoadLocalStatus(cwd);
const cachedStatus = yield* getCachedStatus(cwd);
const initialRemote = cachedStatus?.remote?.value ?? null;
yield* retainRemotePoller(
cwd,
options?.automaticRemoteRefreshInterval ??
Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL),
cachedStatus?.remote === null || cachedStatus?.remote === undefined,
yield* Effect.acquireRelease(
retainRemotePoller(
cwd,
options?.automaticRemoteRefreshInterval ??
Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL),
cachedStatus?.remote === null || cachedStatus?.remote === undefined,
),
() => releaseRemotePoller(cwd),
);
const release = releaseRemotePoller(cwd).pipe(Effect.ignore, Effect.asVoid);
return Stream.concat(
Stream.make({
_tag: "snapshot" as const,
@@ -536,7 +563,7 @@ export const make = Effect.gen(function* () {
Stream.filter((event) => event.cwd === cwd),
Stream.map((event) => event.event),
),
).pipe(Stream.ensuring(release));
);
}),
);