Limits | rust-crawler | Spatie       

 SPATIE  

rust-crawler
============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Rust-crawler](https://spatie.be/docs/rust-crawler/v1)  Configuring-the-crawler  Limits

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/rust-crawler/v1) 

  Limits    
- [ Introduction ](https://spatie.be/docs/rust-crawler/v1/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/rust-crawler/v1/installation-setup)
- [ Support us ](https://spatie.be/docs/rust-crawler/v1/support-us)
- [ Questions and issues ](https://spatie.be/docs/rust-crawler/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/rust-crawler/v1/changelog)
- [ About us ](https://spatie.be/docs/rust-crawler/v1/about-us)

Basic usage
-----------

- [ Your first crawl ](https://spatie.be/docs/rust-crawler/v1/basic-usage/starting-your-first-crawl)
- [ Crawl responses ](https://spatie.be/docs/rust-crawler/v1/basic-usage/handling-crawl-responses)
- [ Using observers ](https://spatie.be/docs/rust-crawler/v1/basic-usage/using-observers)
- [ Collecting URLs ](https://spatie.be/docs/rust-crawler/v1/basic-usage/collecting-urls)
- [ Filtering URLs ](https://spatie.be/docs/rust-crawler/v1/basic-usage/filtering-urls)
- [ Testing ](https://spatie.be/docs/rust-crawler/v1/basic-usage/testing)
- [ Tracking progress ](https://spatie.be/docs/rust-crawler/v1/basic-usage/tracking-progress)

Configuring the crawler
-----------------------

- [ Concurrency &amp; throttling ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/crawl-behavior)
- [ Limits ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/setting-crawl-limits)
- [ Extracting resources ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/extracting-resources)
- [ Configuring requests ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/configuring-requests)
- [ Response filtering ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/handling-responses)
- [ Respecting robots.txt ](https://spatie.be/docs/rust-crawler/v1/configuring-the-crawler/respecting-robots-txt)

Advanced usage
--------------

- [ JavaScript rendering ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/rendering-javascript)
- [ Custom link extraction ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/extracting-custom-links)
- [ Custom request handlers ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/custom-request-handlers)
- [ Crawling across requests ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/crawling-across-requests)
- [ Custom crawl queue ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/using-a-custom-crawl-queue)
- [ Graceful shutdown ](https://spatie.be/docs/rust-crawler/v1/advanced-usage/graceful-shutdown)

 Limits
======

###  On this page 

1. [ Crawl depth ](#content-crawl-depth)
2. [ Crawl and time limits ](#content-crawl-and-time-limits)
3. [ Using the total crawl limit ](#content-using-the-total-crawl-limit)
4. [ Using the current crawl limit ](#content-using-the-current-crawl-limit)
5. [ Using time limits ](#content-using-time-limits)
6. [ Combining limits ](#content-combining-limits)

By default, the crawler continues until it has crawled every page it can find. This behavior might cause issues if you are working in an environment with limitations such as a serverless environment.

Crawl depth
-----------------------------------------------------------------------------------------

You can limit how deep the crawler will go using the `max_depth` method.

```
use spatie_crawler::Crawler;

fn shallow_crawler() -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .max_depth(2)
        .build()
}
```

A depth of 0 means only the start URL will be crawled. A depth of 1 means the start URL and any pages it links to, and so on. Additional `seed_urls` are independent roots at depth 0.

The crawler fetches pages at the maximum depth but does not schedule their discoveries. XML sitemap discoveries preserve the sitemap's depth rather than adding an HTML-link level, but discovery scheduling still stops at the configured maximum depth. First discovery wins during deduplication; concurrency does not guarantee that the recorded depth is the shortest path.

Crawl and time limits
-----------------------------------------------------------------------------------------------------------------------

The crawl behavior can be controlled with these options:

MethodBudget`max_pages(count)`URL jobs in this execution`max_pages_total(count)`Persisted URL-attempt budget across executions`time_limit(duration)`Elapsed time in this execution`time_limit_total(duration)`Cumulative elapsed time across executionsAll four default to unlimited. When both execution and cumulative limits are set, the tighter remaining budget wins. Zero pages is valid and fetches nothing. When a limit stops the crawl with work remaining, the final `CrawlEvent::Finished` reports `FinishReason::PageLimit` or `FinishReason::TimeLimit`. Inspect its `error` too for storage or scheduler failures.

Page budgets count URL jobs, including HTTP/transport failures, MIME skips and noindex skips. A retry stays in the same job. Scope, depth and robots.txt refusals before fetching do not consume page attempts. Robots requests are not page attempts. Attempt slots are reserved before concurrent work, preventing page-budget overshoot. For persisted budget and crash behavior, see [checkpoint guarantees](../advanced-usage/crawling-across-requests#checkpoint-guarantees).

Using the total crawl limit
-----------------------------------------------------------------------------------------------------------------------------------------

The `max_pages_total` method allows you to limit the total number of URL jobs across executions. Use the same `SqliteStore` checkpoint to resume the crawl.

```
use std::path::Path;
use spatie_crawler::{Crawler, SqliteStore};

fn total_limit(database: &Path) -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .store(SqliteStore::open(database)?)
        .max_pages_total(5)
        .build()
}
```

Once the stored attempt count reaches 5, reopening the same checkpoint with this limit will not fetch more URLs. See [checkpoint guarantees](../advanced-usage/crawling-across-requests#checkpoint-guarantees) for ownership, recovery and durability requirements.

Using the current crawl limit
-----------------------------------------------------------------------------------------------------------------------------------------------

The `max_pages` method limits how many URL jobs will be processed in a single execution. This is especially useful when [crawling across multiple requests](../advanced-usage/crawling-across-requests).

```
use std::path::Path;
use spatie_crawler::{Crawler, SqliteStore};

fn current_limit(database: &Path) -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .store(SqliteStore::open(database)?)
        .max_pages(5)
        .build()
}
```

Each execution processes up to 5 URL jobs, without a total page limit. Reopen the checkpoint after the previous stream has ended to process the next batch.

Using time limits
-----------------------------------------------------------------------------------------------------------

The `time_limit_total` method sets the maximum execution time across all executions. The `time_limit` method sets the maximum execution time for a single crawl. Both accept a `Duration`.

```
use std::path::Path;
use std::time::Duration;
use spatie_crawler::{Crawler, SqliteStore};

fn timed_crawler(database: &Path) -> spatie_crawler::Result {
    Crawler::builder("https://example.com")?
        .store(SqliteStore::open(database)?)
        .time_limit_total(Duration::from_secs(60))
        .time_limit(Duration::from_secs(30))
        .build()
}
```

Elapsed time includes initialization and output backpressure. `Progress::elapsed_ms` describes the current execution; `total_elapsed_ms` includes prior executions. See [checkpoint guarantees](../advanced-usage/crawling-across-requests#checkpoint-guarantees) for when elapsed time is persisted.

Time limits are not process deadlines. They stop scheduling work and recover pending claims, but do not guarantee the process exits or output flushes at that instant. A blocked observer or event consumer can delay completion. Already-running blocking extraction can outlive cancellation. Keep draining the stream after cancellation to receive the final event.

Use [request timeouts](configuring-requests#timeouts) to bound individual asynchronous fetches and renderer calls. Use [response limits](handling-responses#maximum-response-size) to bound individual bodies. Neither those limits nor the event buffer provide a global memory bound, and there is no total-frontier-size limit.

Combining limits
--------------------------------------------------------------------------------------------------------

All limits can be combined to control the crawler. This function processes at most 5 URL jobs per invocation, up to 10 persisted attempts in total, with execution and cumulative time limits.

```
use std::path::Path;
use std::time::Duration;
use spatie_crawler::{CrawlEvent, Crawler, FinishReason, SqliteStore};

async fn run_batch(database: &Path) -> Result {
    let mut crawl = Crawler::builder("https://example.com")?
        .store(SqliteStore::open(database)?)
        .max_pages(5)
        .max_pages_total(10)
        .time_limit(Duration::from_secs(30))
        .time_limit_total(Duration::from_secs(60))
        .build()?
        .crawl();

    let mut reason = FinishReason::Completed;
    while let Some(event) = crawl.next().await {
        if let CrawlEvent::Finished {
            reason: finished,
            error,
            ..
        } = event
        {
            if let Some(error) = error {
                return Err(error.into());
            }
            reason = finished;
        }
    }
    Ok(reason)
}
```

Reopening the same database resumes remaining work. Keep the same seed and apply the desired configuration on each invocation. See [checkpoint guarantees](../advanced-usage/crawling-across-requests#checkpoint-guarantees) before running batches against the same store.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
