fix(login-perf): kill relay REQ storms and feedback loops

Real-environment console logs revealed nip-07 login pegging the CPU and
starving encrypted-list decryption via relay REQ storms (~1000 REQ/s):

- moderationService.rebuildTrustedContacts emitted "contacts"
  unconditionally, forming an emit->fetch->emit loop (~1600 kind-3 REQ/s).
  Now emits only on actual change. Regression test added.
- Per-video live report (kind 1984) and view-count (kind 30079)
  subscriptions fan out O(videos x relays) and re-fire on every flaky-relay
  reconnect. Circuit-broken behind flags until batched (see refactor plan).
- onVideosShouldRefresh now coalesces forced reloads (2s leading-edge
  throttle) so list-change signals can't drive a reload loop.
- nip-07 queue reserves one foreground slot; DM decryption demoted to LOW
  so background DM backfill can't starve feed-driving lists. Test added.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
thePR0M3TH3ANandClaude Opus 4.8 committed 2026-06-14 09:52:33 -04:00
1 parent 4bd9503caf
commit a3b205bfa1
7 files changed
+293 -5

No files matched your search

+32
View File
@@ -199,6 +199,11 @@ const UNSUPPORTED_BTITH_MESSAGE =
const VIDEO_EVENT_KIND = 30078;
const RELAY_UI_BATCH_DELAY_MS = 250;
// Minimum gap between forced full-feed reloads triggered by list-change signals
// (blocks/subscriptions/hashtag prefs). Coalesces bursts so they cannot drive a
// self-sustaining reload loop. See onVideosShouldRefresh.
const VIDEO_REFRESH_COOLDOWN_MS = 2000;
/**
* Simple IntersectionObserver-based lazy loader for images (or videos).
*
@@ -2499,6 +2504,33 @@ class Application {
}
async onVideosShouldRefresh({ reason } = {}) {
// Coalesce bursts of "should refresh" signals into at most one forced reload
// per cooldown window. List loads (blocks, subscriptions, hashtag prefs) each
// emit a change that lands here, and a forced reload re-fetches those very
// lists — under relay load they re-emit "change", which without this throttle
// produced a self-sustaining feed-reload loop that flooded relays with REQs
// (moderation/view re-subscriptions) and pegged the CPU. Leading edge runs
// immediately so the UI stays responsive; further signals within the window
// collapse into a single trailing reload.
const now = Date.now();
const cooldown = VIDEO_REFRESH_COOLDOWN_MS;
if (
this._lastShouldRefreshAt &&
now - this._lastShouldRefreshAt < cooldown
) {
this._pendingShouldRefreshReason = reason || this._pendingShouldRefreshReason;
if (!this._shouldRefreshTrailingTimer) {
const wait = Math.max(50, cooldown - (now - this._lastShouldRefreshAt));
this._shouldRefreshTrailingTimer = setTimeout(() => {
this._shouldRefreshTrailingTimer = null;
const pendingReason = this._pendingShouldRefreshReason;
this._pendingShouldRefreshReason = null;
this.onVideosShouldRefresh({ reason: pendingReason });
}, wait);
}
return;
}
this._lastShouldRefreshAt = now;
try {
await this.refreshAllVideoGrids({ reason, forceMainReload: true });
} catch (error) {