datasynth-core 2.3.1

Core domain models, traits, and distributions for synthetic enterprise data generation
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Token bucket rate limiter implementation.
//!
//! Provides a token bucket algorithm for rate limiting with support for
//! burst capacity and multiple backpressure strategies.

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

use serde::{Deserialize, Serialize};

/// Configuration for rate limiting.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Target entities per second.
    pub entities_per_second: f64,
    /// Burst size (maximum tokens in bucket).
    pub burst_size: u32,
    /// Backpressure strategy when rate is exceeded.
    pub backpressure: RateLimitBackpressure,
    /// Whether rate limiting is enabled.
    pub enabled: bool,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            entities_per_second: 1000.0,
            burst_size: 100,
            backpressure: RateLimitBackpressure::Block,
            enabled: true,
        }
    }
}

/// Backpressure strategy when rate limit is exceeded.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RateLimitBackpressure {
    /// Block until tokens are available.
    #[default]
    Block,
    /// Drop excess items.
    Drop,
    /// Buffer items up to a limit, then block.
    Buffer {
        /// Maximum number of items to buffer.
        max_buffered: usize,
    },
}

/// Result of a rate limit acquisition.
#[derive(Debug, Clone, PartialEq)]
pub enum RateLimitAction {
    /// Request can proceed immediately.
    Proceed,
    /// Request was dropped due to rate limiting.
    Dropped,
    /// Request was buffered.
    Buffered {
        /// Position in buffer.
        position: usize,
    },
    /// Request waited before proceeding.
    Waited {
        /// Time waited in milliseconds.
        wait_time_ms: u64,
    },
}

/// Statistics for the rate limiter.
#[derive(Debug, Clone, Default)]
pub struct RateLimiterStats {
    /// Total acquisitions attempted.
    pub total_acquisitions: u64,
    /// Acquisitions that proceeded immediately.
    pub immediate_proceeds: u64,
    /// Acquisitions that required waiting.
    pub waits: u64,
    /// Acquisitions that were dropped.
    pub drops: u64,
    /// Acquisitions that were buffered.
    pub buffers: u64,
    /// Total wait time in milliseconds.
    pub total_wait_time_ms: u64,
    /// Current tokens available.
    pub current_tokens: f64,
    /// Current buffer size.
    pub buffer_size: usize,
}

/// Token bucket rate limiter.
///
/// Implements the token bucket algorithm for rate limiting:
/// - Tokens are added at a steady rate up to a maximum (burst) capacity
/// - Each operation consumes one token
/// - If no tokens are available, behavior depends on backpressure strategy
pub struct RateLimiter {
    config: RateLimitConfig,
    /// Current number of tokens in the bucket.
    tokens: f64,
    /// Time of last token refill.
    last_refill: Instant,
    /// Buffer for items waiting due to rate limiting.
    buffer: VecDeque<Instant>,
    /// Statistics.
    stats: RateLimiterStats,
}

impl RateLimiter {
    /// Creates a new rate limiter with the given configuration.
    pub fn new(config: RateLimitConfig) -> Self {
        Self {
            tokens: config.burst_size as f64,
            last_refill: Instant::now(),
            buffer: VecDeque::new(),
            stats: RateLimiterStats {
                current_tokens: config.burst_size as f64,
                ..Default::default()
            },
            config,
        }
    }

    /// Creates a rate limiter with a simple rate (entities per second).
    pub fn with_rate(entities_per_second: f64) -> Self {
        Self::new(RateLimitConfig {
            entities_per_second,
            ..Default::default()
        })
    }

    /// Creates a disabled rate limiter (always allows).
    pub fn disabled() -> Self {
        Self::new(RateLimitConfig {
            enabled: false,
            ..Default::default()
        })
    }

    /// Acquires a token, blocking if necessary.
    ///
    /// Returns the action taken to acquire the token.
    pub fn acquire(&mut self) -> RateLimitAction {
        if !self.config.enabled {
            self.stats.total_acquisitions += 1;
            self.stats.immediate_proceeds += 1;
            return RateLimitAction::Proceed;
        }

        self.stats.total_acquisitions += 1;
        self.refill_tokens();

        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            self.stats.current_tokens = self.tokens;
            self.stats.immediate_proceeds += 1;
            return RateLimitAction::Proceed;
        }

        // No tokens available, apply backpressure strategy
        match self.config.backpressure {
            RateLimitBackpressure::Block => {
                let wait_time = self.wait_for_token();
                self.stats.waits += 1;
                self.stats.total_wait_time_ms += wait_time;
                RateLimitAction::Waited {
                    wait_time_ms: wait_time,
                }
            }
            RateLimitBackpressure::Drop => {
                self.stats.drops += 1;
                RateLimitAction::Dropped
            }
            RateLimitBackpressure::Buffer { max_buffered } => {
                if self.buffer.len() < max_buffered {
                    self.buffer.push_back(Instant::now());
                    self.stats.buffers += 1;
                    self.stats.buffer_size = self.buffer.len();
                    RateLimitAction::Buffered {
                        position: self.buffer.len(),
                    }
                } else {
                    // Buffer full, block
                    let wait_time = self.wait_for_token();
                    self.stats.waits += 1;
                    self.stats.total_wait_time_ms += wait_time;
                    RateLimitAction::Waited {
                        wait_time_ms: wait_time,
                    }
                }
            }
        }
    }

    /// Tries to acquire a token without blocking.
    ///
    /// Returns `Some(action)` if a token was acquired, `None` if rate limited.
    pub fn try_acquire(&mut self) -> Option<RateLimitAction> {
        if !self.config.enabled {
            self.stats.total_acquisitions += 1;
            self.stats.immediate_proceeds += 1;
            return Some(RateLimitAction::Proceed);
        }

        self.refill_tokens();

        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            self.stats.current_tokens = self.tokens;
            self.stats.total_acquisitions += 1;
            self.stats.immediate_proceeds += 1;
            Some(RateLimitAction::Proceed)
        } else {
            None
        }
    }

    /// Acquires a token with a timeout.
    ///
    /// Returns the action taken, or `None` if the timeout was exceeded.
    pub fn acquire_timeout(&mut self, timeout: Duration) -> Option<RateLimitAction> {
        if !self.config.enabled {
            self.stats.total_acquisitions += 1;
            self.stats.immediate_proceeds += 1;
            return Some(RateLimitAction::Proceed);
        }

        self.stats.total_acquisitions += 1;
        self.refill_tokens();

        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            self.stats.current_tokens = self.tokens;
            self.stats.immediate_proceeds += 1;
            return Some(RateLimitAction::Proceed);
        }

        // Calculate time needed for one token
        let tokens_needed = 1.0 - self.tokens;
        let time_needed = Duration::from_secs_f64(tokens_needed / self.config.entities_per_second);

        if time_needed > timeout {
            // Timeout exceeded
            match self.config.backpressure {
                RateLimitBackpressure::Drop => {
                    self.stats.drops += 1;
                    Some(RateLimitAction::Dropped)
                }
                _ => None,
            }
        } else {
            std::thread::sleep(time_needed);
            self.refill_tokens();
            self.tokens -= 1.0;
            self.stats.current_tokens = self.tokens;
            self.stats.waits += 1;
            self.stats.total_wait_time_ms += time_needed.as_millis() as u64;
            Some(RateLimitAction::Waited {
                wait_time_ms: time_needed.as_millis() as u64,
            })
        }
    }

    /// Returns the current statistics.
    pub fn stats(&self) -> RateLimiterStats {
        let mut stats = self.stats.clone();
        stats.current_tokens = self.tokens;
        stats.buffer_size = self.buffer.len();
        stats
    }

    /// Resets the rate limiter to initial state.
    pub fn reset(&mut self) {
        self.tokens = self.config.burst_size as f64;
        self.last_refill = Instant::now();
        self.buffer.clear();
        self.stats = RateLimiterStats {
            current_tokens: self.tokens,
            ..Default::default()
        };
    }

    /// Returns the current number of available tokens.
    pub fn available_tokens(&self) -> f64 {
        self.tokens
    }

    /// Returns the configuration.
    pub fn config(&self) -> &RateLimitConfig {
        &self.config
    }

    /// Updates the rate limit.
    pub fn set_rate(&mut self, entities_per_second: f64) {
        self.config.entities_per_second = entities_per_second;
    }

    /// Enables or disables the rate limiter.
    pub fn set_enabled(&mut self, enabled: bool) {
        self.config.enabled = enabled;
    }

    /// Refills tokens based on elapsed time.
    fn refill_tokens(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_refill);
        let new_tokens = elapsed.as_secs_f64() * self.config.entities_per_second;

        self.tokens = (self.tokens + new_tokens).min(self.config.burst_size as f64);
        self.last_refill = now;
    }

    /// Waits until a token is available.
    fn wait_for_token(&mut self) -> u64 {
        let tokens_needed = 1.0 - self.tokens;
        let wait_secs = tokens_needed / self.config.entities_per_second;
        let wait_duration = Duration::from_secs_f64(wait_secs);

        std::thread::sleep(wait_duration);

        self.refill_tokens();
        self.tokens -= 1.0;
        self.stats.current_tokens = self.tokens;

        wait_duration.as_millis() as u64
    }

    /// Processes the buffer, releasing items as tokens become available.
    pub fn process_buffer(&mut self) -> Vec<Duration> {
        self.refill_tokens();

        let mut wait_times = Vec::new();

        while !self.buffer.is_empty() && self.tokens >= 1.0 {
            if let Some(enqueue_time) = self.buffer.pop_front() {
                let wait_time = enqueue_time.elapsed();
                wait_times.push(wait_time);
                self.tokens -= 1.0;
            }
        }

        self.stats.buffer_size = self.buffer.len();
        self.stats.current_tokens = self.tokens;

        wait_times
    }
}

/// A rate-limited wrapper for any iterator.
pub struct RateLimitedIterator<I> {
    inner: I,
    limiter: RateLimiter,
}

impl<I> RateLimitedIterator<I> {
    /// Creates a new rate-limited iterator.
    pub fn new(inner: I, limiter: RateLimiter) -> Self {
        Self { inner, limiter }
    }

    /// Creates a rate-limited iterator with a simple rate.
    pub fn with_rate(inner: I, entities_per_second: f64) -> Self {
        Self::new(inner, RateLimiter::with_rate(entities_per_second))
    }

    /// Returns the limiter statistics.
    pub fn stats(&self) -> RateLimiterStats {
        self.limiter.stats()
    }
}

impl<I: Iterator> Iterator for RateLimitedIterator<I> {
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.limiter.acquire();
        self.inner.next()
    }
}

/// Extension trait to add rate limiting to any iterator.
pub trait RateLimitExt: Iterator + Sized {
    /// Applies rate limiting to this iterator.
    fn rate_limit(self, entities_per_second: f64) -> RateLimitedIterator<Self> {
        RateLimitedIterator::with_rate(self, entities_per_second)
    }

    /// Applies rate limiting with custom config.
    fn rate_limit_with(self, config: RateLimitConfig) -> RateLimitedIterator<Self> {
        RateLimitedIterator::new(self, RateLimiter::new(config))
    }
}

impl<I: Iterator> RateLimitExt for I {}

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

    #[test]
    fn test_rate_limiter_immediate_proceed() {
        let config = RateLimitConfig {
            entities_per_second: 1000.0,
            burst_size: 10,
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // First 10 should proceed immediately (burst capacity)
        for _ in 0..10 {
            let action = limiter.acquire();
            assert_eq!(action, RateLimitAction::Proceed);
        }

        let stats = limiter.stats();
        assert_eq!(stats.total_acquisitions, 10);
        assert_eq!(stats.immediate_proceeds, 10);
    }

    #[test]
    fn test_rate_limiter_blocking() {
        let config = RateLimitConfig {
            entities_per_second: 1000.0,
            burst_size: 1,
            backpressure: RateLimitBackpressure::Block,
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // First should proceed
        let action1 = limiter.acquire();
        assert_eq!(action1, RateLimitAction::Proceed);

        // Second should wait
        let action2 = limiter.acquire();
        assert!(matches!(action2, RateLimitAction::Waited { .. }));
    }

    #[test]
    fn test_rate_limiter_drop() {
        let config = RateLimitConfig {
            entities_per_second: 10.0,
            burst_size: 1,
            backpressure: RateLimitBackpressure::Drop,
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // First should proceed
        let action1 = limiter.acquire();
        assert_eq!(action1, RateLimitAction::Proceed);

        // Second should be dropped (no time to refill)
        let action2 = limiter.acquire();
        assert_eq!(action2, RateLimitAction::Dropped);

        let stats = limiter.stats();
        assert_eq!(stats.drops, 1);
    }

    #[test]
    fn test_rate_limiter_buffer() {
        let config = RateLimitConfig {
            entities_per_second: 10.0,
            burst_size: 1,
            backpressure: RateLimitBackpressure::Buffer { max_buffered: 5 },
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // First should proceed
        let action1 = limiter.acquire();
        assert_eq!(action1, RateLimitAction::Proceed);

        // Next should be buffered
        let action2 = limiter.acquire();
        assert!(matches!(action2, RateLimitAction::Buffered { position: 1 }));

        let stats = limiter.stats();
        assert_eq!(stats.buffers, 1);
        assert_eq!(stats.buffer_size, 1);
    }

    #[test]
    fn test_rate_limiter_try_acquire() {
        let config = RateLimitConfig {
            entities_per_second: 10.0,
            burst_size: 1,
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // First should succeed
        assert!(limiter.try_acquire().is_some());

        // Second should fail (no time to refill)
        assert!(limiter.try_acquire().is_none());
    }

    #[test]
    fn test_rate_limiter_disabled() {
        let mut limiter = RateLimiter::disabled();

        // All should proceed immediately
        for _ in 0..100 {
            let action = limiter.acquire();
            assert_eq!(action, RateLimitAction::Proceed);
        }
    }

    #[test]
    fn test_rate_limiter_reset() {
        let config = RateLimitConfig {
            entities_per_second: 10.0,
            burst_size: 5,
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // Consume some tokens
        for _ in 0..5 {
            limiter.acquire();
        }

        assert!(limiter.available_tokens() < 1.0);

        limiter.reset();

        assert_eq!(limiter.available_tokens(), 5.0);
    }

    #[test]
    fn test_rate_limited_iterator() {
        let items = vec![1, 2, 3, 4, 5];
        let rate_limited: Vec<_> = items
            .into_iter()
            .rate_limit_with(RateLimitConfig {
                entities_per_second: 10000.0,
                burst_size: 100,
                ..Default::default()
            })
            .collect();

        assert_eq!(rate_limited, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_rate_limiter_refill() {
        let config = RateLimitConfig {
            entities_per_second: 100.0, // 100 per second = 1 per 10ms
            burst_size: 10,
            ..Default::default()
        };
        let mut limiter = RateLimiter::new(config);

        // Consume all tokens
        for _ in 0..10 {
            limiter.try_acquire();
        }
        assert!(limiter.available_tokens() < 1.0);

        // Wait for refill (20ms should give ~2 tokens)
        std::thread::sleep(Duration::from_millis(25));

        // Should have some tokens now
        assert!(limiter.try_acquire().is_some());
    }

    #[test]
    fn test_rate_limit_config_default() {
        let config = RateLimitConfig::default();
        assert!(config.enabled);
        assert_eq!(config.entities_per_second, 1000.0);
        assert_eq!(config.burst_size, 100);
    }
}