Throttle COUNT diagnostics unless verbose dev mode

This commit is contained in:
thePR0M3TH3AN committed 2025-10-27 14:48:29 -04:00
1 parent 8e2f21846a
commit 7de8eddcbe
4 files changed
+84 -28

No files matched your search

+14 -17
View File
@@ -1,6 +1,6 @@
// js/nostr/client.js
import { isDevMode, isVerboseDevMode } from "../config.js";
import { isDevMode } from "../config.js";
import { FEATURE_PUBLISH_NIP71 } from "../constants.js";
import { accessControl } from "../accessControl.js";
// 🔧 merged conflicting changes from codex/update-video-publishing-and-parsing-logic vs unstable
@@ -58,6 +58,10 @@ import {
listVideoComments as listVideoCommentsForClient,
subscribeVideoComments as subscribeVideoCommentsForClient,
} from "./commentEvents.js";
import {
logCountTimeoutCleanupFailure,
logRelayCountFailure,
} from "./countDiagnostics.js";
import {
publishEventToRelay,
publishEventToRelays,
@@ -345,9 +349,7 @@ function withRequestTimeout(promise, timeoutMs, onTimeout, message = "Request ti
try {
onTimeout();
} catch (cleanupError) {
if (isVerboseDevMode) {
devLogger.warn("[nostr] COUNT timeout cleanup failed:", cleanupError);
}
logCountTimeoutCleanupFailure(cleanupError);
}
}
reject(new Error(message));
@@ -5741,20 +5743,15 @@ export class NostrClient {
});
const count = this.extractCountValue(frame?.[2]);
return { url, ok: true, frame, count };
} catch (error) {
const isUnsupported = error?.code === "count-unsupported";
if (isUnsupported) {
this.countUnsupportedRelays.add(url);
} else {
if (isVerboseDevMode) {
devLogger.warn(
`[nostr] COUNT request failed on ${url}:`,
error,
);
}
}
return { url, ok: false, error, unsupported: isUnsupported };
} catch (error) {
const isUnsupported = error?.code === "count-unsupported";
if (isUnsupported) {
this.countUnsupportedRelays.add(url);
} else {
logRelayCountFailure(url, error);
}
return { url, ok: false, error, unsupported: isUnsupported };
}
})
);
+65
View File
@@ -0,0 +1,65 @@
// js/nostr/countDiagnostics.js
// Centralized helpers for gating high-volume COUNT diagnostics behind
// verbose dev mode and deduplicating repeated warnings so the console stays
// readable during normal development sessions.
import { devLogger } from "../utils/logger.js";
import { isVerboseDevMode } from "../config.js";
const seenWarningKeys = new Set();
function isVerboseCountLoggingEnabled() {
if (typeof window !== "undefined" && window) {
const runtimeFlag = window.__BITVID_VERBOSE_DEV_MODE__;
if (typeof runtimeFlag === "boolean") {
return runtimeFlag;
}
}
return Boolean(isVerboseDevMode);
}
function logCountWarning(message, args = [], { key, throttle = true } = {}) {
if (!isVerboseCountLoggingEnabled()) {
return;
}
if (throttle && key) {
const normalizedKey = String(key);
if (seenWarningKeys.has(normalizedKey)) {
return;
}
seenWarningKeys.add(normalizedKey);
}
devLogger.warn(message, ...args);
}
export function logCountTimeoutCleanupFailure(error) {
logCountWarning("[nostr] COUNT timeout cleanup failed:", [error], {
key: "count-timeout-cleanup",
});
}
export function logRelayCountFailure(url, error) {
const normalizedUrl =
typeof url === "string" && url.trim() ? url.trim() : "(unknown relay)";
logCountWarning(`[nostr] COUNT request failed on ${normalizedUrl}:`, [error], {
key: `relay:${normalizedUrl}`,
});
}
export function logRebroadcastCountFailure(error) {
logCountWarning("[nostr] COUNT request for rebroadcast failed:", [error], {
key: "rebroadcast-count-failure",
});
}
export function logViewCountFailure(error) {
logCountWarning("[nostr] COUNT view request failed:", [error], {
key: "view-count-failure",
});
}
export function resetCountWarningHistory() {
seenWarningKeys.clear();
}
+3 -7
View File
@@ -1,7 +1,4 @@
import {
ENSURE_PRESENCE_REBROADCAST_COOLDOWN_SECONDS,
isVerboseDevMode,
} from "../config.js";
import { ENSURE_PRESENCE_REBROADCAST_COOLDOWN_SECONDS } from "../config.js";
import {
publishEventToRelays,
assertAnyRelayAccepted,
@@ -17,6 +14,7 @@ import {
} from "./toolkit.js";
import { DEFAULT_NIP07_PERMISSION_METHODS } from "./nip07Permissions.js";
import { devLogger, userLogger } from "../utils/logger.js";
import { logRebroadcastCountFailure } from "./countDiagnostics.js";
const REBROADCAST_GUARD_PREFIX = "bitvid:rebroadcast:v1";
const rebroadcastAttemptMemory = new Map();
@@ -1024,9 +1022,7 @@ export async function rebroadcastEvent({ client, eventId, options = {} }) {
})
: null;
} catch (error) {
if (isVerboseDevMode) {
devLogger.warn("[nostr] COUNT request for rebroadcast failed:", error);
}
logRebroadcastCountFailure(error);
}
if (countResult?.total && Number(countResult.total) > 0) {
+2 -4
View File
@@ -1,7 +1,6 @@
import {
VIEW_COUNT_DEDUPE_WINDOW_SECONDS,
VIEW_COUNT_BACKFILL_MAX_DAYS,
isVerboseDevMode,
} from "../config.js";
import { VIEW_FILTER_INCLUDE_LEGACY_VIDEO } from "../constants.js";
import {
@@ -13,6 +12,7 @@ import { publishEventToRelay } from "../nostrPublish.js";
import { RELAY_URLS } from "./toolkit.js";
import { normalizePointerInput } from "./watchHistory.js";
import { devLogger, userLogger } from "../utils/logger.js";
import { logViewCountFailure } from "./countDiagnostics.js";
const VIEW_EVENT_GUARD_PREFIX = "bitvid:viewed";
@@ -591,9 +591,7 @@ export async function countVideoViewEvents(client, pointer, options = {}) {
}
} catch (error) {
if (error?.code !== "count-unsupported") {
if (isVerboseDevMode) {
devLogger.warn("[nostr] COUNT view request failed:", error);
}
logViewCountFailure(error);
}
}