PEP-224: живое обновление без сокета, кэш по отпечатку объявленных статусов

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
peppa-bot 2026-11-20 21:10:33 +05:00
parent 971c7f67d8
commit 6d78792af6
3 changed files with 12 additions and 3 deletions

View File

@ -87,7 +87,7 @@ export function featureThresholds(): Partial<Record<FeatureName, number>> {
return out;
}
let cache: { at: number; value: Record<FeatureName, FeatureStatus> } | null = null;
let cache: { at: number; print: string; value: Record<FeatureName, FeatureStatus> } | null = null;
/** Tests and the superadmin's changes: read afresh next time. */
export function resetFeatureCache(): void {
@ -97,7 +97,10 @@ export function resetFeatureCache(): void {
/** Every feature's state (cached 15 s); trouble with observe.db = everything available. */
export function featureStatuses(o: { now?: Date; fresh?: boolean } = {}): Record<FeatureName, FeatureStatus> {
const now = o.now ?? new Date();
if (!o.fresh && !o.now && cache && Date.now() - cache.at < CACHE_MS) return cache.value;
// The announced states (written by the observer) are part of the key: a change shows at once, even in
// another copy of this module (Next bundles route handlers and pages separately).
const print = o.now ? "" : featureFingerprint();
if (!o.fresh && !o.now && cache && cache.print === print && Date.now() - cache.at < CACHE_MS) return cache.value;
let value: Record<FeatureName, FeatureStatus>;
try {
value = computeFeatures(listMonitors(), featureThresholds(), now);
@ -105,7 +108,7 @@ export function featureStatuses(o: { now?: Date; fresh?: boolean } = {}): Record
console.error("feature-status: observe.db unavailable, everything counts as available:", (e as Error).message);
value = allAvailable();
}
if (!o.now) cache = { at: Date.now(), value };
if (!o.now) cache = { at: Date.now(), print, value };
return value;
}

View File

@ -196,6 +196,9 @@ class LiveHub {
if (v !== this.seen) {
this.seen = v;
this.wake();
} else if (this.listeners.has("features") && featureFingerprint() !== this.prints.get("features")) {
// PEP-224: the observer writes observe.db only — watch it directly (one tiny query), not just pokes.
this.wake();
}
}, 1000);
this.timer.unref?.();

View File

@ -90,9 +90,12 @@ describe("Feature status", () => {
check(relayMon.id, good, new Date());
assert.equal(core.featureStatus("egov_login", { fresh: true }).state, "unavailable", "one good check is not enough (recover_threshold 2)");
// The cache follows the announced states: another module copy (a page) sees a change at once.
assert.equal(core.featureStatus("egov_login").state, "unavailable", "cached");
check(relayMon.id, good, new Date());
assert.equal(core.featureStatus("egov_login", { fresh: true }).state, "available");
await core.featureTick(db);
assert.equal(core.featureStatus("egov_login").state, "available", "announced — the cache is dropped by the fingerprint");
msgs = await sent();
assert.equal(msgs.length, 2);
assert.match(msgs[1]!, /✅ «Вход через eGov» снова включено для пользователей/);