Skip to main content

adler_core/
executor.rs

1//! Concurrent fan-out runner for site probes.
2//!
3//! Spawns one task per site and bounds the maximum in-flight count with a
4//! [`Semaphore`]. Tasks are independent — a panic or hang in one site never
5//! blocks results from the rest. Each task self-aborts when the global
6//! deadline (if any) is reached; remaining sites surface as
7//! [`MatchKind::Uncertain`].
8
9use std::num::NonZeroUsize;
10use std::sync::Arc;
11use std::time::Duration;
12
13use tokio::sync::Semaphore;
14use tokio::task::JoinSet;
15use tokio::time::{Instant as TokioInstant, timeout_at};
16
17use crate::check::{CheckOutcome, MatchKind};
18use crate::client::Client;
19use crate::site::Site;
20use crate::username::Username;
21
22/// Default concurrency for [`run`].
23///
24/// Most sites are distinct hosts, so the per-host throttle rarely serialises;
25/// the bottleneck is network round-trips, and 32 in-flight probes keeps the
26/// pipe full without hammering any single host.
27const DEFAULT_CONCURRENCY: NonZeroUsize = match NonZeroUsize::new(32) {
28    Some(n) => n,
29    None => unreachable!(),
30};
31
32/// Tunables for [`run`].
33#[derive(Debug, Clone)]
34#[must_use = "ExecutorOptions does nothing until passed to executor::run"]
35pub struct ExecutorOptions {
36    /// Maximum number of in-flight site probes.
37    pub concurrency: NonZeroUsize,
38    /// Total wall-clock deadline for the entire scan. Sites still in flight
39    /// when this elapses produce [`MatchKind::Uncertain`] outcomes.
40    pub deadline: Option<Duration>,
41}
42
43impl Default for ExecutorOptions {
44    fn default() -> Self {
45        Self {
46            concurrency: DEFAULT_CONCURRENCY,
47            deadline: None,
48        }
49    }
50}
51
52impl ExecutorOptions {
53    /// Override [`Self::concurrency`].
54    pub fn concurrency(mut self, n: NonZeroUsize) -> Self {
55        self.concurrency = n;
56        self
57    }
58
59    /// Set a total scan deadline.
60    pub fn deadline(mut self, d: Duration) -> Self {
61        self.deadline = Some(d);
62        self
63    }
64}
65
66/// Run a fan-out scan over `sites`, returning one outcome per site.
67///
68/// Results come back in completion order (not input order) — sort by name
69/// for stable presentation. A panicking site task is logged at `error` and
70/// silently dropped; transient HTTP failures already become
71/// [`MatchKind::Uncertain`] inside `Client::check`.
72pub async fn run(
73    client: &Client,
74    sites: &[Site],
75    username: &Username,
76    options: ExecutorOptions,
77) -> Vec<CheckOutcome> {
78    run_with_progress(client, sites, username, options, |_| {}).await
79}
80
81/// Variant of [`run`] that invokes `on_outcome` for each completed probe.
82///
83/// Useful for driving a live progress indicator or for emitting streaming
84/// output before the full scan finishes. The callback runs on the executor
85/// task between completions; long work inside it will throttle the loop.
86pub async fn run_with_progress<F>(
87    client: &Client,
88    sites: &[Site],
89    username: &Username,
90    options: ExecutorOptions,
91    mut on_outcome: F,
92) -> Vec<CheckOutcome>
93where
94    F: FnMut(&CheckOutcome),
95{
96    let semaphore = Arc::new(Semaphore::new(options.concurrency.get()));
97    let deadline_at = options.deadline.map(|d| TokioInstant::now() + d);
98    let mut set: JoinSet<CheckOutcome> = JoinSet::new();
99
100    for site in sites {
101        let site = site.clone();
102        let username = username.clone();
103        let client = client.clone();
104        let permits = Arc::clone(&semaphore);
105        set.spawn(async move {
106            let permit = match permits.acquire_owned().await {
107                Ok(p) => p,
108                Err(_closed) => {
109                    return CheckOutcome {
110                        site: site.name.clone(),
111                        url: site.url_for(&username),
112                        kind: MatchKind::Uncertain,
113                        reason: Some(crate::check::UncertainReason::SchedulerClosed),
114                        elapsed_ms: 0,
115                        enrichment: std::collections::BTreeMap::new(),
116                        evidence: Vec::new(),
117                        transport: None,
118                        escalations: 0,
119                    };
120                }
121            };
122            let probe = client.check(&site, &username);
123            let outcome = match deadline_at {
124                None => probe.await,
125                Some(at) => match timeout_at(at, probe).await {
126                    Ok(o) => o,
127                    Err(_elapsed) => CheckOutcome {
128                        site: site.name.clone(),
129                        url: site.url_for(&username),
130                        kind: MatchKind::Uncertain,
131                        reason: Some(crate::check::UncertainReason::Deadline),
132                        elapsed_ms: 0,
133                        enrichment: std::collections::BTreeMap::new(),
134                        evidence: Vec::new(),
135                        transport: None,
136                        escalations: 0,
137                    },
138                },
139            };
140            drop(permit);
141            outcome
142        });
143    }
144
145    let mut results = Vec::with_capacity(sites.len());
146    while let Some(joined) = set.join_next().await {
147        match joined {
148            Ok(outcome) => {
149                on_outcome(&outcome);
150                results.push(outcome);
151            }
152            Err(err) if err.is_cancelled() => {
153                tracing::warn!(error = %err, "check task cancelled");
154            }
155            Err(err) => {
156                tracing::error!(error = %err, "check task panicked");
157            }
158        }
159    }
160    results
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166    use crate::site::Signal;
167    use crate::test_fixtures::{default_site, test_client_builder};
168    use wiremock::matchers::{any, path};
169    use wiremock::{Mock, MockServer, ResponseTemplate};
170
171    /// Test sites are uniformly defined with a Found/NotFound status pair,
172    /// matching how production sites.json migrates from Phase 1.
173    fn site(server: &MockServer, name: &str, segment: &str) -> Site {
174        let mut s = default_site(name, &format!("{}/{}/{{username}}", server.uri(), segment));
175        s.signals = vec![
176            Signal::StatusFound { codes: vec![200] },
177            Signal::StatusNotFound { codes: vec![404] },
178        ];
179        s
180    }
181
182    /// Wider timeout than the default test client — executor
183    /// concurrency tests fan out 10–30 mock calls and the 2s default
184    /// is too tight on a loaded CI runner.
185    fn fast_client() -> Client {
186        test_client_builder()
187            .timeout(Duration::from_secs(5))
188            .build()
189            .expect("fast_client builds")
190    }
191
192    fn opts_with_concurrency(n: usize) -> ExecutorOptions {
193        ExecutorOptions::default().concurrency(NonZeroUsize::new(n).unwrap())
194    }
195
196    #[tokio::test]
197    async fn runs_all_sites_concurrently() {
198        let server = MockServer::start().await;
199
200        Mock::given(any())
201            .and(path("/a/alice"))
202            .respond_with(ResponseTemplate::new(200))
203            .mount(&server)
204            .await;
205        Mock::given(any())
206            .and(path("/b/alice"))
207            .respond_with(ResponseTemplate::new(404))
208            .mount(&server)
209            .await;
210        Mock::given(any())
211            .and(path("/c/alice"))
212            .respond_with(ResponseTemplate::new(200))
213            .mount(&server)
214            .await;
215
216        let sites = vec![
217            site(&server, "A", "a"),
218            site(&server, "B", "b"),
219            site(&server, "C", "c"),
220        ];
221        let user = Username::new("alice").unwrap();
222        let mut out = run(&fast_client(), &sites, &user, opts_with_concurrency(4)).await;
223        out.sort_by(|a, b| a.site.cmp(&b.site));
224
225        assert_eq!(out.len(), 3);
226        assert_eq!(out[0].kind, MatchKind::Found);
227        assert_eq!(out[1].kind, MatchKind::NotFound);
228        assert_eq!(out[2].kind, MatchKind::Found);
229    }
230
231    #[tokio::test]
232    async fn respects_concurrency_limit() {
233        let server = MockServer::start().await;
234        for i in 0..6 {
235            Mock::given(any())
236                .and(path(format!("/{i}/alice")))
237                .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_millis(50)))
238                .mount(&server)
239                .await;
240        }
241        let sites: Vec<Site> = (0..6)
242            .map(|i| site(&server, &format!("S{i}"), &i.to_string()))
243            .collect();
244        let user = Username::new("alice").unwrap();
245        let started = std::time::Instant::now();
246        let out = run(&fast_client(), &sites, &user, opts_with_concurrency(2)).await;
247        let elapsed = started.elapsed();
248        assert_eq!(out.len(), 6);
249        // 6 sites / 2 concurrent * 50 ms = 150 ms floor.
250        assert!(
251            elapsed >= Duration::from_millis(120),
252            "expected ≥120 ms, got {elapsed:?}",
253        );
254    }
255
256    #[tokio::test]
257    async fn empty_input_returns_empty() {
258        let user = Username::new("alice").unwrap();
259        let out = run(&fast_client(), &[], &user, opts_with_concurrency(4)).await;
260        assert!(out.is_empty());
261    }
262
263    #[tokio::test]
264    async fn run_with_progress_invokes_callback_per_outcome() {
265        use std::sync::Mutex;
266        let server = MockServer::start().await;
267        Mock::given(any())
268            .and(path("/a/alice"))
269            .respond_with(ResponseTemplate::new(200))
270            .mount(&server)
271            .await;
272        Mock::given(any())
273            .and(path("/b/alice"))
274            .respond_with(ResponseTemplate::new(404))
275            .mount(&server)
276            .await;
277        let sites = vec![site(&server, "A", "a"), site(&server, "B", "b")];
278        let user = Username::new("alice").unwrap();
279        let calls = Mutex::new(0);
280        let outcomes = run_with_progress(
281            &fast_client(),
282            &sites,
283            &user,
284            opts_with_concurrency(4),
285            |_| *calls.lock().unwrap() += 1,
286        )
287        .await;
288        assert_eq!(outcomes.len(), 2);
289        assert_eq!(*calls.lock().unwrap(), 2);
290    }
291
292    #[tokio::test]
293    async fn deadline_marks_slow_sites_uncertain() {
294        let server = MockServer::start().await;
295        Mock::given(any())
296            .and(path("/slow/alice"))
297            .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(2)))
298            .mount(&server)
299            .await;
300        Mock::given(any())
301            .and(path("/fast/alice"))
302            .respond_with(ResponseTemplate::new(200))
303            .mount(&server)
304            .await;
305        let sites = vec![site(&server, "Slow", "slow"), site(&server, "Fast", "fast")];
306        let user = Username::new("alice").unwrap();
307        let options = ExecutorOptions::default()
308            .concurrency(NonZeroUsize::new(4).unwrap())
309            .deadline(Duration::from_millis(200));
310        let started = std::time::Instant::now();
311        let mut out = run(&fast_client(), &sites, &user, options).await;
312        let elapsed = started.elapsed();
313        out.sort_by(|a, b| a.site.cmp(&b.site));
314
315        assert_eq!(out.len(), 2);
316        // Fast site completed; slow one hit the deadline.
317        let fast = out.iter().find(|o| o.site == "Fast").unwrap();
318        let slow = out.iter().find(|o| o.site == "Slow").unwrap();
319        assert_eq!(fast.kind, MatchKind::Found);
320        assert_eq!(slow.kind, MatchKind::Uncertain);
321        assert_eq!(slow.reason, Some(crate::check::UncertainReason::Deadline));
322        assert!(
323            elapsed < Duration::from_millis(800),
324            "scan should abort near the deadline, got {elapsed:?}",
325        );
326    }
327}