use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ThrottleConfig {
pub batch_delay_ms: u64,
pub batch_size: usize,
pub enabled: bool,
}
impl Default for ThrottleConfig {
fn default() -> Self {
Self {
batch_delay_ms: 10,
batch_size: 100,
enabled: true,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Throttle {
config: ThrottleConfig,
}
impl Throttle {
pub fn new(config: ThrottleConfig) -> Self {
Self { config }
}
pub fn delay_sync(&self) {
if !self.config.enabled {
return;
}
std::thread::sleep(Duration::from_millis(self.config.batch_delay_ms));
}
pub async fn delay_async(&self) {
if !self.config.enabled {
return;
}
tokio::time::sleep(Duration::from_millis(self.config.batch_delay_ms)).await;
}
pub fn process_batched<T, F, R>(&self, items: Vec<T>, f: F) -> Vec<R>
where
F: Fn(&[T]) -> Vec<R>,
{
let mut results = Vec::new();
let mut first = true;
for chunk in items.chunks(self.config.batch_size) {
if first {
first = false;
} else {
self.delay_sync();
}
results.extend(f(chunk));
}
results
}
pub async fn process_batched_async<T, F, Fut, R>(&self, items: Vec<T>, f: F) -> Vec<R>
where
T: Clone,
F: Fn(Vec<T>) -> Fut,
Fut: std::future::Future<Output = Vec<R>>,
{
let mut results = Vec::new();
let mut first = true;
for chunk in items.chunks(self.config.batch_size) {
if first {
first = false;
} else {
self.delay_async().await;
}
results.extend(f(chunk.to_vec()).await);
}
results
}
pub fn batch_size(&self) -> usize {
self.config.batch_size
}
pub fn is_enabled(&self) -> bool {
self.config.enabled
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_throttle_disabled() {
let config = ThrottleConfig {
enabled: false,
batch_delay_ms: 1000, batch_size: 10,
};
let throttle = Throttle::new(config);
let start = std::time::Instant::now();
throttle.delay_sync();
let elapsed = start.elapsed();
assert!(
elapsed.as_millis() < 10,
"Disabled throttle should not delay"
);
}
#[test]
fn test_throttle_enabled() {
let config = ThrottleConfig {
enabled: true,
batch_delay_ms: 50,
batch_size: 10,
};
let throttle = Throttle::new(config);
let start = std::time::Instant::now();
throttle.delay_sync();
let elapsed = start.elapsed();
assert!(
elapsed.as_millis() >= 50,
"Enabled throttle should delay at least batch_delay_ms"
);
}
#[test]
fn test_process_batched() {
let config = ThrottleConfig {
enabled: true,
batch_delay_ms: 1, batch_size: 3,
};
let throttle = Throttle::new(config);
let items: Vec<i32> = (0..10).collect();
let results =
throttle.process_batched(items, |batch| batch.iter().map(|x| x * 2).collect());
assert_eq!(results, vec![0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
}
#[test]
fn test_process_batched_empty() {
let throttle = Throttle::default();
let items: Vec<i32> = vec![];
let results =
throttle.process_batched(items, |batch| batch.iter().map(|x| x * 2).collect());
assert!(results.is_empty());
}
#[test]
fn test_getters() {
let config = ThrottleConfig {
enabled: true,
batch_delay_ms: 25,
batch_size: 50,
};
let throttle = Throttle::new(config);
assert_eq!(throttle.batch_size(), 50);
assert!(throttle.is_enabled());
}
#[test]
fn test_default_config() {
let config = ThrottleConfig::default();
assert_eq!(config.batch_delay_ms, 10);
assert_eq!(config.batch_size, 100);
assert!(config.enabled);
}
#[test]
fn test_default_throttle() {
let throttle = Throttle::default();
assert_eq!(throttle.batch_size(), 100);
assert!(throttle.is_enabled());
}
}