Skip to main content

Scheduler

Struct Scheduler 

Source
pub struct Scheduler { /* private fields */ }
Expand description

Manages the request queue and tracks visited URLs to prevent duplicate crawling.

The Scheduler is responsible for:

  • Maintaining a queue of pending requests
  • Tracking which URLs have been visited using a Bloom Filter and LRU cache
  • Providing backpressure when too many requests are pending
  • Supporting checkpoint-based state restoration

§Architecture

The scheduler runs as a separate async task and communicates via message passing. This design ensures thread-safe access without requiring explicit locks.

§Duplicate Detection

The scheduler uses a two-tier approach for duplicate detection:

  1. Bloom Filter: Fast, memory-efficient probabilistic check (may have false positives)
  2. LRU Cache: Definitive check with TTL-based eviction

Requests are first checked against the Bloom Filter. If it indicates a possible duplicate, the LRU cache is consulted for confirmation.

Implementations§

Source§

impl Scheduler

Source

pub fn new( _initial_state: Option<()>, ) -> (Arc<Scheduler>, AsyncReceiver<Request>)

Creates a new Scheduler and returns a tuple containing the scheduler and a request receiver.

This method initializes the scheduler with optional checkpoint state for resuming interrupted crawls. When checkpoint data is provided, the scheduler restores:

  • Pending request queue
  • Visited URL cache
  • Salvaged requests

The scheduler spawns two background tasks:

  1. Bloom Filter Flush Task: Periodically flushes the URL fingerprint buffer
  2. Run Loop Task: Processes incoming messages and dispatches requests
§Parameters
  • initial_state: Optional checkpoint state to restore from a previous crawl
§Returns

A tuple of (Arc<Scheduler>, AsyncReceiver<Request>):

  • Arc<Scheduler>: Thread-safe reference to the scheduler for sending commands
  • AsyncReceiver<Request>: Channel receiver for consuming scheduled requests
§Example
let (scheduler, request_rx) = Scheduler::new(None::<()>);
Source

pub async fn snapshot(&self) -> Result<(), SpiderError>

Source

pub async fn enqueue_request(&self, request: Request) -> Result<(), SpiderError>

Source

pub async fn shutdown(&self) -> Result<(), SpiderError>

Signals the scheduler loop to stop processing new work.

§Errors

Returns an error when the shutdown message cannot be sent.

Source

pub async fn mark_visited(&self, fingerprint: String) -> Result<(), SpiderError>

Marks a single fingerprint as visited.

§Errors

Returns an error when the message cannot be delivered to the scheduler loop.

Source

pub async fn mark_visited_batch( &self, fingerprints: Vec<String>, ) -> Result<(), SpiderError>

Marks multiple fingerprints as visited in one message.

If fingerprints is empty, this method returns immediately.

§Errors

Returns an error when the batch message cannot be delivered to the scheduler loop.

Source

pub fn is_visited(&self, fingerprint: &str) -> bool

Returns true if fingerprint has already been visited.

Source

pub fn should_enqueue(&self, request: &Request) -> bool

Returns true when request has not been visited yet.

Source

pub fn len(&self) -> usize

Returns the number of pending requests in the scheduler.

Source

pub fn is_empty(&self) -> bool

Returns true if the scheduler has no pending requests.

Source

pub fn is_idle(&self) -> bool

Returns true when the scheduler is currently idle.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more