2020-12-11 02:09:09 -07:00
|
|
|
import PQueue from 'p-queue';
|
2022-09-01 01:36:04 -06:00
|
|
|
import { logger } from '../../logger';
|
2022-01-07 04:44:55 -07:00
|
|
|
import { parseUrl } from '../url';
|
2024-07-22 10:59:29 -06:00
|
|
|
import { getConcurrentRequestsLimit } from './rate-limits';
|
2020-12-11 02:09:09 -07:00
|
|
|
|
2022-09-01 01:36:04 -06:00
|
|
|
const hostQueues = new Map<string, PQueue | null>();
|
2020-12-11 02:09:09 -07:00
|
|
|
|
2024-03-07 01:01:34 -07:00
|
|
|
export function getQueue(url: string): PQueue | null {
|
2022-01-07 04:44:55 -07:00
|
|
|
const host = parseUrl(url)?.host;
|
2020-12-11 02:09:09 -07:00
|
|
|
if (!host) {
|
2022-09-01 01:36:04 -06:00
|
|
|
// should never happen
|
2022-11-07 04:29:02 -07:00
|
|
|
logger.debug(`No host on ${url}`);
|
2020-12-11 02:09:09 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let queue = hostQueues.get(host);
|
|
|
|
if (queue === undefined) {
|
|
|
|
queue = null; // null represents "no queue", as opposed to undefined
|
2024-03-07 01:01:34 -07:00
|
|
|
const concurrency = getConcurrentRequestsLimit(url);
|
2020-12-11 02:09:09 -07:00
|
|
|
if (concurrency) {
|
2022-10-21 00:02:05 -06:00
|
|
|
logger.debug(`Using queue: host=${host}, concurrency=${concurrency}`);
|
2020-12-11 02:09:09 -07:00
|
|
|
queue = new PQueue({ concurrency });
|
2022-09-01 01:36:04 -06:00
|
|
|
} else {
|
2022-10-29 00:15:21 -06:00
|
|
|
logger.trace({ host }, 'No concurrency limits');
|
2020-12-11 02:09:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hostQueues.set(host, queue);
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clear(): void {
|
|
|
|
hostQueues.clear();
|
|
|
|
}
|