spire_core/frontier/mod.rs
1//! The frontier: requests waiting to be crawled.
2//!
3//! A [`Frontier`] stores [`Seed`]s and decides which one is crawled next.
4//! [`InMemFrontier`](memory::InMemFrontier) provides the storage; facades wrap any frontier to add
5//! behavior and are chained with [`FrontierExt`]:
6//!
7//! ```
8//! use spire_core::frontier::FrontierExt;
9//! use spire_core::frontier::memory::InMemFrontier;
10//!
11//! let frontier = InMemFrontier::fifo()
12//! .dedup()
13//! .max_depth(5)
14//! .retry(3)
15//! .tag_backoff();
16//! ```
17//!
18//! Pushes flow from the outermost facade inwards, so above, a seed that is
19//! too deep is refused before it is marked as seen.
20//!
21//! - [`Filter`] refuses seeds outside the crawl's scope, e.g. other hosts.
22//! - [`Dedup`] refuses seeds whose [`Fingerprint`](seed::Fingerprint) was seen before.
23//! - [`MaxDepth`] refuses seeds deeper than a limit.
24//! - [`Retry`] re-queues seeds after [`Report::Retry`], with a delay.
25//! - [`TagBackoff`] holds back a tag's seeds after [`Report::Backoff`].
26//!
27//! Without a [`Retry`] facade, retries are refused and the seeds fail;
28//! without a [`TagBackoff`] facade, backoffs only delay the retried request.
29
30pub(crate) mod dynamic;
31pub mod facade;
32pub mod memory;
33pub mod queue;
34pub mod seed;
35pub mod seen;
36#[cfg(test)]
37pub(crate) mod test_util;
38
39use std::future::{Future, ready};
40use std::time::Duration;
41
42use tokio::time::Instant;
43
44use self::facade::{AllowedHosts, Dedup, Filter, MaxDepth, Retry, RetryPolicy, TagBackoff};
45use self::seed::Seed;
46use self::seen::{InMemSeen, SeenSet};
47use crate::Result;
48use crate::request::Tag;
49
50/// Stores [`Seed`]s and decides which one is crawled next.
51pub trait Frontier: Send + Sync + 'static {
52 /// Offers a seed, which the frontier may refuse.
53 ///
54 /// # Errors
55 ///
56 /// Returns an error if the underlying storage fails.
57 fn push(&self, seed: Seed) -> impl Future<Output = Result<Admission>> + Send;
58
59 /// Takes the next seed that is ready to be crawled.
60 ///
61 /// # Errors
62 ///
63 /// Returns an error if the underlying storage fails.
64 fn pop(&self) -> impl Future<Output = Result<Next>> + Send;
65
66 /// Tells the frontier how a popped seed ended.
67 ///
68 /// Facades react to the reports they handle and pass the rest inwards.
69 /// The default implementation ignores the report, refusing retries.
70 ///
71 /// # Errors
72 ///
73 /// Returns an error if the underlying storage fails.
74 fn report(&self, report: Report) -> impl Future<Output = Result<Ack>> + Send {
75 let ack = match report {
76 Report::Retry { .. } => Ack::RetryRefused,
77 _ => Ack::Applied,
78 };
79 ready(Ok(ack))
80 }
81
82 /// Returns the number of queued seeds, if known.
83 fn size_hint(&self) -> Option<usize> {
84 None
85 }
86
87 /// Returns a copy of every seed held, in no particular order, to save
88 /// the crawl and resume it later; `None` if the frontier cannot, e.g.
89 /// because it persists seeds itself.
90 ///
91 /// Facades add the seeds they hold, such as seeds waiting for a retry.
92 fn snapshot(&self) -> Option<Vec<Seed>> {
93 None
94 }
95}
96
97impl<F: Frontier> Frontier for std::sync::Arc<F> {
98 fn push(&self, seed: Seed) -> impl Future<Output = Result<Admission>> + Send {
99 F::push(self, seed)
100 }
101
102 fn pop(&self) -> impl Future<Output = Result<Next>> + Send {
103 F::pop(self)
104 }
105
106 fn report(&self, report: Report) -> impl Future<Output = Result<Ack>> + Send {
107 F::report(self, report)
108 }
109
110 fn size_hint(&self) -> Option<usize> {
111 F::size_hint(self)
112 }
113
114 fn snapshot(&self) -> Option<Vec<Seed>> {
115 F::snapshot(self)
116 }
117}
118
119/// Whether [`Frontier::push`] queued a seed.
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
121#[non_exhaustive]
122pub enum Admission {
123 /// The seed was queued.
124 Queued,
125 /// The seed was refused because an identical one was seen before.
126 Duplicate,
127 /// The seed was refused because it is deeper than allowed.
128 TooDeep,
129 /// The link was skipped because it is not an `http(s)` URL.
130 Unsupported,
131 /// The seed was refused because it is outside the crawl's scope.
132 OutOfScope,
133}
134
135impl Admission {
136 /// Whether the seed was queued.
137 #[must_use]
138 pub const fn is_queued(self) -> bool {
139 matches!(self, Self::Queued)
140 }
141}
142
143/// The result of [`Frontier::pop`].
144#[derive(Debug)]
145pub enum Next {
146 /// A seed is ready to be crawled.
147 Ready(Seed),
148 /// No seed is ready now, but one will be at the given instant.
149 Wait(Instant),
150 /// No seed is queued.
151 Empty,
152}
153
154/// How a popped seed ended, see [`Frontier::report`].
155#[derive(Debug)]
156#[non_exhaustive]
157pub enum Report {
158 /// The seed was crawled.
159 Done(Seed),
160 /// The seed failed and will not be tried again.
161 Failed(Seed),
162 /// The seed was not crawled on purpose, e.g. robots.txt disallows it or
163 /// its tag was aborted.
164 Skipped(Seed),
165 /// The crawl stopped before the seed finished; a persistent frontier
166 /// should keep it for the next run.
167 Cancelled(Seed),
168 /// The seed should be tried again.
169 Retry {
170 /// The seed that failed.
171 seed: Seed,
172 /// Delay before the next attempt; `None` defers to the frontier.
173 after: Option<Duration>,
174 },
175 /// Seeds with `tag` should not be crawled before `until`.
176 Backoff {
177 /// The tag to pause; `None` pauses untagged seeds.
178 tag: Option<Tag>,
179 /// The earliest instant at which the tag may be crawled again.
180 until: Instant,
181 },
182}
183
184/// What a frontier did with a [`Report`].
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
186#[non_exhaustive]
187pub enum Ack {
188 /// The report was applied.
189 Applied,
190 /// A [`Report::Retry`] was refused, because the seed is out of retries or
191 /// the frontier cannot retry; the seed has failed.
192 RetryRefused,
193}
194
195/// Wraps a [`Frontier`] in another, see [`FrontierExt::layer`].
196pub trait FrontierLayer<F> {
197 /// The wrapping frontier.
198 type Frontier: Frontier;
199
200 /// Wraps `inner`.
201 fn layer(self, inner: F) -> Self::Frontier;
202}
203
204/// Chains facades onto a [`Frontier`].
205pub trait FrontierExt: Frontier + Sized {
206 /// Refuses seeds seen before, remembering them in memory.
207 fn dedup(self) -> Dedup<Self> {
208 Dedup::new(self, InMemSeen::default())
209 }
210
211 /// Refuses seeds seen before, remembering them in `seen`.
212 fn dedup_with<S: SeenSet>(self, seen: S) -> Dedup<Self, S> {
213 Dedup::new(self, seen)
214 }
215
216 /// Refuses seeds `predicate` rejects.
217 fn filter<P>(self, predicate: P) -> Filter<Self, P>
218 where
219 P: Fn(&Seed) -> bool + Send + Sync + 'static,
220 {
221 Filter::new(self, predicate)
222 }
223
224 /// Refuses seeds outside `hosts` and their subdomains, e.g. to keep a
225 /// crawl from following links off-site.
226 fn allow_hosts<I>(
227 self,
228 hosts: I,
229 ) -> Filter<Self, impl Fn(&Seed) -> bool + Send + Sync + 'static>
230 where
231 I: IntoIterator<Item: AsRef<str>>,
232 {
233 let hosts = AllowedHosts::new(hosts);
234 Filter::new(self, move |seed: &Seed| hosts.allows(seed))
235 }
236
237 /// Refuses seeds deeper than `depth`.
238 fn max_depth(self, depth: u32) -> MaxDepth<Self> {
239 MaxDepth::new(self, depth)
240 }
241
242 /// Retries failed seeds up to `max_retries` times with exponential
243 /// backoff.
244 fn retry(self, max_retries: u32) -> Retry<Self> {
245 Retry::new(self, RetryPolicy::new(max_retries))
246 }
247
248 /// Retries failed seeds according to `policy`.
249 fn retry_with(self, policy: RetryPolicy) -> Retry<Self> {
250 Retry::new(self, policy)
251 }
252
253 /// Holds back seeds of paused tags.
254 fn tag_backoff(self) -> TagBackoff<Self> {
255 TagBackoff::new(self)
256 }
257
258 /// Wraps the frontier with a custom facade.
259 fn layer<L: FrontierLayer<Self>>(self, layer: L) -> L::Frontier {
260 layer.layer(self)
261 }
262}
263
264impl<F: Frontier> FrontierExt for F {}