-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(crawler): treat HTTP 429 as rate-limit condition with cooldown backoff #3642
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
Closed
SoloDevAbu
wants to merge
3
commits into
apify:master
from
SoloDevAbu:fix/rate-limit-429-cooldown-backoff
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9c83637
fix(crawler): treat HTTP 429 as rate-limit condition with cooldown ba…
SoloDevAbu 19cd219
test(crawler): cover 429 cooldown retry behavior across crawlers
SoloDevAbu 77615db
fix(basic-crawler): honor HTTP 429 Retry-After without blocking inter…
SoloDevAbu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ import { | |
| RequestQueue, | ||
| RequestQueueV1, | ||
| RequestState, | ||
| RateLimitError, | ||
| RetryRequestError, | ||
| Router, | ||
| SessionError, | ||
|
|
@@ -55,7 +56,7 @@ import { | |
| validators, | ||
| } from '@crawlee/core'; | ||
| import type { Awaitable, BatchAddRequestsResult, Dictionary, SetStatusMessageOptions } from '@crawlee/types'; | ||
| import { getObjectType, isAsyncIterable, isIterable, RobotsTxtFile, ROTATE_PROXY_ERRORS } from '@crawlee/utils'; | ||
| import { getObjectType, isAsyncIterable, isIterable, RobotsTxtFile, ROTATE_PROXY_ERRORS, sleep } from '@crawlee/utils'; | ||
| import { stringify } from 'csv-stringify/sync'; | ||
| import { ensureDir, writeFile, writeJSON } from 'fs-extra'; | ||
| import ow, { ArgumentError } from 'ow'; | ||
|
|
@@ -263,6 +264,13 @@ export interface BasicCrawlerOptions<Context extends CrawlingContext = BasicCraw | |
| */ | ||
| sameDomainDelaySecs?: number; | ||
|
|
||
| /** | ||
| * How long to wait before retrying a request that failed with a rate limit error (HTTP 429). | ||
| * This value will only be used if the server does not return a `Retry-After` header. | ||
| * @default 60 | ||
| */ | ||
| rateLimitCooldownSecs?: number; | ||
|
|
||
| /** | ||
| * Maximum number of session rotations per request. | ||
| * The crawler will automatically rotate the session in case of a proxy error or if it gets blocked by the website. | ||
|
|
@@ -547,6 +555,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| protected maxRequestRetries: number; | ||
| protected maxCrawlDepth?: number; | ||
| protected sameDomainDelayMillis: number; | ||
| protected rateLimitCooldownMillis: number; | ||
| protected domainAccessedTime: Map<string, number>; | ||
| protected maxSessionRotations: number; | ||
| protected maxRequestsPerCrawl?: number; | ||
|
|
@@ -598,6 +607,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| handleFailedRequestFunction: ow.optional.function, | ||
| maxRequestRetries: ow.optional.number, | ||
| sameDomainDelaySecs: ow.optional.number, | ||
| rateLimitCooldownSecs: ow.optional.number, | ||
| maxSessionRotations: ow.optional.number, | ||
| maxRequestsPerCrawl: ow.optional.number, | ||
| maxCrawlDepth: ow.optional.number, | ||
|
|
@@ -641,6 +651,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| requestManager, | ||
| maxRequestRetries = 3, | ||
| sameDomainDelaySecs = 0, | ||
| rateLimitCooldownSecs = 60, | ||
| maxSessionRotations = 10, | ||
| maxRequestsPerCrawl, | ||
| maxCrawlDepth, | ||
|
|
@@ -768,6 +779,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| this.maxRequestRetries = maxRequestRetries; | ||
| this.maxCrawlDepth = maxCrawlDepth; | ||
| this.sameDomainDelayMillis = sameDomainDelaySecs * 1000; | ||
| this.rateLimitCooldownMillis = rateLimitCooldownSecs * 1000; | ||
| this.maxSessionRotations = maxSessionRotations; | ||
| this.stats = new Statistics({ | ||
| logMessage: `${log.getOptions().prefix} request statistics:`, | ||
|
|
@@ -1674,7 +1686,9 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| throw secondaryError; | ||
| } | ||
| // decrease the session score if the request fails (but the error handler did not throw) | ||
| crawlingContext.session?.markBad(); | ||
| if (!(err instanceof RateLimitError)) { | ||
| crawlingContext.session?.markBad(); | ||
| } | ||
| } finally { | ||
| await this._cleanupContext(crawlingContext); | ||
|
|
||
|
|
@@ -1850,6 +1864,13 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| retryCount, | ||
| }); | ||
|
|
||
| if (error instanceof RateLimitError) { | ||
| const delayMillis = error.delayMillis || this.rateLimitCooldownMillis; | ||
| await sleep(delayMillis); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if |
||
| await source.reclaimRequest(request, { forefront: request.userData?.__crawlee?.forefront }); | ||
| return; | ||
| } | ||
|
|
||
| await source.reclaimRequest(request, { forefront: request.userData?.__crawlee?.forefront }); | ||
| return; | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| export const BLOCKED_STATUS_CODES = [401, 403, 429]; | ||
| export const BLOCKED_STATUS_CODES = [401, 403]; | ||
| export const PERSIST_STATE_KEY = 'SDK_SESSION_POOL_STATE'; | ||
| export const MAX_POOL_SIZE = 1000; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that for backward compatibility, we should retry immediately. This change in default behaviour might make some users' workflows run much longer.