mocra 0.3.0

A distributed, event-driven crawling and data collection framework
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
#![allow(unused)]
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Rate limiter configuration.
#[derive(Debug, Clone)]
pub struct RateLimitConfig {
    /// Maximum requests per second.
    pub max_requests_per_second: f32,
    /// Window size (default: 1 second).
    pub window_size: Duration,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            max_requests_per_second: 2.0,
            window_size: Duration::from_secs(1),
        }
    }
}

impl RateLimitConfig {
    pub fn new(max_requests_per_second: f32) -> Self {
        Self {
            max_requests_per_second,
            window_size: Duration::from_secs(1),
        }
    }

    pub fn with_window_size(mut self, window_size: Duration) -> Self {
        self.window_size = window_size;
        self
    }
}

/// Sliding-window rate limiter.
///
/// Uses a sliding-window strategy to throttle requests per identifier.
/// Optimizations:
/// 1. Uses read/write locks to reduce contention and allow concurrent reads.
/// 2. Stores timestamps directly to reduce memory overhead.
/// 3. Optimized cleanup logic to avoid unnecessary scans.
/// 4. Supports per-key rate limits.
/// 5. Supports dynamic key-level config updates.
#[derive(Debug)]
pub struct SlidingWindowRateLimiter {
    /// Default rate-limit config (used for keys without explicit config).
    default_config: Arc<RwLock<RateLimitConfig>>,
    /// Per-key custom configuration.
    key_configs: Arc<RwLock<HashMap<String, RateLimitConfig>>>,
    /// Timestamp lists per identifier.
    records: Arc<RwLock<HashMap<String, Vec<Instant>>>>,
}
impl Default for SlidingWindowRateLimiter {
    fn default() -> Self {
        Self::new(RateLimitConfig::default())
    }
}

