fix(fetcher): re-use proxy agents
This commit is contained in:
parent
aded562304
commit
ae32376758
2 changed files with 13 additions and 6 deletions
|
|
@ -8,7 +8,7 @@ import winston from 'winston';
|
|||
import { BlockedError, HttpError, NotFoundError, QueueIsFullError, TimeoutError, TooManyRequestsError, TooManyTimeoutsError } from '../error';
|
||||
import { BlockedReason, Context } from '../types';
|
||||
import { noCache } from './config';
|
||||
import { createProxyAgent, getProxyForUrl } from './dispatcher';
|
||||
import { getProxyAgent, getProxyForUrl } from './dispatcher';
|
||||
import { envGet } from './env';
|
||||
|
||||
interface HttpCacheItem {
|
||||
|
|
@ -288,7 +288,7 @@ export class Fetcher {
|
|||
...init,
|
||||
keepalive: true,
|
||||
signal: AbortSignal.timeout(init?.timeout ?? this.DEFAULT_TIMEOUT),
|
||||
...(/* istanbul ignore next */ proxyUrl && { dispatcher: createProxyAgent(proxyUrl) }),
|
||||
...(/* istanbul ignore next */ proxyUrl && { dispatcher: getProxyAgent(proxyUrl) }),
|
||||
};
|
||||
|
||||
response = await fetch(finalUrl, finalInit);
|
||||
|
|
|
|||
|
|
@ -24,10 +24,17 @@ export const getProxyForUrl = (ctx: Context, url: URL): URL | undefined => {
|
|||
return undefined;
|
||||
};
|
||||
|
||||
export const createProxyAgent = (proxyUrl: URL): Dispatcher => {
|
||||
if (proxyUrl.protocol === 'socks5:') {
|
||||
return socksDispatcher({ type: 5, host: proxyUrl.hostname, port: parseInt(proxyUrl.port) });
|
||||
const proxyAgents = new Map<string, Dispatcher>();
|
||||
export const getProxyAgent = (proxyUrl: URL): Dispatcher => {
|
||||
let proxyAgent = proxyAgents.get(proxyUrl.href);
|
||||
|
||||
if (!proxyAgent) {
|
||||
proxyAgent = proxyUrl.protocol === 'socks5:'
|
||||
? socksDispatcher({ type: 5, host: proxyUrl.hostname, port: parseInt(proxyUrl.port) })
|
||||
: new ProxyAgent({ uri: proxyUrl.href });
|
||||
|
||||
proxyAgents.set(proxyUrl.href, proxyAgent);
|
||||
}
|
||||
|
||||
return new ProxyAgent({ uri: proxyUrl.href });
|
||||
return proxyAgent;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue