stygian-graph 0.9.2

High-performance graph-based web scraping engine with AI extraction, multi-modal support, and anti-bot capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Request-count rate limiter for GraphQL API targets with pluggable algorithms.
//!
//! Two strategies are supported via [`RateLimitStrategy`]:
//!
//! - **[`SlidingWindow`](RateLimitStrategy::SlidingWindow)** — limits outgoing requests to
//!   `max_requests` in any rolling `window` duration.  Before each request
//!   [`rate_limit_acquire`] is called; it records the current timestamp and sleeps
//!   until the oldest in-window request expires if the window is already full.
//!
//! - **[`TokenBucket`](RateLimitStrategy::TokenBucket)** — refills tokens at a steady rate
//!   (`max_requests / window`); short bursts are absorbed by the bucket capacity before
//!   the rate is enforced.  Computes the exact wait time required to accumulate the next
//!   token instead of sleeping speculatively.
//!
//! A server-returned `Retry-After` value can be applied via
//! [`rate_limit_retry_after`], which imposes a hard block until the indicated
//! instant irrespective of the active algorithm.
//!
//! Operates in parallel with [`graphql_throttle`](crate::adapters::graphql_throttle)
//! (leaky-bucket cost throttle).  Both can be active simultaneously.

use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::Mutex;

/// Re-export — canonical definition lives in the ports layer.
pub use crate::ports::graphql_plugin::RateLimitConfig;
pub use crate::ports::graphql_plugin::RateLimitStrategy;

// ─────────────────────────────────────────────────────────────────────────────
// WindowState — per-strategy mutable inner state
// ─────────────────────────────────────────────────────────────────────────────

/// Per-strategy mutable state held inside [`RequestWindow`].
#[derive(Debug)]
enum WindowState {
    /// A rolling `VecDeque` of request timestamps for the sliding-window algorithm.
    Sliding { timestamps: VecDeque<Instant> },
    /// Token count and last-refill timestamp for the token-bucket algorithm.
    TokenBucket { tokens: f64, last_refill: Instant },
}

// ─────────────────────────────────────────────────────────────────────────────
// RequestWindow
// ─────────────────────────────────────────────────────────────────────────────

/// Mutable inner state — algorithm-specific window data plus an optional hard
/// block set by a server-returned `Retry-After` header.
#[derive(Debug)]
struct RequestWindow {
    state: WindowState,
    config: RateLimitConfig,
    /// Hard block until this instant, set by [`record_retry_after`].
    blocked_until: Option<Instant>,
}

impl RequestWindow {
    fn new(config: &RateLimitConfig) -> Self {
        let state = match config.strategy {
            RateLimitStrategy::SlidingWindow => WindowState::Sliding {
                timestamps: VecDeque::with_capacity(config.max_requests as usize),
            },
            RateLimitStrategy::TokenBucket => WindowState::TokenBucket {
                tokens: f64::from(config.max_requests),
                last_refill: Instant::now(),
            },
        };
        Self {
            state,
            config: config.clone(),
            blocked_until: None,
        }
    }