impl SlidingWindowRateLimiter {
    /// Creates a new rate limiter instance.
    ///
    /// # Parameters
    /// * `default_config` - Default rate-limit configuration.
    pub fn new(default_config: RateLimitConfig) -> Self {
        Self {
            default_config: Arc::new(RwLock::new(default_config)),
            key_configs: Arc::new(RwLock::new(HashMap::new())),
            records: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Creates a simple limiter.
    ///
    /// # Parameters
    /// * `max_requests_per_second` - Maximum requests per second (supports decimals).
    pub fn simple(max_requests_per_second: f32) -> Self {
        Self::new(RateLimitConfig::new(max_requests_per_second))
    }

    /// Sets rate-limit config for a specific key.
    ///
    /// # Parameters
    /// * `key` - Target key.
    /// * `config` - Rate-limit configuration.
    pub async fn set_key_config(&self, key: String, config: RateLimitConfig) {
        let mut configs = self.key_configs.write().await;
        configs.insert(key, config);
    }
    /// Sets global limit (affects all keys).
    pub async fn set_limit(&self, limit: f32) {
        self.default_config.write().await.max_requests_per_second = limit;
        let mut config = self.key_configs.write().await;
        config.iter_mut().for_each(|(_, config)| {
            config.max_requests_per_second = limit;
        })
    }

    /// Sets multiple key configs in batch.
    ///
    /// # Parameters
    /// * `configs` - Mapping from key to config.
    pub async fn set_key_configs(&self, configs: HashMap<String, RateLimitConfig>) {
        let mut key_configs = self.key_configs.write().await;
        for (key, config) in configs {
            key_configs.insert(key, config);
        }
    }

    /// Removes config for a key (falls back to default config).
    ///
    /// # Parameters
    /// * `key` - Key whose config should be removed.
    pub async fn remove_key_config(&self, key: &str) {
        let mut configs = self.key_configs.write().await;
        configs.remove(key);
    }

    /// Gets config for a specific key.
    ///
    /// # Parameters
    /// * `key` - Key to query.
    ///
    /// # Returns
    /// Key-specific config, or default config if none is set.
    pub async fn get_key_config(&self, key: &str) -> RateLimitConfig {
        let configs = self.key_configs.read().await;
        if let Some(config) = configs.get(key) {
            config.clone()
        } else {
            self.default_config.read().await.clone()
        }
    }

    /// Returns all configured keys and their configs.
    pub async fn get_all_key_configs(&self) -> HashMap<String, RateLimitConfig> {
        let configs = self.key_configs.read().await;
        configs.clone()
    }

    /// Records a request.
    ///
    /// # Parameters
    /// * `identifier` - Identifier used to distinguish request sources.
    pub async fn record(&self, identifier: String) {
        let config = self.get_key_config(&identifier).await;
        let mut records = self.records.write().await;
        let entry = records.entry(identifier.clone()).or_insert_with(Vec::new);

        // Add new timestamp.
        entry.push(Instant::now());

        // Clean expired records (return value ignored because entry was just appended).
        let _ = self.clean_expired_records_internal(entry, &config);

        // Opportunistic cleanup to avoid long-term buildup of inactive identifiers.
        self.opportunistic_cleanup(&mut records).await;
    }

    /// Verifies whether request limit has been reached.
    ///
    /// # Parameters
    /// * `identifier` - Identifier used to distinguish request sources.
    ///
    /// # Returns
    /// * `Ok(())` - Limit not reached; request can proceed.
    /// * `Err(u64)` - Limit reached; wait time in milliseconds.
    pub async fn verify(&self, identifier: &str) -> Result<(), u64> {
        let config = self.get_key_config(identifier).await;
        let records = self.records.read().await;

        if let Some(entry) = records.get(identifier) {
            // No records: allow immediately.
            if entry.is_empty() {
                return Ok(());
            }

            // Compute minimum interval between adjacent requests.
            let min_interval = Duration::from_millis(
                (config.window_size.as_millis() as f64 / config.max_requests_per_second as f64)
                    as u64,
            );

            // Get timestamp of the last request.
            let last_request_time = entry.last().unwrap();
            let elapsed_since_last = last_request_time.elapsed();

            // Check whether interval is sufficient.
            if elapsed_since_last >= min_interval {
                Ok(())
            } else {
                // Interval too short; compute remaining wait time.
                let wait_time = min_interval - elapsed_since_last;
                Err(wait_time.as_millis() as u64)
            }
        } else {
            // No records: allow immediately.
            Ok(())
        }
    }

    /// Verifies and records a request (atomic operation).
    ///
    /// # Parameters
    /// * `identifier` - Identifier used to distinguish request sources.
    ///
    /// # Returns
    /// * `Ok(())` - Limit not reached; request was recorded.
    /// * `Err(u64)` - Limit reached; wait time in milliseconds.
    pub async fn verify_and_record(&self, identifier: String) -> Result<(), u64> {
        let config = self.get_key_config(&identifier).await;
        let mut records = self.records.write().await;
        // Perform opportunistic cleanup while holding write lock.
        self.opportunistic_cleanup(&mut records).await;

        let entry = records.entry(identifier).or_insert_with(Vec::new);
        // Clean expired records.
        self.clean_expired_records_internal(entry, &config);

        let now = Instant::now();

        // No records: allow immediately.
        if entry.is_empty() {
            entry.push(now);
            return Ok(());
        }

        // Compute minimum interval between adjacent requests.
        // Interval = window_size / max_requests_per_second.
        let min_interval = Duration::from_millis(
            (config.window_size.as_millis() as f64 / config.max_requests_per_second as f64) as u64,
        );

        // Get timestamp of the last request.
        let last_request_time = entry.last().unwrap();
        let elapsed_since_last = now.duration_since(*last_request_time);

        // Check whether interval is sufficient.
        if elapsed_since_last >= min_interval {
            // Interval is sufficient; record request.
            entry.push(now);
            Ok(())
        } else {
            // Interval too short; compute remaining wait time.
            let wait_time = min_interval - elapsed_since_last;
            Err(wait_time.as_millis() as u64)
        }
    }

    /// Internal helper: cleans expired timestamps.
    ///
    /// # Parameters
    /// * `records` - Timestamp list to clean.
    /// * `config` - Config for this key.
    ///
    /// # Returns
    /// * `true` - No records remain after cleanup; identifier can be removed.
    /// * `false` - Valid records remain.
    fn clean_expired_records_internal(
        &self,
        records: &mut Vec<Instant>,
        config: &RateLimitConfig,
    ) -> bool {
        let now = Instant::now();
        let cutoff_time = now - config.window_size;

        // Keep records within current window.
        records.retain(|&timestamp| timestamp > cutoff_time);

        // Return whether identifier should be removed.
        records.is_empty()
    }

    /// Opportunistic cleanup: prune expired identifiers during write operations.
    /// This prevents long-inactive identifiers from lingering in memory indefinitely.
    async fn opportunistic_cleanup(&self, records: &mut HashMap<String, Vec<Instant>>) {
        let total_identifiers = records.len();

        // Clean up at most 5 identifiers, or 10% of total (minimum 1), whichever is smaller.
        // For small maps, still perform lightweight cleanup to avoid stale key retention.
        let max_cleanup_count = std::cmp::min(5, (total_identifiers / 10).max(1));

        let mut keys_to_remove = Vec::new();

        // Iterate over a subset of identifiers for cleanup.
        for (checked_count, (key, entry)) in records.iter_mut().enumerate() {
            if checked_count >= max_cleanup_count {
                break;
            }

            // Get config for this key.
            let config = self.get_key_config(key).await;
            if self.clean_expired_records_internal(entry, &config) {
                keys_to_remove.push(key.clone());
            }
        }

        // Remove empty identifiers.
        for key in keys_to_remove {
            records.remove(&key);
        }
    }

    /// Counts valid records without mutating source data.
    fn count_valid_records(&self, records: &[Instant], config: &RateLimitConfig) -> f32 {
        let now = Instant::now();
        let cutoff_time = now - config.window_size;

        records
            .iter()
            .filter(|&&timestamp| timestamp > cutoff_time)
            .count() as f32
    }

    /// Calculates wait time in milliseconds.
    ///
    /// # Parameters
    /// * `records` - Timestamp list.
    /// * `config` - Config for this key.
    ///
    /// # Returns
    /// Milliseconds to wait.
    fn calculate_wait_time(&self, records: &[Instant], config: &RateLimitConfig) -> u64 {
        if records.is_empty() {
            return 0;
        }

        // Find oldest record.
        let oldest_record = match records.iter().min() {
            Some(record) => record,
            None => return 0, // Should not happen because `is_empty()` is checked above.
        };

        // Compute elapsed duration since oldest record.
        let elapsed = oldest_record.elapsed();

        // If still in window, compute remaining wait time.
        if elapsed < config.window_size {
            let remaining = config.window_size - elapsed;
            remaining.as_millis() as u64 + 1 // Add 1ms to ensure next check can pass.
        } else {
            0
        }
    }

    /// Returns current request count for identifier.
    ///
    /// # Parameters
    /// * `identifier` - Identifier.
    ///
    /// # Returns
    /// Number of requests in current window.
    pub async fn get_current_count(&self, identifier: &str) -> usize {
        let config = self.get_key_config(identifier).await;
        let records = self.records.read().await;
        if let Some(entry) = records.get(identifier) {
            self.count_valid_records(entry, &config) as usize
        } else {
            0
        }
    }

    /// Returns time until next available request (milliseconds).
    ///
    /// # Parameters
    /// * `identifier` - Identifier.
    ///
    /// # Returns
    /// Wait time in milliseconds; `0` means request is immediately available.
    pub async fn get_next_available_time(&self, identifier: &str) -> u64 {
        let config = self.get_key_config(identifier).await;
        let records = self.records.read().await;

        if let Some(entry) = records.get(identifier) {
            if entry.is_empty() {
                return 0;
            }

            // Compute minimum interval between adjacent requests.
            let min_interval = Duration::from_millis(
                (config.window_size.as_millis() as f64 / config.max_requests_per_second as f64)
                    as u64,
            );

            // Get timestamp of the last request.
            let last_request_time = entry.last().unwrap();
            let elapsed_since_last = last_request_time.elapsed();

            if elapsed_since_last >= min_interval {
                0
            } else {
                let wait_time = min_interval - elapsed_since_last;
                wait_time.as_millis() as u64
            }
        } else {
            0
        }
    }

    /// Cleans all expired records.
    /// This can be called periodically to reclaim memory.
    /// Note: methods already perform automatic cleanup; this is primarily for manual cleanup.
    pub async fn cleanup(&self) {
        let mut records = self.records.write().await;
        let mut keys_to_remove = Vec::new();

        for (key, entry) in records.iter_mut() {
            let config = self.get_key_config(key).await;
            if self.clean_expired_records_internal(entry, &config) {
                keys_to_remove.push(key.clone());
            }
        }

        // Remove empty entries.
        for key in keys_to_remove {
            records.remove(&key);
        }
    }

    /// Returns number of identifiers currently stored.
    pub async fn get_identifier_count(&self) -> usize {
        let records = self.records.read().await;
        records.len()
    }

    /// Resets records for a specific identifier.
    pub async fn reset(&self, identifier: &str) {
        let mut records = self.records.write().await;
        records.remove(identifier);
    }

    /// Resets all records.
    pub async fn reset_all(&self) {
        let mut records = self.records.write().await;
        records.clear();
    }
}

impl Clone for SlidingWindowRateLimiter {
    fn clone(&self) -> Self {
        Self {
            default_config: self.default_config.clone(),
            key_configs: Arc::clone(&self.key_configs),
            records: Arc::clone(&self.records),
        }
    }
}