From 413b133a1fb799e54c01ddd79fc898c9f35f9bb9 Mon Sep 17 00:00:00 2001 From: Bojan Kucera Date: Sun, 8 Jun 2025 09:30:12 -0400 Subject: [PATCH] getting close to final proxy... i think --- .../src/services/proxy.service.ts | 6 ++-- libs/http/src/client.ts | 35 +++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/apps/data-service/src/services/proxy.service.ts b/apps/data-service/src/services/proxy.service.ts index 2497957..c2319f1 100644 --- a/apps/data-service/src/services/proxy.service.ts +++ b/apps/data-service/src/services/proxy.service.ts @@ -7,10 +7,10 @@ export class ProxyService { private logger = new Logger('proxy-service'); private cache: CacheProvider = createCache('hybrid'); private httpClient: HttpClient; - private readonly concurrencyLimit = pLimit(1); + private readonly concurrencyLimit = pLimit(250); private readonly CACHE_KEY = 'proxy'; private readonly CACHE_TTL = 86400; // 24 hours - private readonly CHECK_TIMEOUT = 3000; + private readonly CHECK_TIMEOUT = 7000; private readonly CHECK_IP = '99.246.102.205' private readonly CHECK_URL = 'https://proxy-detection.stare.gg/?api_key=bd406bf53ddc6abe1d9de5907830a955'; private readonly PROXY_SOURCES = [ @@ -185,7 +185,7 @@ export class ProxyService { responseTime: response.responseTime, }; // console.log('Proxy check result:', proxy); - if (isWorking && JSON.stringify(response.data).includes(this.CHECK_IP)) { + if (isWorking && !JSON.stringify(response.data).includes(this.CHECK_IP)) { await this.cache.set(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`, result, this.CACHE_TTL); } // else { // TODO diff --git a/libs/http/src/client.ts b/libs/http/src/client.ts index 4b1dfdc..f0d32cd 100644 --- a/libs/http/src/client.ts +++ b/libs/http/src/client.ts @@ -78,24 +78,47 @@ export class HttpClient { */ private async executeRequest(config: RequestConfig): Promise> { const timeout = config.timeout ?? this.config.timeout ?? 30000; const controller = new AbortController(); - + const startTime = Date.now(); + let timeoutId: NodeJS.Timeout | undefined; + // Set up timeout - const timeoutId = setTimeout(() => { - controller.abort(); - }, timeout); + // Create a timeout promise that will reject + const timeoutPromise = new Promise((_, reject) => { + timeoutId = setTimeout(() => { + const elapsed = Date.now() - startTime; + this.logger?.warn('Request timeout triggered', { + url: config.url, + method: config.method, + timeout, + elapsed + }); + + // Attempt to abort (may or may not work with Bun) + controller.abort(); + + // Force rejection regardless of signal behavior + reject(new HttpError(`Request timeout after ${timeout}ms (elapsed: ${elapsed}ms)`)); + }, timeout); + }); try { // Get the appropriate adapter const adapter = AdapterFactory.getAdapter(config); - // Execute request - const response = await adapter.request(config, controller.signal); + const response = await Promise.race([ + adapter.request(config, controller.signal), + timeoutPromise + ]); + this.logger?.debug('Adapter request successful', { url: config.url, elapsedMs: Date.now() - startTime }); // Clear timeout on success clearTimeout(timeoutId); return response; } catch (error) { + const elapsed = Date.now() - startTime; + console.log(`Request Aborted after ${elapsed}ms due to timeout of ${timeout}ms`); + clearTimeout(timeoutId); // Handle timeout