-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): Force default rate limits for some known hosts (#30207)
Co-authored-by: HonkingGoose <[email protected]> Co-authored-by: Michael Kriese <[email protected]>
- Loading branch information
1 parent
e286902
commit 8d183d6
Showing
11 changed files
with
189 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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('gets the limit from the host rules', () => { | ||
hostRules.add({ matchHost: 'example.com', concurrentRequestLimit: 123 }); | ||
expect(getConcurrentRequestsLimit('https://example.com')).toBe(123); | ||
}); | ||
|
||
it('selects default value if 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); | ||
}); | ||
|
||
it('matches wildcard host', () => { | ||
setHttpRateLimits([{ matchHost: '*', concurrency: 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('gets 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); | ||
}); | ||
|
||
it('matches wildcard host', () => { | ||
setHttpRateLimits([{ matchHost: '*', throttleMs: 123 }]); | ||
expect(getThrottleIntervalMs('https://example.com')).toBe(123); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import is from '@sindresorhus/is'; | ||
import { matchesHost } from '../host-rules'; | ||
import * as hostRules from '../host-rules'; | ||
import type { RateLimitRule } from './types'; | ||
|
||
const defaults: RateLimitRule[] = [ | ||
{ | ||
// https://guides.rubygems.org/rubygems-org-rate-limits/ | ||
matchHost: 'rubygems.org', | ||
throttleMs: 125, | ||
}, | ||
{ | ||
// https://crates.io/data-access#api | ||
matchHost: 'https://crates.io/api/', | ||
throttleMs: 1000, | ||
}, | ||
{ | ||
matchHost: '*', | ||
concurrency: 16, | ||
}, | ||
]; | ||
|
||
let limits: RateLimitRule[] = []; | ||
|
||
export function setHttpRateLimits(rules?: RateLimitRule[]): void { | ||
limits = rules ?? defaults; | ||
} | ||
|
||
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; | ||
|
||
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 limits) { | ||
if (!matches(url, matchHost) || !is.number(limit)) { | ||
continue; | ||
} | ||
|
||
if (result && result <= limit) { | ||
continue; | ||
} | ||
|
||
result = limit; | ||
break; | ||
} | ||
|
||
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 limits) { | ||
if (!matches(url, matchHost) || !is.number(limit)) { | ||
continue; | ||
} | ||
|
||
if (result && result >= limit) { | ||
continue; | ||
} | ||
|
||
result = limit; | ||
break; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters