Skip to content
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
2 changes: 1 addition & 1 deletion docs/03_concepts/19_request_throttling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To use request throttling, create a <ApiLink to="class/ThrottlingRequestManager"

2. **429 backoff**: When the crawler detects an HTTP 429 response, the `ThrottlingRequestManager` records an exponential backoff delay for that domain (starting at 2s, doubling up to 60s). Requests already in flight when the limit was hit are treated as a single rate-limit event, so the delay doubles once per backoff window rather than once per 429. Once the domain goes a full extra window without rate-limiting, the next 429 starts the backoff over at the initial delay. If the response includes a `Retry-After` header with a positive delay, that value takes priority.

3. **Crawl-delay**: If `robots.txt` specifies a `crawl-delay`, the manager enforces a minimum interval between requests to that domain. To space out the requests to every listed domain, set `min_crawl_delay`. A longer `crawl-delay` from `robots.txt` still takes precedence.
3. **Crawl-delay**: If `robots.txt` specifies a `crawl-delay`, the manager enforces a minimum interval between requests to that domain. When several `robots.txt` files cover one domain, such as HTTP and HTTPS of one host or the hosts of a site with `throttle_by='registrable_domain'`, the longest crawl-delay applies. To space out the requests to every listed domain, set `min_crawl_delay`. A longer `crawl-delay` from `robots.txt` still takes precedence.

4. **Fair scheduling**: `fetch_next_request` sorts available sub-managers by how long each domain has been waiting, ensuring no domain is starved.

Expand Down
2 changes: 2 additions & 0 deletions docs/06_upgrading/upgrading_to_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ If you have a custom request manager, implement `record_pacing_signal`. Return `

`ThrottlingRequestManager` now rejects a `base_delay` or `max_delay` that isn't positive, so a zero `base_delay` no longer works as a way to honor only `Retry-After`.

A domain's crawl-delay now follows the longest `MinIntervalSignal` it gets. In v1, `set_crawl_delay` kept the first value and ignored later ones.
Comment thread
Mantisus marked this conversation as resolved.
Resolved

A `RequestThrottledError` you raise yourself now starts the domain's backoff, and the request is deferred if the request manager paces its domain. In v1, it was an ordinary error unless you had called `record_domain_delay` first. A deferred request never fails, so raise the error only when the server has refused the request. In `AdaptivePlaywrightCrawler`, such an error from the static run now defers the request instead of falling back to the browser.

## `ThrottlingRequestManager.inner` is optional
Expand Down
9 changes: 5 additions & 4 deletions src/crawlee/request_loaders/_throttling_request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ def record_pacing_signal(self, signal: PacingSignal) -> bool:

A `RateLimitedSignal` backs the domain off for the signal's `wait` if it's positive, and exponentially from
`base_delay` otherwise. Only the first refusal of a burst advances the backoff. A `MinIntervalSignal` becomes
the domain's crawl-delay, unless `min_crawl_delay` is longer. The first one wins, so a robots.txt re-fetch can't
change the dispatch cadence mid-crawl.
the domain's crawl-delay, unless the domain already has a longer one or `min_crawl_delay` is longer. So the
strictest robots.txt among the hosts a domain covers sets the pace, and a robots.txt re-fetch can't speed up the
Comment thread
Mantisus marked this conversation as resolved.
Resolved
dispatch cadence.

Returns:
`True` if the URL's domain is in `domains`, so the manager paces it by the signal. `False` if the domain
Expand Down Expand Up @@ -571,12 +572,12 @@ def _record_rate_limit(self, url: str, wait: timedelta | None) -> bool:
return True

def _record_declared_interval(self, url: str, interval: timedelta) -> bool:
"""Record a declared minimum interval as the crawl-delay of the URL's domain, unless it already has one."""
"""Record a declared minimum interval as the crawl-delay of the URL's domain, unless it has a longer one."""
state = self._get_domain_state(url)
if state is None:
return False

if state.crawl_delay is None:
if state.crawl_delay is None or interval > state.crawl_delay:
Comment thread
Mantisus marked this conversation as resolved.
Resolved
state.crawl_delay = interval
Comment thread
vdusek marked this conversation as resolved.
Resolved
logger.debug('Set crawl-delay for domain "%s" to %.1fs', state.domain, interval.total_seconds())

Expand Down
25 changes: 18 additions & 7 deletions tests/unit/test_throttling_request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,19 +528,30 @@ async def test_crawl_delay_integration(manager: ThrottlingRequestManager[Request
assert state.crawl_delay == timedelta(seconds=5)


async def test_min_interval_keeps_first_value(manager: ThrottlingRequestManager[RequestQueue]) -> None:
"""A later min-interval signal is accepted but doesn't change the crawl-delay."""
@pytest.mark.parametrize(
('later', 'expected'),
[
pytest.param(timedelta(seconds=9), timedelta(seconds=9), id='longer-raises'),
pytest.param(timedelta(seconds=2), timedelta(seconds=5), id='shorter-ignored'),
],
)
async def test_min_interval_keeps_longest_value(
manager: ThrottlingRequestManager[RequestQueue],
later: timedelta,
expected: timedelta,
) -> None:
"""A later min-interval signal is accepted and can raise the crawl-delay, but never lowers it."""
url = f'https://{THROTTLED_DOMAIN}/page1'
manager.record_pacing_signal(MinIntervalSignal(url, interval=timedelta(seconds=5), scope='hostname'))

accepted = manager.record_pacing_signal(MinIntervalSignal(url, interval=timedelta(seconds=9), scope='hostname'))
accepted = manager.record_pacing_signal(MinIntervalSignal(url, interval=later, scope='hostname'))

assert accepted is True
assert manager._domain_states[THROTTLED_DOMAIN].crawl_delay == timedelta(seconds=5)
assert manager._domain_states[THROTTLED_DOMAIN].crawl_delay == expected


async def test_site_keeps_first_crawl_delay(site_manager: ThrottlingRequestManager[RequestQueue]) -> None:
"""With `registrable_domain`, the first crawl-delay from any host of a site paces the whole site."""
async def test_site_keeps_longest_crawl_delay(site_manager: ThrottlingRequestManager[RequestQueue]) -> None:
"""With `registrable_domain`, the longest crawl-delay among the hosts of a site paces the whole site."""
site_manager.record_pacing_signal(
MinIntervalSignal('https://www.example.com/page1', interval=timedelta(seconds=5), scope='hostname')
)
Expand All @@ -550,7 +561,7 @@ async def test_site_keeps_first_crawl_delay(site_manager: ThrottlingRequestManag
)

assert accepted is True
assert site_manager._domain_states['example.com'].crawl_delay == timedelta(seconds=5)
assert site_manager._domain_states['example.com'].crawl_delay == timedelta(seconds=9)


async def test_min_interval_refused_unconfigured(manager: ThrottlingRequestManager[RequestQueue]) -> None:
Expand Down
Loading