    /// Try to acquire a request slot.
    ///
    /// - Returns `None` if a slot is available (the slot is claimed immediately).
    /// - Returns `Some(wait)` with the duration the caller must sleep before
    ///   retrying (capped at `config.max_delay_ms`).
    fn acquire(&mut self) -> Option<Duration> {
        let now = Instant::now();
        let max_delay = Duration::from_millis(self.config.max_delay_ms);

        // Check hard block imposed by a previous Retry-After response.
        if let Some(until) = self.blocked_until {
            if until > now {
                let wait = until.duration_since(now);
                return Some(wait.min(max_delay));
            }
            self.blocked_until = None;
        }

        match &mut self.state {
            WindowState::Sliding { timestamps } => {
                let window = self.config.window;
                // Drop timestamps that have rolled out of the window.
                while timestamps
                    .front()
                    .is_some_and(|t| now.duration_since(*t) >= window)
                {
                    timestamps.pop_front();
                }

                if timestamps.len() < self.config.max_requests as usize {
                    // Slot available — record and permit.
                    timestamps.push_back(now);
                    None
                } else {
                    // Window is full; compute wait until the oldest entry rolls out.
                    let &oldest = timestamps.front()?;
                    let elapsed = now.duration_since(oldest);
                    let wait = window.saturating_sub(elapsed);
                    Some(wait.min(max_delay))
                }
            }

            WindowState::TokenBucket {
                tokens,
                last_refill,
            } => {
                // Guard: zero max_requests or zero window would produce rate=0 → inf wait.
                if self.config.max_requests == 0 || self.config.window.is_zero() {
                    return Some(max_delay);
                }

                // Refill tokens proportional to elapsed time.
                let elapsed = now.duration_since(*last_refill);
                let rate = f64::from(self.config.max_requests) / self.config.window.as_secs_f64();
                let refill = elapsed.as_secs_f64() * rate;
                *tokens = (*tokens + refill).min(f64::from(self.config.max_requests));
                *last_refill = now;

                if *tokens >= 1.0 {
                    *tokens -= 1.0;
                    None
                } else {
                    // Compute exact wait until 1 token has accumulated.
                    let wait_secs = (1.0 - *tokens) / rate;
                    let wait = Duration::from_secs_f64(wait_secs);
                    Some(wait.min(max_delay))
                }
            }
        }
    }

