Skip to main content

Context

Struct Context 

Source
pub struct Context<C> { /* private fields */ }
Expand description

A request being crawled, with everything needed to handle it.

It holds the Request, a session to fetch it with, a Queue to add the links it leads to, and the crawl’s datasets. Handlers borrow it, so several of them can handle one request: the request is fetched once, by the first response or body, and the complete response is kept. Its body is a fixed snapshot, including for browser sessions; live document operations go through the session.

The crawler learns how the fetch went, its time and status, from the task the request runs on. Fetching after moving the context to another task, e.g. with tokio::spawn, works, but adaptive host limits and Stats::unchanged do not see it.

Implementations§

Source§

impl<C> Context<C>

Source

pub fn new(request: Request, session: C) -> Self

Creates a standalone context, e.g. to test a handler.

It gets an empty queue of its own and no datasets; see with_queue and with_dataset.

Source

pub fn with_queue(self, queue: &Queue) -> Self

Replaces the queue, binding it to the request.

Source

pub fn with_dataset<T, D>(self, dataset: D) -> Self
where T: Send + 'static, D: Dataset<T>,

Registers dataset for items of type T.

Source

pub fn with_extension<T: Clone + Send + Sync + 'static>(self, value: T) -> Self

Adds a service to this request, replacing any value of the same type.

Source

pub const fn request(&self) -> &Request

Returns the request.

Source

pub const fn request_mut(&mut self) -> &mut Request

Returns the request mutably, e.g. to add headers before fetching.

Source

pub const fn session(&self) -> &C

Returns the session, including any live document capability.

Source

pub const fn queue(&self) -> &Queue

Returns the queue for the links this request leads to.

Source

pub fn dataset<T: Send + 'static>(&self) -> Result<Data<T>>

Returns the dataset for items of type T.

§Errors

Returns a Dataset error if no dataset is registered for T.

Source

pub async fn response(&self) -> Result<&Response>
where C: Session,

Returns the complete response, fetching the request on the first call.

Later calls return the same response without fetching again. A failed fetch is not kept: the next call tries again. Once fetched, links followed through the queue, including clones taken earlier, resolve against the URI the response was served from.

§Errors

Returns an error if the session fails to fetch the request.

Source

pub async fn body(&self) -> Result<Bytes>
where C: Session,

Returns a cheap clone of the cached response body, fetching first if needed. Browser sessions also return a fixed HTML snapshot; use the session’s document capability for live observations.

§Errors

Returns an error if the request cannot be fetched.

Trait Implementations§

Source§

impl<C> Debug for Context<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> !Freeze for Context<C>

§

impl<C> !RefUnwindSafe for Context<C>

§

impl<C> !UnwindSafe for Context<C>

§

impl<C> Send for Context<C>
where C: Send,

§

impl<C> Sync for Context<C>
where C: Sync,

§

impl<C> Unpin for Context<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Context<C>
where C: UnsafeUnpin,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<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
Source§

impl<W> WorkerExt for W

Source§

fn layer<C, M>(self, middleware: M) -> Layered<M, C>
where C: Send + 'static, Self: Worker<C>, M: Middleware<C>,

Wraps this worker in middleware, which runs before it, and before any middleware added earlier.