diff --git a/apps/data-service/src/services/proxy.service.ts b/apps/data-service/src/services/proxy.service.ts index c2319f1..abb5b80 100644 --- a/apps/data-service/src/services/proxy.service.ts +++ b/apps/data-service/src/services/proxy.service.ts @@ -7,7 +7,7 @@ export class ProxyService { private logger = new Logger('proxy-service'); private cache: CacheProvider = createCache('hybrid'); private httpClient: HttpClient; - private readonly concurrencyLimit = pLimit(250); + private readonly concurrencyLimit = pLimit(200); private readonly CACHE_KEY = 'proxy'; private readonly CACHE_TTL = 86400; // 24 hours private readonly CHECK_TIMEOUT = 7000; @@ -163,12 +163,15 @@ export class ProxyService { * Check if a proxy is working */ async checkProxy(proxy: ProxyInfo): Promise { + let success = false; this.logger.debug('Checking Proxy : ', { + protocol: proxy.protocol, host: proxy.host, port: proxy.port, }); // console.log('Checking proxy:', `${proxy.protocol}://${proxy.host}:${proxy.port}`, this.concurrencyLimit.activeCount, this.concurrencyLimit.pendingCount); try { + // Test the proxy const response = await this.httpClient.get(this.CHECK_URL, { @@ -186,11 +189,11 @@ export class ProxyService { }; // console.log('Proxy check result:', proxy); if (isWorking && !JSON.stringify(response.data).includes(this.CHECK_IP)) { + success = true await this.cache.set(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`, result, this.CACHE_TTL); - } - // else { // TODO - // await this.cache.del(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`); - // } + } else { + await this.cache.del(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`); + } this.logger.debug('Proxy check completed', { host: proxy.host, @@ -212,7 +215,8 @@ export class ProxyService { // Cache failed result for shorter time // await this.cache.set(cacheKey, result, 300); // 5 minutes - // await this.cache.del(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`); + if(!success) // If the proxy check failed, remove it from cache - success is here cause i think abort signal fails sometimes + await this.cache.del(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`); this.logger.debug('Proxy check failed', { host: proxy.host, diff --git a/libs/http/src/client.ts b/libs/http/src/client.ts index f0d32cd..67db8bd 100644 --- a/libs/http/src/client.ts +++ b/libs/http/src/client.ts @@ -80,13 +80,13 @@ export class HttpClient { const controller = new AbortController(); const startTime = Date.now(); let timeoutId: NodeJS.Timeout | undefined; - + // Set up 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', { + this.logger?.debug('Request timeout triggered', { url: config.url, method: config.method, timeout,