Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): Force default rate limits for some known hosts #30207

Merged
merged 16 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions lib/modules/datasource/rubygems/http.ts

This file was deleted.

5 changes: 2 additions & 3 deletions lib/modules/datasource/rubygems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { Marshal } from '@qnighy/marshal';
import type { ZodError } from 'zod';
import { logger } from '../../../logger';
import { cache } from '../../../util/cache/package/decorator';
import { HttpError } from '../../../util/http';
import { Http, HttpError } from '../../../util/http';
import { AsyncResult, Result } from '../../../util/result';
import { getQueryString, joinUrlParts, parseUrl } from '../../../util/url';
import * as rubyVersioning from '../../versioning/ruby';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { getV1Releases } from './common';
import { RubygemsHttp } from './http';
import { MetadataCache } from './metadata-cache';
import { GemInfo, MarshalledVersionInfo } from './schema';
import { VersionsEndpointCache } from './versions-endpoint-cache';
Expand All @@ -34,7 +33,7 @@ export class RubyGemsDatasource extends Datasource {

constructor() {
super(RubyGemsDatasource.id);
this.http = new RubygemsHttp(RubyGemsDatasource.id);
this.http = new Http(RubyGemsDatasource.id);
this.versionsEndpointCache = new VersionsEndpointCache(this.http);
this.metadataCache = new MetadataCache(this.http);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/host-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface HostRuleSearch {
readOnly?: boolean;
}

function matchesHost(url: string, matchHost: string): boolean {
export function matchesHost(url: string, matchHost: string): boolean {
if (isHttpUrl(url) && isHttpUrl(matchHost)) {
return url.startsWith(matchHost);
}
Expand Down
40 changes: 40 additions & 0 deletions lib/util/http/default-limits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import is from '@sindresorhus/is';
import { matchesHost } from '../host-rules';
import type { RateLimitRule } from './types';

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

export function getDefaultThrottleIntervalMs(url: string): number | null {
for (const rule of defaultLimits) {
if (
matchesHost(url, rule.matchHost) &&
is.number(rule.throttleIntervalMs)
) {
return rule.throttleIntervalMs;

Check warning on line 22 in lib/util/http/default-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/default-limits.ts#L22

Added line #L22 was not covered by tests
}
}

return null;
}

export function getDefaultConcurrentRequestsLimit(url: string): number | null {
for (const rule of defaultLimits) {

Check warning on line 30 in lib/util/http/default-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/default-limits.ts#L30

Added line #L30 was not covered by tests
if (
matchesHost(url, rule.matchHost) &&
is.number(rule.maxConcurrentRequests)
) {
return rule.maxConcurrentRequests;

Check warning on line 35 in lib/util/http/default-limits.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/default-limits.ts#L35

Added line #L35 was not covered by tests
}
}

return null;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L39 was not covered by tests
}
8 changes: 2 additions & 6 deletions lib/util/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { hooks } from './hooks';
import { applyHostRule, findMatchingRule } from './host-rules';
import { getQueue } from './queue';
import { getRetryAfter, wrapWithRetry } from './retry-after';
import { Throttle, getThrottle } from './throttle';
import { getThrottle } from './throttle';
import type {
GotJSONOptions,
GotOptions,
Expand Down Expand Up @@ -134,10 +134,6 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
);
}

protected getThrottle(url: string): Throttle | null {
return getThrottle(url);
}

protected async request<T>(
requestUrl: string | URL,
httpOptions: InternalHttpOptions,
Expand Down Expand Up @@ -212,7 +208,7 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
return gotTask(url, options, { queueMs });
};

const throttle = this.getThrottle(url);
const throttle = getThrottle(url);
const throttledTask: GotTask<T> = throttle
? () => throttle.add<HttpResponse<T>>(httpTask)
: httpTask;
Expand Down
7 changes: 6 additions & 1 deletion lib/util/http/queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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';

const hostQueues = new Map<string, PQueue | null>();
Expand All @@ -16,7 +17,11 @@
let queue = hostQueues.get(host);
if (queue === undefined) {
queue = null; // null represents "no queue", as opposed to undefined
const concurrency = getConcurrentRequestsLimit(url);
const hostConcurrency = getConcurrentRequestsLimit(url);
const defaultConcurrency = getDefaultConcurrentRequestsLimit(url);
const concurrency = Math.min(
...[hostConcurrency, defaultConcurrency].filter((x) => x !== null),

Check failure on line 23 in lib/util/http/queue.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'number | null' is not assignable to parameter of type 'number'.

Check failure on line 23 in lib/util/http/queue.ts

View workflow job for this annotation

GitHub Actions / lint-other

Argument of type 'number | null' is not assignable to parameter of type 'number'.

Check warning on line 23 in lib/util/http/queue.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/http/queue.ts#L20-L23

Added lines #L20 - L23 were not covered by tests
zharinov marked this conversation as resolved.
Show resolved Hide resolved
);
if (concurrency) {
logger.debug(`Using queue: host=${host}, concurrency=${concurrency}`);
queue = new PQueue({ concurrency });
Expand Down
11 changes: 8 additions & 3 deletions lib/util/http/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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';

const hostThrottles = new Map<string, Throttle | null>();
Expand All @@ -9,7 +10,7 @@
private throttle: ReturnType<typeof pThrottle>;

constructor(interval: number) {
this.throttle = pThrottle({

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (6/16)

modules/manager/terraform/lockfile/hash › returns null if getBuilds returns null

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at Http.request (lib/util/http/index.ts:211:35) at Http.request [as requestJson] (lib/util/http/index.ts:289:28) at Http.requestJson [as getJson] (lib/util/http/index.ts:345:17) at TerraformProviderDatasource.getJson (lib/modules/datasource/terraform-module/base.ts:25:23) at apply (lib/util/decorator/index.ts:55:34) at callback (lib/util/cache/package/decorator.ts:127:24) at TerraformProviderDatasource.getBuilds (lib/modules/datasource/terraform-provider/index.ts:230:7) at lib/util/cache/package/decorator.ts:127:18 at Function.createHashes (lib/modules/manager/terraform/lockfile/hash.ts:157:20) at Object.<anonymous> (lib/modules/manager/terraform/lockfile/hash.spec.ts:46:20)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (6/16)

modules/manager/terraform/lockfile/hash › full walkthrough on terraform cloud

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at Http.request (lib/util/http/index.ts:211:35) at Http.request [as requestJson] (lib/util/http/index.ts:289:28) at Http.requestJson [as getJson] (lib/util/http/index.ts:345:17) at TerraformProviderDatasource.getJson (lib/modules/datasource/terraform-module/base.ts:25:23) at apply (lib/util/decorator/index.ts:55:34) at callback (lib/util/cache/package/decorator.ts:127:24) at TerraformProviderDatasource.getBuilds (lib/modules/datasource/terraform-provider/index.ts:230:7) at lib/util/cache/package/decorator.ts:127:18 at Function.createHashes (lib/modules/manager/terraform/lockfile/hash.ts:157:20) at Object.<anonymous> (lib/modules/manager/terraform/lockfile/hash.spec.ts:194:20)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (15/16)

util/http/github › HTTP › supports app mode

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as get] (lib/util/http/index.ts:254:17) at Object.<anonymous> (lib/util/http/github.spec.ts:70:23)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (15/16)

util/http/github › HTTP › supports different datasources

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as get] (lib/util/http/index.ts:254:17) at Object.<anonymous> (lib/util/http/github.spec.ts:89:33)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (15/16)

util/http/github › HTTP › paginates

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as requestJson] (lib/util/http/index.ts:289:28) at GithubHttp.requestJson [as getJson] (lib/util/http/index.ts:345:17) at Object.<anonymous> (lib/util/http/github.spec.ts:109:35)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (15/16)

util/http/github › HTTP › uses paginationField

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as requestJson] (lib/util/http/index.ts:289:28) at GithubHttp.requestJson [as getJson] (lib/util/http/index.ts:345:17) at Object.<anonymous> (lib/util/http/github.spec.ts:135:40)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (15/16)

util/http/github › HTTP › paginates with auth and repo

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as requestJson] (lib/util/http/index.ts:289:28) at GithubHttp.requestJson [as getJson] (lib/util/http/index.ts:345:17) at Object.<anonymous> (lib/util/http/github.spec.ts:171:35)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (7/16)

modules/platform/github/index › initPlatform() › should throw if using fine-grained token with GHE <3.10

expect(received).rejects.toThrow(expected) Expected substring: "Init: Fine-grained Personal Access Tokens do not support GitHub Enterprise Server API version <3.10 and cannot be used with Renovate." Received message: "Expected `interval` to be a finite number" 11 | 12 | constructor(interval: number) { > 13 | this.throttle = pThrottle({ | ^ 14 | strict: true, 15 | limit: 1, 16 | interval, at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as requestJson] (lib/util/http/index.ts:289:28) at GithubHttp.requestJson [as headJson] (lib/util/http/index.ts:375:17) at headJson (lib/modules/platform/github/index.ts:120:41) at Object.detectGhe [as initPlatform] (lib/modules/platform/github/index.ts:153:9) at Object.<anonymous> (lib/modules/platform/github/index.spec.ts:75:16) at Object.toThrow (node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:218:22) at Object.<anonymous> (lib/modules/platform/github/index.spec.ts:79:17)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (7/16)

modules/platform/github/index › initPlatform() › should throw if using fine-grained token with GHE unknown version

expect(received).rejects.toThrow(expected) Expected substring: "Init: Fine-grained Personal Access Tokens do not support GitHub Enterprise Server API version <3.10 and cannot be used with Renovate." Received message: "Expected `interval` to be a finite number" 11 | 12 | constructor(interval: number) { > 13 | this.throttle = pThrottle({ | ^ 14 | strict: true, 15 | limit: 1, 16 | interval, at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as requestJson] (lib/util/http/index.ts:289:28) at GithubHttp.requestJson [as headJson] (lib/util/http/index.ts:375:17) at headJson (lib/modules/platform/github/index.ts:120:41) at Object.detectGhe [as initPlatform] (lib/modules/platform/github/index.ts:153:9) at Object.<anonymous> (lib/modules/platform/github/index.spec.ts:87:16) at Object.toThrow (node_modules/.pnpm/[email protected]/node_modules/expect/build/index.js:218:22) at Object.<anonymous> (lib/modules/platform/github/index.spec.ts:91:17)

Check failure on line 13 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / test (7/16)

modules/platform/github/index › initPlatform() › should support fine-grained token with GHE >=3.10

TypeError: Expected `interval` to be a finite number at pThrottle (node_modules/.pnpm/[email protected]/node_modules/p-throttle/index.js:16:9) at new Throttle (lib/util/http/throttle.ts:13:30) at getThrottle (lib/util/http/throttle.ts:45:18) at GithubHttp.request (lib/util/http/index.ts:211:35) at GithubHttp.request (lib/util/http/github.ts:322:34) at GithubHttp.request [as requestJson] (lib/util/http/index.ts:289:28) at GithubHttp.requestJson [as headJson] (lib/util/http/index.ts:375:17) at headJson (lib/modules/platform/github/index.ts:120:41) at Object.detectGhe [as initPlatform] (lib/modules/platform/github/index.ts:153:9) at Object.<anonymous> (lib/modules/platform/github/index.spec.ts:106:22)
strict: true,
limit: 1,
interval,
Expand All @@ -33,9 +34,13 @@
let throttle = hostThrottles.get(host);
if (throttle === undefined) {
throttle = null; // null represents "no throttle", as opposed to undefined
const throttleOptions = getThrottleIntervalMs(url);
if (throttleOptions) {
const intervalMs = throttleOptions;
const hostThrottleMs = getThrottleIntervalMs(url);
const defaultThrottleMs = getDefaultThrottleIntervalMs(url);
const throttleMs = Math.max(
...[hostThrottleMs, defaultThrottleMs].filter((x) => x !== null),

Check failure on line 40 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'number | null' is not assignable to parameter of type 'number'.

Check failure on line 40 in lib/util/http/throttle.ts

View workflow job for this annotation

GitHub Actions / lint-other

Argument of type 'number | null' is not assignable to parameter of type 'number'.
zharinov marked this conversation as resolved.
Show resolved Hide resolved
);
if (throttleMs) {
const intervalMs = throttleMs;
logger.debug(`Using throttle ${intervalMs} intervalMs for host ${host}`);
throttle = new Throttle(intervalMs);
} else {
Expand Down
6 changes: 6 additions & 0 deletions lib/util/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ export interface HttpResponse<T = string> {

export type Task<T> = () => Promise<T>;
export type GotTask<T> = Task<HttpResponse<T>>;

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