GeminiBot commited on
Commit ·
5a97b06
1
Parent(s): 5ceef9b
Implement concurrency queue (max 5) to protect IP from DDG burst bans
Browse files3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383966 unmodified lines10610710882831098511011111211311411511611767 unmodified lines1851861871591881891901913 unmodified linesimport UserAgent from "user-agents";
let activeRequests = 0;const MAX_CONCURRENT_CHATS = 5; // Максимум 5 одновременных запросов к DDGconst requestQueue: (() => void)[] = [];
export class DuckAI { // Метод для ожидания своей очереди private async acquireSlot(reqId: string): Promise<void> { if (activeRequests < MAX_CONCURRENT_CHATS) { activeRequests++; return; } console.log(`[${reqId}] [Queue] Space busy (${activeRequests} active). Waiting for slot...`); return new Promise((resolve) => { requestQueue.push(() => { activeRequests++; resolve(); }); }); }
// Освобождение слота private releaseSlot() { activeRequests--; if (requestQueue.length > 0) { const next = requestQueue.shift(); if (next) next(); } }
private async solveChallenge(vqdHash: string, reqId: string): Promise<string> { const start = Date.now(); const memBefore = process.memoryUsage().heapUsed / 1024 / 1024;66 unmodified lines
async chat(request: any): Promise<string> { const reqId = Math.random().toString(36).substring(7).toUpperCase(); activeRequests++; const startTime = Date.now(); console.log(`[${reqId}] [Chat] NEW REQUEST. Parallel active: ${activeRequests}`); // Ждем свободный слот (максимум 5 параллельно) await this.acquireSlot(reqId); const startTime = Date.now(); console.log(`[${reqId}] [Chat] STARTING (Active: ${activeRequests}, Queued: ${requestQueue.length})`);
// Добавляем Jitter (случайную задержку 0-500мс), чтобы не долбить DDG пачкой await new Promise(r => setTimeout(resolve => r(resolve), Math.random() * 500));67 unmodified lines console.error(`[${reqId}] [Chat] FAILED: ${error.message}`); throw error; } finally { activeRequests--; this.releaseSlot(); } }
src/duckai.ts+33-4