This commit is contained in:
Bojan Kucera 2025-06-08 10:03:03 -04:00
parent 413b133a1f
commit 5d8b102422
2 changed files with 12 additions and 8 deletions

View file

@ -7,7 +7,7 @@ export class ProxyService {
private logger = new Logger('proxy-service'); private logger = new Logger('proxy-service');
private cache: CacheProvider = createCache('hybrid'); private cache: CacheProvider = createCache('hybrid');
private httpClient: HttpClient; private httpClient: HttpClient;
private readonly concurrencyLimit = pLimit(250); private readonly concurrencyLimit = pLimit(200);
private readonly CACHE_KEY = 'proxy'; private readonly CACHE_KEY = 'proxy';
private readonly CACHE_TTL = 86400; // 24 hours private readonly CACHE_TTL = 86400; // 24 hours
private readonly CHECK_TIMEOUT = 7000; private readonly CHECK_TIMEOUT = 7000;
@ -163,12 +163,15 @@ export class ProxyService {
* Check if a proxy is working * Check if a proxy is working
*/ */
async checkProxy(proxy: ProxyInfo): Promise<ProxyInfo> { async checkProxy(proxy: ProxyInfo): Promise<ProxyInfo> {
let success = false;
this.logger.debug('Checking Proxy : ', { this.logger.debug('Checking Proxy : ', {
protocol: proxy.protocol,
host: proxy.host, host: proxy.host,
port: proxy.port, port: proxy.port,
}); });
// console.log('Checking proxy:', `${proxy.protocol}://${proxy.host}:${proxy.port}`, this.concurrencyLimit.activeCount, this.concurrencyLimit.pendingCount); // console.log('Checking proxy:', `${proxy.protocol}://${proxy.host}:${proxy.port}`, this.concurrencyLimit.activeCount, this.concurrencyLimit.pendingCount);
try { try {
// Test the proxy // Test the proxy
const response = await this.httpClient.get(this.CHECK_URL, { const response = await this.httpClient.get(this.CHECK_URL, {
@ -186,11 +189,11 @@ export class ProxyService {
}; };
// console.log('Proxy check result:', proxy); // 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)) {
success = true
await this.cache.set(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`, result, this.CACHE_TTL); await this.cache.set(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`, result, this.CACHE_TTL);
} } else {
// else { // TODO await this.cache.del(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`);
// await this.cache.del(`${this.CACHE_KEY}:${proxy.protocol}://${proxy.host}:${proxy.port}`); }
// }
this.logger.debug('Proxy check completed', { this.logger.debug('Proxy check completed', {
host: proxy.host, host: proxy.host,
@ -212,7 +215,8 @@ export class ProxyService {
// Cache failed result for shorter time // Cache failed result for shorter time
// await this.cache.set(cacheKey, result, 300); // 5 minutes // 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', { this.logger.debug('Proxy check failed', {
host: proxy.host, host: proxy.host,

View file

@ -80,13 +80,13 @@ export class HttpClient {
const controller = new AbortController(); const controller = new AbortController();
const startTime = Date.now(); const startTime = Date.now();
let timeoutId: NodeJS.Timeout | undefined; let timeoutId: NodeJS.Timeout | undefined;
// Set up timeout // Set up timeout
// Create a timeout promise that will reject // Create a timeout promise that will reject
const timeoutPromise = new Promise<never>((_, reject) => { const timeoutPromise = new Promise<never>((_, reject) => {
timeoutId = setTimeout(() => { timeoutId = setTimeout(() => {
const elapsed = Date.now() - startTime; const elapsed = Date.now() - startTime;
this.logger?.warn('Request timeout triggered', { this.logger?.debug('Request timeout triggered', {
url: config.url, url: config.url,
method: config.method, method: config.method,
timeout, timeout,