Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 17, 2024
1 parent bef838b commit a660eb8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 70 deletions.
40 changes: 0 additions & 40 deletions lib/util/http/default-limits.ts

This file was deleted.

14 changes: 0 additions & 14 deletions lib/util/http/host-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,3 @@ export function applyHostRule<GotOptions extends HostRulesGotOptions>(

return options;
}

export function getConcurrentRequestsLimit(url: string): number | null {
const { concurrentRequestLimit } = hostRules.find({ url });
return is.number(concurrentRequestLimit) && concurrentRequestLimit > 0
? concurrentRequestLimit
: null;
}

export function getThrottleIntervalMs(url: string): number | null {
const { maxRequestsPerSecond } = hostRules.find({ url });
return is.number(maxRequestsPerSecond) && maxRequestsPerSecond > 0
? Math.ceil(1000 / maxRequestsPerSecond)
: null;
}
9 changes: 2 additions & 7 deletions lib/util/http/queue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import PQueue from 'p-queue';
import { logger } from '../../logger';
import { parseUrl } from '../url';
import { getDefaultConcurrentRequestsLimit } from './default-limits';
import { getConcurrentRequestsLimit } from './host-rules';
import { getConcurrentRequestsLimit } from './rate-limits';

const hostQueues = new Map<string, PQueue | null>();

Expand All @@ -17,11 +16,7 @@ export function getQueue(url: string): PQueue | null {
let queue = hostQueues.get(host);
if (queue === undefined) {
queue = null; // null represents "no queue", as opposed to undefined
const hostConcurrency = getConcurrentRequestsLimit(url);
const defaultConcurrency = getDefaultConcurrentRequestsLimit(url);
const concurrency = Math.min(
...[hostConcurrency, defaultConcurrency].filter((x) => x !== null),
);
const concurrency = getConcurrentRequestsLimit(url);
if (concurrency) {
logger.debug(`Using queue: host=${host}, concurrency=${concurrency}`);
queue = new PQueue({ concurrency });
Expand Down
73 changes: 73 additions & 0 deletions lib/util/http/rate-limits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import is from '@sindresorhus/is';
import { matchesHost } from '../host-rules';
import * as hostRules from '../host-rules';
import type { RateLimitRule } from './types';

const defaults: RateLimitRule[] = [
{
matchHost: 'rubygems.org',
throttleMs: 125,
},
{
matchHost: 'https://crates.io/api/',
throttleMs: 1000,
},
];

export function getConcurrentRequestsLimit(url: string): number | null {
let result: number | null = null;

const { concurrentRequestLimit: hostRuleLimit } = hostRules.find({ url });
if (
is.number(hostRuleLimit) &&
hostRuleLimit > 0 &&
hostRuleLimit < Number.MAX_SAFE_INTEGER
) {
result = hostRuleLimit;
}

for (const { matchHost, concurrency: limit } of defaults) {
if (!matchesHost(url, matchHost)) {
continue;
}

if (!is.number(limit)) {
continue;
}

if (result && result <= limit) {
continue;

Check warning on line 39 in lib/util/http/rate-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/rate-limits.ts#L39

Added line #L39 was not covered by tests
}

result = limit;

Check warning on line 42 in lib/util/http/rate-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/rate-limits.ts#L42

Added line #L42 was not covered by tests
}

return result;
}

export function getThrottleIntervalMs(url: string): number | null {
let result: number | null = null;

const { maxRequestsPerSecond } = hostRules.find({ url });
if (is.number(maxRequestsPerSecond) && maxRequestsPerSecond > 0) {
result = Math.ceil(1000 / maxRequestsPerSecond);
}

for (const { matchHost, throttleMs: limit } of defaults) {
if (!matchesHost(url, matchHost)) {
continue;
}

if (!is.number(limit)) {
continue;

Check warning on line 62 in lib/util/http/rate-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/rate-limits.ts#L62

Added line #L62 was not covered by tests
}

if (result && result >= limit) {
continue;

Check warning on line 66 in lib/util/http/rate-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/rate-limits.ts#L66

Added line #L66 was not covered by tests
}

result = limit;
}

return result;
}
9 changes: 2 additions & 7 deletions lib/util/http/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pThrottle from 'p-throttle';
import { logger } from '../../logger';
import { parseUrl } from '../url';
import { getDefaultThrottleIntervalMs } from './default-limits';
import { getThrottleIntervalMs } from './host-rules';
import { getThrottleIntervalMs } from './rate-limits';

const hostThrottles = new Map<string, Throttle | null>();

Expand Down Expand Up @@ -34,11 +33,7 @@ export function getThrottle(url: string): Throttle | null {
let throttle = hostThrottles.get(host);
if (throttle === undefined) {
throttle = null; // null represents "no throttle", as opposed to undefined
const hostThrottleMs = getThrottleIntervalMs(url);
const defaultThrottleMs = getDefaultThrottleIntervalMs(url);
const throttleMs = Math.max(
...[hostThrottleMs, defaultThrottleMs].filter((x) => x !== null),
);
const throttleMs = getThrottleIntervalMs(url);
if (throttleMs) {
const intervalMs = throttleMs;
logger.debug(`Using throttle ${intervalMs} intervalMs for host ${host}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/util/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ export type GotTask<T> = Task<HttpResponse<T>>;

export interface RateLimitRule {
matchHost: string;
throttleIntervalMs?: number;
maxConcurrentRequests?: number;
throttleMs?: number;
concurrency?: number;
}

0 comments on commit a660eb8

Please sign in to comment.