    /// Set a hard block for `secs` seconds to honour a server-returned
    /// `Retry-After` interval.
    ///
    /// A shorter `secs` value will never override a longer existing block.
    fn record_retry_after(&mut self, secs: u64) {
        let until = Instant::now() + Duration::from_secs(secs);
        match self.blocked_until {
            // Keep the later of the two blocks.
            Some(existing) if existing >= until => {}
            _ => self.blocked_until = Some(until),
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// RequestRateLimit
// ─────────────────────────────────────────────────────────────────────────────

/// Shareable, cheaply-cloneable handle to a per-plugin sliding-window limiter.
///
/// # Example
///
/// ```rust
/// use stygian_graph::adapters::graphql_rate_limit::{
///     RateLimitConfig, RequestRateLimit, rate_limit_acquire,
/// };
///
/// # async fn example() {
/// let rl = RequestRateLimit::new(RateLimitConfig::default());
/// rate_limit_acquire(&rl).await;
/// // … send the request …
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct RequestRateLimit {
    inner: Arc<Mutex<RequestWindow>>,
    config: RateLimitConfig,
}

impl RequestRateLimit {
    /// Create a new `RequestRateLimit` from a [`RateLimitConfig`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use stygian_graph::adapters::graphql_rate_limit::{RateLimitConfig, RequestRateLimit};
    ///
    /// let rl = RequestRateLimit::new(RateLimitConfig::default());
    /// ```
    #[must_use]
    pub fn new(config: RateLimitConfig) -> Self {
        let window = RequestWindow::new(&config);
        Self {
            inner: Arc::new(Mutex::new(window)),
            config,
        }
    }

    /// Return the [`RateLimitConfig`] this limiter was initialised from.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::time::Duration;
    /// use stygian_graph::adapters::graphql_rate_limit::{RateLimitConfig, RequestRateLimit};
    ///
    /// let cfg = RateLimitConfig { max_requests: 50, ..Default::default() };
    /// let rl = RequestRateLimit::new(cfg.clone());
    /// assert_eq!(rl.config().max_requests, 50);
    /// ```
    #[must_use]
    pub const fn config(&self) -> &RateLimitConfig {
        &self.config
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Public API
// ─────────────────────────────────────────────────────────────────────────────

/// Sleep until a request slot is available within the rolling window, then
/// record the slot.
///
/// If the window is full the function sleeps until the oldest in-window entry
/// expires (capped at `config.max_delay_ms`).  The `Mutex` guard is dropped
/// before every `.await` call to preserve `Send` bounds.
///
/// # Example
///
/// ```rust
/// use stygian_graph::adapters::graphql_rate_limit::{
///     RateLimitConfig, RequestRateLimit, rate_limit_acquire,
/// };
/// use std::time::Duration;
///
/// # async fn example() {
/// let rl = RequestRateLimit::new(RateLimitConfig {
///     max_requests: 2,
///     ..Default::default()
/// });
/// rate_limit_acquire(&rl).await;
/// rate_limit_acquire(&rl).await;
/// // Third call blocks until the window rolls forward.
/// # }
/// ```
pub async fn rate_limit_acquire(rl: &RequestRateLimit) {
    loop {
        let delay = {
            let mut guard = rl.inner.lock().await;
            guard.acquire()
        };
        match delay {
            None => return,
            Some(d) => {
                tracing::debug!(
                    delay_ms = d.as_millis(),
                    "rate limiter: window full, sleeping"
                );
                tokio::time::sleep(d).await;
            }
        }
    }
}

/// Record a server-returned `Retry-After` delay.
///
/// Call this when the upstream API responds with HTTP 429 and a `Retry-After`
/// header.  Subsequent calls to [`rate_limit_acquire`] will block for at least
/// `secs` seconds.  A shorter `secs` value will never shorten an existing block.
///
/// # Example
///
/// ```rust
/// use stygian_graph::adapters::graphql_rate_limit::{
///     RateLimitConfig, RequestRateLimit, rate_limit_retry_after,
/// };
///
/// # async fn example() {
/// let rl = RequestRateLimit::new(RateLimitConfig::default());
/// rate_limit_retry_after(&rl, 30).await;
/// # }
/// ```
pub async fn rate_limit_retry_after(rl: &RequestRateLimit, retry_after_secs: u64) {
    let mut guard = rl.inner.lock().await;
    guard.record_retry_after(retry_after_secs);
}

/// Parse an integer `Retry-After` value from a header string.
///
/// Returns `None` if the value cannot be parsed as a non-negative integer.
/// HTTP-date format is intentionally not supported.
///
/// # Example
///
/// ```rust
/// use stygian_graph::adapters::graphql_rate_limit::parse_retry_after;
///
/// assert_eq!(parse_retry_after("30"), Some(30));
/// assert_eq!(parse_retry_after("not-a-number"), None);
/// ```
#[must_use]
pub fn parse_retry_after(value: &str) -> Option<u64> {
    value.trim().parse::<u64>().ok()
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    fn cfg(max_requests: u32, window_secs: u64) -> RateLimitConfig {
        RateLimitConfig {
            max_requests,
            window: Duration::from_secs(window_secs),
            max_delay_ms: 60_000,
            strategy: RateLimitStrategy::SlidingWindow,
        }
    }

    fn cfg_bucket(max_requests: u32, window_secs: u64) -> RateLimitConfig {
        RateLimitConfig {
            max_requests,
            window: Duration::from_secs(window_secs),
            max_delay_ms: 60_000,
            strategy: RateLimitStrategy::TokenBucket,
        }
    }

    // 1. Window allows exactly max_requests without blocking.
    #[test]
    fn window_allows_up_to_max() {
        let mut w = RequestWindow::new(&cfg(3, 60));
        assert!(w.acquire().is_none(), "slot 1");
        assert!(w.acquire().is_none(), "slot 2");
        assert!(w.acquire().is_none(), "slot 3");
        assert!(w.acquire().is_some(), "4th request must be blocked");
    }

    // 2. After the window expires the slot becomes available again.
    #[test]
    fn window_resets_after_expiry() {
        let mut w = RequestWindow::new(&RateLimitConfig {
            max_requests: 1,
            window: Duration::from_millis(10),
            max_delay_ms: 60_000,
            strategy: RateLimitStrategy::SlidingWindow,
        });
        assert!(w.acquire().is_none(), "first request");
        std::thread::sleep(Duration::from_millis(25));
        assert!(w.acquire().is_none(), "window should have expired");
    }

    // 3. Timestamps are recorded immediately so concurrent callers see a
    //    reduced slot count without waiting for the request to complete.
    #[test]
    fn timestamps_recorded_immediately() {
        let mut w = RequestWindow::new(&cfg(2, 60));
        w.acquire();
        w.acquire();
        // After two acquisitions the window is full.
        assert!(w.acquire().is_some(), "third request must be blocked");
    }

    // 4. record_retry_after blocks subsequent acquire calls.
    #[test]
    fn retry_after_blocks_further_requests() {
        let mut w = RequestWindow::new(&cfg(100, 60));
        w.record_retry_after(30);
        assert!(
            w.acquire().is_some(),
            "Retry-After must block the next request"
        );
    }

    // 5. A shorter Retry-After must not reduce an existing longer block.
    #[test]
    fn retry_after_does_not_shorten_existing_block() {
        let mut w = RequestWindow::new(&cfg(100, 60));
        w.record_retry_after(60);
        let until_before = w.blocked_until.unwrap();
        w.record_retry_after(1);
        let until_after = w.blocked_until.unwrap();
        assert!(
            until_after >= until_before,
            "shorter retry-after must not override the longer block"
        );
    }

    // 6. parse_retry_after handles valid integers and rejects garbage.
    #[test]
    fn parse_retry_after_parses_integers() {
        assert_eq!(parse_retry_after("42"), Some(42));
        assert_eq!(parse_retry_after("0"), Some(0));
        assert_eq!(parse_retry_after("not-a-number"), None);
        assert_eq!(parse_retry_after(""), None);
        assert_eq!(parse_retry_after("  30  "), Some(30));
    }

    // ── Token-bucket strategy tests ──────────────────────────────────────────

    // 7. Token bucket allows up to max_requests immediately (full bucket).
    #[test]
    fn token_bucket_allows_up_to_max() {
        let mut w = RequestWindow::new(&cfg_bucket(3, 60));
        assert!(w.acquire().is_none(), "token 1");
        assert!(w.acquire().is_none(), "token 2");
        assert!(w.acquire().is_none(), "token 3");
        assert!(
            w.acquire().is_some(),
            "4th request must be blocked — bucket empty"
        );
    }

    // 8. Token bucket refills over time.
    #[test]
    fn token_bucket_refills_after_delay() {
        let mut w = RequestWindow::new(&RateLimitConfig {
            // 1 token per 10 ms
            max_requests: 1,
            window: Duration::from_millis(10),
            max_delay_ms: 60_000,
            strategy: RateLimitStrategy::TokenBucket,
        });
        assert!(w.acquire().is_none(), "first request consumes the token");
        assert!(w.acquire().is_some(), "bucket empty — must block");
        std::thread::sleep(Duration::from_millis(20));
        assert!(w.acquire().is_none(), "bucket should have refilled");
    }

    // 9. Token bucket also respects Retry-After hard blocks.
    #[test]
    fn token_bucket_respects_retry_after() {
        let mut w = RequestWindow::new(&cfg_bucket(100, 60));
        w.record_retry_after(30);
        assert!(
            w.acquire().is_some(),
            "Retry-After must block even with tokens available"
        );
    }

    // 10. Token bucket wait duration is proportional to the deficit.
    #[test]
    fn token_bucket_wait_is_proportional() {
        // 60 requests / 60 s = 1 token/s.  After draining the bucket the wait
        // for a single token should be ≈ 1 s.
        let mut w = RequestWindow::new(&RateLimitConfig {
            max_requests: 1,
            window: Duration::from_secs(1),
            max_delay_ms: 60_000,
            strategy: RateLimitStrategy::TokenBucket,
        });
        w.acquire(); // consume the only token
        let wait = w.acquire().unwrap();
        // Wait should be close to 1 s; allow generous tolerance for slow CI.
        assert!(
            wait <= Duration::from_secs(1),
            "wait {wait:?} should not exceed 1 s"
        );
        assert!(
            wait >= Duration::from_millis(800),
            "wait {wait:?} should be close to 1 s"
        );
    }
}