use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use futures::future::BoxFuture;
use crate::changes::{Change, Publish};
use crate::problem::Problem;
pub type Link = (String, Option<usize>, String);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
Fine,
Broken(String),
Unreachable(String),
}
pub trait Ask: Send + Sync {
fn ask<'a>(&'a self, address: &'a str, wait: Duration) -> BoxFuture<'a, Outcome>;
}
pub trait Background: Send + Sync {
fn spawn(&self, task: BoxFuture<'static, ()>) -> bool;
fn sleep(&self, pause: Duration) -> BoxFuture<'static, ()>;
}
#[derive(Debug, Clone, Copy)]
pub struct Waits {
pub first: Duration,
pub retry: Duration,
pub retry_after: Duration,
}
impl Default for Waits {
fn default() -> Self {
Waits {
first: Duration::from_secs(10),
retry: Duration::from_secs(30),
retry_after: Duration::from_secs(30),
}
}
}
const FAILURES_BEFORE_A_PROBLEM: u32 = 3;
enum Answer {
Pending,
Fine,
Broken(String),
Unreachable(u32, String),
}
pub struct External {
ask: Arc<dyn Ask>,
background: Arc<dyn Background>,
waits: Waits,
answers: Mutex<HashMap<String, Answer>>,
changes: Option<Arc<dyn Publish>>,
}
impl External {
pub fn new(
ask: Arc<dyn Ask>,
background: Arc<dyn Background>,
waits: Waits,
changes: Option<Arc<dyn Publish>>,
) -> Arc<Self> {
Arc::new(External {
ask,
background,
waits,
answers: Mutex::default(),
changes,
})
}
pub async fn check(&self, links: Vec<Link>) -> Vec<Problem> {
let mut by_address: HashMap<String, Vec<(String, Option<usize>)>> = HashMap::new();
for (file, line, address) in links {
by_address.entry(address).or_default().push((file, line));
}
let checks = by_address.into_iter().map(|(address, links)| async move {
let Some(why) = self.broken(&address).await else {
return Vec::new();
};
links
.into_iter()
.map(|(file, line)| Problem {
file,
line,
column: None,
message: format!("{address} — {why}"),
target: Some(address.clone()),
})
.collect()
});
futures::future::join_all(checks)
.await
.into_iter()
.flatten()
.collect()
}
async fn broken(&self, address: &str) -> Option<String> {
match self.ask.ask(address, self.waits.first).await {
Outcome::Fine => None,
Outcome::Broken(why) => Some(why),
Outcome::Unreachable(_) => match self.ask.ask(address, self.waits.retry).await {
Outcome::Fine => None,
Outcome::Broken(why) | Outcome::Unreachable(why) => Some(why),
},
}
}
pub fn preview(self: &Arc<Self>, links: Vec<Link>) -> Vec<Problem> {
let mut problems = Vec::new();
let mut answers = self
.answers
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
for (file, line, address) in links {
match answers.get(&address) {
Some(Answer::Broken(why)) => problems.push(Problem {
file,
line,
column: None,
message: format!("{address} — {why}"),
target: Some(address),
}),
Some(Answer::Unreachable(failures, why))
if *failures >= FAILURES_BEFORE_A_PROBLEM =>
{
problems.push(Problem {
file,
line,
column: None,
message: format!(
"{address} — {why}, {FAILURES_BEFORE_A_PROBLEM} times in a row"
),
target: Some(address),
})
}
Some(Answer::Pending | Answer::Fine | Answer::Unreachable(..)) => {}
None => {
if self
.background
.spawn(Box::pin(Arc::clone(self).watch(address.clone())))
{
answers.insert(address, Answer::Pending);
}
}
}
}
problems
}
async fn watch(self: Arc<Self>, address: String) {
let mut failures = 0;
loop {
let outcome = self.ask.ask(&address, self.waits.first).await;
let shown_before = failures >= FAILURES_BEFORE_A_PROBLEM;
let (answer, shown_now, done) = match outcome {
Outcome::Fine => (Answer::Fine, false, true),
Outcome::Broken(why) => (Answer::Broken(why), true, true),
Outcome::Unreachable(why) => {
failures += 1;
let shown = failures >= FAILURES_BEFORE_A_PROBLEM;
(Answer::Unreachable(failures, why), shown, false)
}
};
self.answers
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.insert(address.clone(), answer);
if shown_now != shown_before
&& let Some(changes) = &self.changes
{
changes.publish(Change::Refresh);
}
if done {
return;
}
let pause = self.waits.retry_after * (1 << (failures - 1).min(2));
self.background.sleep(pause).await;
}
}
#[cfg(any(test, feature = "testing"))]
pub fn failures(&self, address: &str) -> u32 {
match self.answers.lock().unwrap().get(address) {
Some(Answer::Unreachable(failures, _)) => *failures,
_ => 0,
}
}
}
#[cfg(any(test, feature = "testing"))]
pub mod testing {
use super::*;
struct Fine;
impl Ask for Fine {
fn ask<'a>(&'a self, _: &'a str, _: Duration) -> BoxFuture<'a, Outcome> {
Box::pin(async { Outcome::Fine })
}
}
struct Nowhere;
impl Background for Nowhere {
fn spawn(&self, _: BoxFuture<'static, ()>) -> bool {
false
}
fn sleep(&self, _: Duration) -> BoxFuture<'static, ()> {
Box::pin(async {})
}
}
pub fn offline() -> Arc<External> {
External::new(Arc::new(Fine), Arc::new(Nowhere), Waits::default(), None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct Table {
script: HashMap<String, Vec<Outcome>>,
asked: Mutex<HashMap<String, usize>>,
}
impl Table {
fn with(mut self, address: &str, outcomes: &[Outcome]) -> Self {
self.script.insert(address.into(), outcomes.to_vec());
self
}
fn asked(&self, address: &str) -> usize {
self.asked
.lock()
.unwrap()
.get(address)
.copied()
.unwrap_or(0)
}
}
impl Ask for Table {
fn ask<'a>(&'a self, address: &'a str, _wait: Duration) -> BoxFuture<'a, Outcome> {
let mut asked = self.asked.lock().unwrap();
let n = asked.entry(address.into()).or_default();
let script = &self.script[address];
let outcome = script[(*n).min(script.len() - 1)].clone();
*n += 1;
Box::pin(async move { outcome })
}
}
fn quick() -> Waits {
Waits {
first: Duration::from_millis(1),
retry: Duration::from_millis(1),
retry_after: Duration::from_millis(1),
}
}
fn link(address: &str) -> Link {
("README.md".into(), Some(3), address.into())
}
struct Tokio;
impl Background for Tokio {
fn spawn(&self, task: BoxFuture<'static, ()>) -> bool {
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
return false;
};
runtime.spawn(task);
true
}
fn sleep(&self, pause: Duration) -> BoxFuture<'static, ()> {
Box::pin(tokio::time::sleep(pause))
}
}
struct Heard(tokio::sync::mpsc::UnboundedSender<Change>);
impl Publish for Heard {
fn publish(&self, change: Change) {
let _ = self.0.send(change);
}
}
fn down() -> Outcome {
Outcome::Unreachable("no connection".into())
}
#[tokio::test]
async fn check_asks_an_unreachable_address_once_more_and_a_broken_one_once() {
let table = Arc::new(
Table::default()
.with("slow", &[down(), Outcome::Fine])
.with("drop", &[down()])
.with("gone", &[Outcome::Broken("404 Not Found".into())]),
);
let external = External::new(table.clone(), Arc::new(Tokio), quick(), None);
let found: Vec<String> = external
.check(vec![link("slow"), link("drop"), link("gone"), link("gone")])
.await
.iter()
.map(ToString::to_string)
.collect();
let mut found = found;
found.sort();
assert_eq!(
found,
vec![
"README.md:3: drop — no connection",
"README.md:3: gone — 404 Not Found",
"README.md:3: gone — 404 Not Found",
]
);
assert_eq!(
(
table.asked("slow"),
table.asked("drop"),
table.asked("gone")
),
(2, 2, 1)
);
}
#[tokio::test]
async fn the_preview_shows_an_unreachable_address_only_after_three_failures() {
let table = Arc::new(Table::default().with("flaky", &[down()]));
let (changes, mut heard) = tokio::sync::mpsc::unbounded_channel();
let external = External::new(
table,
Arc::new(Tokio),
quick(),
Some(Arc::new(Heard(changes))),
);
assert!(external.preview(vec![link("flaky")]).is_empty());
assert_eq!(heard.recv().await, Some(Change::Refresh));
assert!(external.failures("flaky") >= FAILURES_BEFORE_A_PROBLEM);
assert_eq!(
external
.preview(vec![link("flaky")])
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>(),
vec!["README.md:3: flaky — no connection, 3 times in a row"]
);
}
#[test]
fn outside_a_runtime_the_preview_asks_nothing() {
let table = Arc::new(Table::default().with("x", &[Outcome::Fine]));
let external = External::new(table.clone(), Arc::new(Tokio), quick(), None);
assert!(external.preview(vec![link("x")]).is_empty());
assert_eq!(table.asked("x"), 0);
}
}