fix: add 403 killswitch, 429 backoff, and rate limits to ErrorChannel

This commit is contained in:
NoLogicAlan committed 2026-10-21 02:49:39 +03:00
1 parent 2932f9110e
commit 6439f07204
1 file changed
+90 -5
+90 -5
View File
@@ -52,9 +52,33 @@ let _dropped = 0;
let _flushing = false; let _flushing = false;
let _announced = false; let _announced = false;
const _forwardDedup = new Map(); let _forwardDedup = new Map();
const _crashDedup = new Map(); let _crashDedup = new Map();
let _lastFailLog = 0; let _lastFailLog = 0;
let _rateLimitedUntil = 0;
let _consecutiveFailures = 0;
let _lastCrashReportTime = 0;
const CRASH_MIN_INTERVAL_MS = 5_000;
const MAX_CONSECUTIVE_FAILURES = 5;
/** @private Check if we are currently rate-limited before sending. */
function _isRateLimited() {
return Date.now() < _rateLimitedUntil;
}
/** @private Apply an explicit rate-limit backoff delay. */
function _applyRateLimit(seconds = 15) {
const ms = Math.max(10_000, Number(seconds) * 1_000);
_rateLimitedUntil = Date.now() + ms;
console.warn(`[ErrorChannel] Rate limit (429) active — backing off all error channel sends for ${Math.round(ms / 1000)}s.`);
}
/** @private Handle permanent permission failures (403/404) by permanently disabling the reporter. */
function _disablePermissionFailure(status) {
_enabled = false;
_queue = [];
console.error(`[ErrorChannel] FATAL: Bot lacks permission (HTTP ${status}) for channel ${_channelId}. ErrorChannel has been PERMANENTLY DISABLED to prevent API flooding.`);
}
const _originals = { error: null, warn: null, info: null }; const _originals = { error: null, warn: null, info: null };
@@ -123,7 +147,12 @@ function _announceOnce() {
* @returns {Promise<boolean>} True when the message was delivered. * @returns {Promise<boolean>} True when the message was delivered.
*/ */
export async function reportCrash(title, err, { fatal = false } = {}) { export async function reportCrash(title, err, { fatal = false } = {}) {
if (!_enabled || !_channelId) return false; if (!_enabled || !_channelId || _isRateLimited()) return false;
const now = Date.now();
if (now - _lastCrashReportTime < CRASH_MIN_INTERVAL_MS) {
return false;
}
_lastCrashReportTime = now;
try { try {
const body = _formatArgs([err]).split("```").join("‹›"); const body = _formatArgs([err]).split("```").join("‹›");
const now = Date.now(); const now = Date.now();
@@ -236,11 +265,20 @@ async function _runFlush() {
_flushing = true; _flushing = true;
try { try {
while (_queue.length > 0) { while (_queue.length > 0) {
if (!_enabled) {
_queue = [];
break;
}
if (_isRateLimited()) {
const waitMs = Math.max(1_000, _rateLimitedUntil - Date.now() + 500);
await _sleep(waitMs);
continue;
}
const content = _queue.shift(); const content = _queue.shift();
await _sendQuietly(content); await _sendQuietly(content);
await _sleep(_minIntervalMs); await _sleep(_minIntervalMs);
} }
if (_dropped > 0) { if (_dropped > 0 && _enabled) {
const n = _dropped; const n = _dropped;
_dropped = 0; _dropped = 0;
await _sendQuietly(`📦 ${n} additional error log message(s) were dropped during a flood to protect rate limits.`); await _sendQuietly(`📦 ${n} additional error log message(s) were dropped during a flood to protect rate limits.`);
@@ -265,35 +303,82 @@ async function _sendQuietly(content) {
/** @private Send through the client's REST client when available, else raw fetch. */ /** @private Send through the client's REST client when available, else raw fetch. */
async function _send(content) { async function _send(content) {
if (!_enabled || _isRateLimited()) return;
const rest = _client?.rest; const rest = _client?.rest;
if (typeof rest?.post === "function") { if (typeof rest?.post === "function") {
try {
await rest.post(`/channels/${_channelId}/messages`, { body: { content } }); await rest.post(`/channels/${_channelId}/messages`, { body: { content } });
_consecutiveFailures = 0;
return; return;
} catch (err) {
const status = err?.statusCode ?? err?.status;
if (status === 403 || status === 404) {
_disablePermissionFailure(status);
throw err;
}
if (status === 429) {
const retryAfter = err?.retryAfter ?? 15;
_applyRateLimit(retryAfter);
throw err;
}
throw err;
}
} }
await _sendRaw(content); await _sendRaw(content);
} }
/** @private Send via client REST, retrying once via raw fetch when that throws. */ /** @private Send via client REST, retrying once via raw fetch when that throws. */
async function _sendWithFallback(content) { async function _sendWithFallback(content) {
if (!_enabled || _isRateLimited()) return;
try { try {
await _send(content); await _send(content);
} catch (_) { } catch (_) {
if (!_enabled || _isRateLimited()) return;
await _sendRaw(content); await _sendRaw(content);
} }
} }
/** @private Send via a direct Fluxer REST call (Authorization: Bot <token>). */ /** @private Send via a direct Fluxer REST call (Authorization: Bot <token>). */
async function _sendRaw(content) { async function _sendRaw(content) {
const res = await fetch(`${API_BASE}/channels/${_channelId}/messages`, { if (!_enabled || _isRateLimited()) return;
let res;
try {
res = await fetch(`${API_BASE}/channels/${_channelId}/messages`, {
method: "POST", method: "POST",
headers: { "Authorization": `Bot ${_token}`, "Content-Type": "application/json" }, headers: { "Authorization": `Bot ${_token}`, "Content-Type": "application/json" },
body: JSON.stringify({ content }), body: JSON.stringify({ content }),
signal: AbortSignal.timeout(SEND_TIMEOUT_MS), signal: AbortSignal.timeout(SEND_TIMEOUT_MS),
}); });
} catch (err) {
_consecutiveFailures++;
if (_consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
_applyRateLimit(30);
}
throw err;
}
if (res.status === 403 || res.status === 404) {
_disablePermissionFailure(res.status);
throw new Error(`Fluxer API HTTP ${res.status}: Permission denied or channel not found`);
}
if (res.status === 429) {
const rawRetry = res.headers.get("retry-after");
const retrySec = Number(rawRetry) || 15;
_applyRateLimit(retrySec);
throw new Error(`Fluxer API HTTP 429: Rate limited, retry after ${retrySec}s`);
}
if (!res.ok) { if (!res.ok) {
_consecutiveFailures++;
if (_consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
_applyRateLimit(30);
}
const text = await res.text().catch(() => ""); const text = await res.text().catch(() => "");
throw new Error(`Fluxer API HTTP ${res.status}: ${text.slice(0, 200)}`); throw new Error(`Fluxer API HTTP ${res.status}: ${text.slice(0, 200)}`);
} }
_consecutiveFailures = 0;
} }
/** @private Human-readable process uptime. */ /** @private Human-readable process uptime. */