Skip to content

Commit

Permalink
Implement initializer and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 17, 2024
1 parent a660eb8 commit d14be67
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
73 changes: 73 additions & 0 deletions lib/util/http/rate-limit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as hostRules from '../host-rules';
import {
getConcurrentRequestsLimit,
getThrottleIntervalMs,
setHttpRateLimits,
} from './rate-limits';

describe('util/http/rate-limit', () => {
beforeEach(() => {
hostRules.clear();
setHttpRateLimits([]);
});

describe('getConcurrentRequestsLimit', () => {
it('returns null if no limits are set', () => {
expect(getConcurrentRequestsLimit('https://example.com')).toBeNull();
});

it('returns null if host does not match', () => {
setHttpRateLimits([
{ matchHost: 'https://crates.io/api/', throttleMs: 1000 },
]);
expect(getConcurrentRequestsLimit('https://index.crates.io')).toBeNull();
});

it('obtains the limit from the host rules', () => {
hostRules.add({ matchHost: 'example.com', concurrentRequestLimit: 123 });
expect(getConcurrentRequestsLimit('https://example.com')).toBe(123);
});

it('selects default value is host rule is greater', () => {
setHttpRateLimits([{ matchHost: 'example.com', concurrency: 123 }]);
hostRules.add({ matchHost: 'example.com', concurrentRequestLimit: 456 });
expect(getConcurrentRequestsLimit('https://example.com')).toBe(123);
});

it('selects host rule value if default is greater', () => {
setHttpRateLimits([{ matchHost: 'example.com', concurrency: 456 }]);
hostRules.add({ matchHost: 'example.com', concurrentRequestLimit: 123 });
expect(getConcurrentRequestsLimit('https://example.com')).toBe(123);
});
});

describe('getThrottleIntervalMs', () => {
it('returns null if no limits are set', () => {
expect(getThrottleIntervalMs('https://example.com')).toBeNull();
});

it('returns null if host does not match', () => {
setHttpRateLimits([
{ matchHost: 'https://crates.io/api/', concurrency: 123 },
]);
expect(getThrottleIntervalMs('https://index.crates.io')).toBeNull();
});

it('obtains the limit from the host rules', () => {
hostRules.add({ matchHost: 'example.com', maxRequestsPerSecond: 8 });
expect(getThrottleIntervalMs('https://example.com')).toBe(125);
});

it('selects maximum throttle when default is greater', () => {
setHttpRateLimits([{ matchHost: 'example.com', throttleMs: 500 }]);
hostRules.add({ matchHost: 'example.com', maxRequestsPerSecond: 8 });
expect(getThrottleIntervalMs('https://example.com')).toBe(500);
});

it('selects maximum throttle when host rule is greater', () => {
setHttpRateLimits([{ matchHost: 'example.com', throttleMs: 125 }]);
hostRules.add({ matchHost: 'example.com', maxRequestsPerSecond: 2 });
expect(getThrottleIntervalMs('https://example.com')).toBe(500);
});
});
});
22 changes: 10 additions & 12 deletions lib/util/http/rate-limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const defaults: RateLimitRule[] = [
},
];

let limits: RateLimitRule[] = [];

export function setHttpRateLimits(rules: RateLimitRule[] = defaults): void {
limits = rules;
}

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

Expand All @@ -26,12 +32,8 @@ export function getConcurrentRequestsLimit(url: string): number | null {
result = hostRuleLimit;
}

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

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

Expand All @@ -53,12 +55,8 @@ export function getThrottleIntervalMs(url: string): number | null {
result = Math.ceil(1000 / maxRequestsPerSecond);
}

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

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

Expand Down
2 changes: 2 additions & 0 deletions lib/workers/global/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { validateGitVersion } from '../../util/git';
import * as hostRules from '../../util/host-rules';
import { initMergeConfidence } from '../../util/merge-confidence';
import { setMaxLimit } from './limits';
import { setHttpRateLimits } from '../../util/http/rate-limits';

Check failure on line 15 in lib/workers/global/initialize.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

`../../util/http/rate-limits` import should occur before import of `../../util/merge-confidence`

async function setDirectories(input: AllConfig): Promise<AllConfig> {
const config: AllConfig = { ...input };
Expand Down Expand Up @@ -79,6 +80,7 @@ export async function globalInitialize(
config_: AllConfig,
): Promise<RenovateConfig> {
let config = config_;
setHttpRateLimits();
await checkVersions();
setGlobalHostRules(config);
config = await initPlatform(config);
Expand Down

0 comments on commit d14be67

Please sign in to comment.