Skip to content

Commit

Permalink
Wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 17, 2024
1 parent 9b950e1 commit dff84f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/util/http/rate-limit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe('util/http/rate-limit', () => {
hostRules.add({ matchHost: 'example.com', concurrentRequestLimit: 123 });
expect(getConcurrentRequestsLimit('https://example.com')).toBe(123);
});

it('matches wildcard host', () => {
setHttpRateLimits([{ matchHost: '*', concurrency: 123 }]);
expect(getConcurrentRequestsLimit('https://example.com')).toBe(123);
});
});

describe('getThrottleIntervalMs', () => {
Expand Down Expand Up @@ -69,5 +74,10 @@ describe('util/http/rate-limit', () => {
hostRules.add({ matchHost: 'example.com', maxRequestsPerSecond: 2 });
expect(getThrottleIntervalMs('https://example.com')).toBe(500);
});

it('matches wildcard host', () => {
setHttpRateLimits([{ matchHost: '*', throttleMs: 123 }]);
expect(getThrottleIntervalMs('https://example.com')).toBe(123);
});
});
});
16 changes: 14 additions & 2 deletions lib/util/http/rate-limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const defaults: RateLimitRule[] = [
matchHost: 'https://crates.io/api/',
throttleMs: 1000,
},
{
matchHost: '*',
concurrency: 8,
},
];

let limits: RateLimitRule[] = [];
Expand All @@ -22,6 +26,14 @@ export function setHttpRateLimits(rules: RateLimitRule[] = defaults): void {
limits = rules;
}

function matches(url: string, host: string): boolean {
if (host === '*') {
return true;
}

return matchesHost(url, host);
}

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

Expand All @@ -35,7 +47,7 @@ export function getConcurrentRequestsLimit(url: string): number | null {
}

for (const { matchHost, concurrency: limit } of limits) {
if (!matchesHost(url, matchHost) || !is.number(limit)) {
if (!matches(url, matchHost) || !is.number(limit)) {
continue;
}

Expand All @@ -58,7 +70,7 @@ export function getThrottleIntervalMs(url: string): number | null {
}

for (const { matchHost, throttleMs: limit } of limits) {
if (!matchesHost(url, matchHost) || !is.number(limit)) {
if (!matches(url, matchHost) || !is.number(limit)) {
continue;
}

Expand Down

0 comments on commit dff84f5

Please sign in to comment.