forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.ts
40 lines (35 loc) · 1.01 KB
/
queue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import PQueue from 'p-queue';
import { logger } from '../../logger';
import { parseUrl } from '../url';
import { getConcurrentRequestsLimit } from './host-rules';
const hostQueues = new Map<string, PQueue | null>();
export function getQueue(
url: string,
defaultConcurrentRequestLimit: number | null,
): PQueue | null {
const host = parseUrl(url)?.host;
if (!host) {
// should never happen
logger.debug(`No host on ${url}`);
return null;
}
let queue = hostQueues.get(host);
if (queue === undefined) {
queue = null; // null represents "no queue", as opposed to undefined
const concurrency = getConcurrentRequestsLimit(
url,
defaultConcurrentRequestLimit,
);
if (concurrency) {
logger.debug(`Using queue: host=${host}, concurrency=${concurrency}`);
queue = new PQueue({ concurrency });
} else {
logger.trace({ host }, 'No concurrency limits');
}
}
hostQueues.set(host, queue);
return queue;
}
export function clear(): void {
hostQueues.clear();
}