getting close to final proxy... i think
This commit is contained in:
parent
2f070d73f9
commit
413b133a1f
2 changed files with 32 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -78,24 +78,47 @@ export class HttpClient {
|
|||
*/ private async executeRequest<T>(config: RequestConfig): Promise<HttpResponse<T>> {
|
||||
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<never>((_, 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<T>(config, controller.signal);
|
||||
const response = await Promise.race([
|
||||
adapter.request<T>(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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue