GeminiBot commited on
Commit ·
5ceef9b
1
Parent(s): aa5670d
Enhance resilience: add jitter and status fetch retries to handle bursts
Browse files83 unmodified lines8485868788893 unmodified lines9394959697989910010110210383 unmodified linesconsole.log(`[${reqId}] [Chat] NEW REQUEST. Parallel active: ${activeRequests}`);const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36';const headers = {"User-Agent": userAgent,3 unmodified lines};try {console.log(`[${reqId}] [Chat] Fetching status...`);const statusRes = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { headers });const hashHeader = statusRes.headers.get("x-vqd-hash-1");if (!hashHeader) throw new Error(`Status ${statusRes.status}: Missing VQD hash`);const solvedVqd = await this.solveChallenge(hashHeader, reqId);83 unmodified lines8485868788899091923 unmodified lines9697989910010110210310410510610710810911011111211311411511611711811912012112283 unmodified linesconsole.log(`[${reqId}] [Chat] NEW REQUEST. Parallel active: ${activeRequests}`);// Добавляем Jitter (случайную задержку 0-500мс), чтобы не долбить DDG пачкойawait new Promise(r => setTimeout(resolve => r(resolve), Math.random() * 500));const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36';const headers = {"User-Agent": userAgent,3 unmodified lines};try {let hashHeader = null;let statusAttempt = 0;// Ретрай для получения статуса (VQD)while (!hashHeader && statusAttempt < 3) {console.log(`[${reqId}] [Chat] Fetching status (Attempt ${statusAttempt + 1})...`);const statusRes = await fetch("https://duckduckgo.com/duckchat/v1/status?q=1", { headers });hashHeader = statusRes.headers.get("x-vqd-hash-1");if (!hashHeader) {if (statusRes.status === 429) {console.log(`[${reqId}] [Chat] Status 429 detected. Waiting...`);await new Promise(r => setTimeout(resolve => r(resolve), 1000 * (statusAttempt + 1)));} else {throw new Error(`Status ${statusRes.status}: Missing VQD hash`);}}statusAttempt++;}if (!hashHeader) throw new Error("Failed to obtain VQD hash after retries");const solvedVqd = await this.solveChallenge(hashHeader, reqId);
src/duckai.ts+23-4