reinhardt-rest 0.1.2

REST API framework aggregator for Reinhardt
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
//! Leaky bucket algorithm implementation for rate limiting
//!
//! The leaky bucket algorithm processes requests at a constant rate,
//! smoothing out bursts. Requests that exceed the bucket's capacity are rejected.

use super::backend::ThrottleBackend;
use super::{Throttle, ThrottleResult};
use super::time_provider::{SystemTimeProvider, TimeProvider};
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::Instant;

/// Configuration for leaky bucket algorithm
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct LeakyBucketConfig {
	/// Maximum number of requests in the bucket (queue capacity)
	pub capacity: usize,
	/// Rate at which requests leak from the bucket (requests per second)
	pub leak_rate: f64,
}

impl LeakyBucketConfig {
	/// Creates a new leaky bucket configuration
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::throttling::leaky_bucket::LeakyBucketConfig;
	///
	/// // 10 requests per second with queue capacity of 20
	/// let config = LeakyBucketConfig::new(20, 10.0);
	/// assert_eq!(config.capacity, 20);
	/// assert_eq!(config.leak_rate, 10.0);
	/// ```
	pub fn new(capacity: usize, leak_rate: f64) -> Self {
		Self {
			capacity,
			leak_rate,
		}
	}

	/// Create configuration for requests per second
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::throttling::leaky_bucket::LeakyBucketConfig;
	///
	/// // 5 requests per second with queue of 10
	/// let config = LeakyBucketConfig::per_second(5.0, 10);
	/// assert_eq!(config.leak_rate, 5.0);
	/// assert_eq!(config.capacity, 10);
	/// ```
	pub fn per_second(rate: f64, capacity: usize) -> Self {
		Self {
			capacity,
			leak_rate: rate,
		}
	}

	/// Create configuration for requests per minute
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::throttling::leaky_bucket::LeakyBucketConfig;
	///
	/// // 60 requests per minute with queue of 100
	/// let config = LeakyBucketConfig::per_minute(60.0, 100);
	/// assert_eq!(config.leak_rate, 1.0);
	/// assert_eq!(config.capacity, 100);
	/// ```
	pub fn per_minute(rate: f64, capacity: usize) -> Self {
		Self {
			capacity,
			leak_rate: rate / 60.0,
		}
	}
}

/// Bucket state for tracking requests
#[derive(Debug, Clone)]
struct BucketState {
	/// Current number of requests in the bucket
	level: f64,
	/// Last time the bucket was leaked
	last_leak: Instant,
}

/// Leaky bucket throttle implementation
///
/// # Examples
///
/// ```
/// use reinhardt_rest::throttling::leaky_bucket::{LeakyBucketThrottle, LeakyBucketConfig};
/// use reinhardt_rest::throttling::{MemoryBackend, Throttle};
/// use std::sync::Arc;
///
/// # tokio_test::block_on(async {
/// let backend = Arc::new(MemoryBackend::new());
/// let config = LeakyBucketConfig::per_second(10.0, 20);
/// let throttle = LeakyBucketThrottle::new("api_key".to_string(), backend, config);
///
/// // Requests are processed at constant rate
/// assert!(throttle.allow_request("user_123").await.unwrap());
/// # });
/// ```
pub struct LeakyBucketThrottle<B: ThrottleBackend, T: TimeProvider = SystemTimeProvider> {
	// Allow dead_code: identifies this throttle instance for distributed backend key-spacing
	#[allow(dead_code)]
	key: String,
	// Allow dead_code: stored for future distributed state persistence via ThrottleBackend
	#[allow(dead_code)]
	backend: Arc<B>,
	config: LeakyBucketConfig,
	time_provider: Arc<T>,
	state: Arc<RwLock<BucketState>>,
}

impl<B: ThrottleBackend> LeakyBucketThrottle<B, SystemTimeProvider> {
	/// Creates a new leaky bucket with default time provider
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_rest::throttling::leaky_bucket::{LeakyBucketThrottle, LeakyBucketConfig};
	/// use reinhardt_rest::throttling::MemoryBackend;
	/// use std::sync::Arc;
	///
	/// let backend = Arc::new(MemoryBackend::new());
	/// let config = LeakyBucketConfig::per_second(5.0, 10);
	/// let throttle = LeakyBucketThrottle::new("api_key".to_string(), backend, config);
	/// ```
	pub fn new(key: String, backend: Arc<B>, config: LeakyBucketConfig) -> Self {
		let initial_state = BucketState {
			level: 0.0,
			last_leak: SystemTimeProvider::new().now(),
		};

		Self {
			key,
			backend,
			config,
			time_provider: Arc::new(SystemTimeProvider::new()),
			state: Arc::new(RwLock::new(initial_state)),
		}
	}
}

impl<B: ThrottleBackend, T: TimeProvider> LeakyBucketThrottle<B, T> {
	/// Creates a new leaky bucket with custom time provider
	pub fn with_time_provider(
		key: String,
		backend: Arc<B>,
		config: LeakyBucketConfig,
		time_provider: Arc<T>,
	) -> Self {
		let initial_state = BucketState {
			level: 0.0,
			last_leak: time_provider.now(),
		};

		Self {
			key,
			backend,
			config,
			time_provider,
			state: Arc::new(RwLock::new(initial_state)),
		}
	}

	/// Leak requests from the bucket based on elapsed time
	fn leak_bucket(&self, state: &mut BucketState) {
		let now = self.time_provider.now();
		let elapsed = now.duration_since(state.last_leak);
		let elapsed_secs = elapsed.as_secs_f64();

		// Calculate how many requests have leaked
		let leaked = elapsed_secs * self.config.leak_rate;

		// Update bucket level (cannot go below 0)
		state.level = (state.level - leaked).max(0.0);
		state.last_leak = now;
	}

	/// Get current bucket level
	pub async fn level(&self) -> f64 {
		let mut state = self.state.write().await;
		self.leak_bucket(&mut state);
		state.level
	}

	/// Reset the bucket to empty
	pub async fn reset(&self) {
		let mut state = self.state.write().await;
		state.level = 0.0;
		state.last_leak = self.time_provider.now();
	}
}

#[async_trait]
impl<B: ThrottleBackend, T: TimeProvider> Throttle for LeakyBucketThrottle<B, T> {
	async fn allow_request(&self, _key: &str) -> ThrottleResult<bool> {
		let mut state = self.state.write().await;

		// Leak requests first
		self.leak_bucket(&mut state);

		// Check if there's room in the bucket
		if state.level < self.config.capacity as f64 {
			state.level += 1.0;
			Ok(true)
		} else {
			Ok(false)
		}
	}

	async fn wait_time(&self, _key: &str) -> ThrottleResult<Option<u64>> {
		let state = self.state.read().await;

		if state.level < self.config.capacity as f64 {
			return Ok(None);
		}

		// Calculate time until space is available
		// Need to wait until level drops below capacity
		let excess = state.level - (self.config.capacity as f64 - 1.0);
		let wait_secs = (excess / self.config.leak_rate).ceil();

		Ok(Some(wait_secs as u64))
	}

	fn get_rate(&self) -> (usize, u64) {
		(self.config.leak_rate as usize, 1)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::throttling::backend::MemoryBackend;
	use crate::throttling::time_provider::MockTimeProvider;

	#[tokio::test]
	async fn test_leaky_bucket_basic() {
		use tokio::time::Instant;
		let time_provider = Arc::new(MockTimeProvider::new(Instant::now()));
		let backend = Arc::new(MemoryBackend::with_time_provider(time_provider.clone()));
		let config = LeakyBucketConfig::new(5, 1.0);
		let throttle = LeakyBucketThrottle::with_time_provider(
			"test".to_string(),
			backend,
			config,
			time_provider,
		);

		// Should allow up to capacity
		for _ in 0..5 {
			assert!(throttle.allow_request("user").await.unwrap());
		}

		// 6th request should fail
		assert!(!throttle.allow_request("user").await.unwrap());
	}

	#[tokio::test]
	async fn test_leaky_bucket_leak() {
		use tokio::time::Instant;
		let time_provider = Arc::new(MockTimeProvider::new(Instant::now()));
		let backend = Arc::new(MemoryBackend::with_time_provider(time_provider.clone()));
		let config = LeakyBucketConfig::new(10, 2.0); // 2 requests per second leak rate
		let throttle = LeakyBucketThrottle::with_time_provider(
			"test".to_string(),
			backend,
			config,
			time_provider.clone(),
		);

		// Fill the bucket
		for _ in 0..10 {
			assert!(throttle.allow_request("user").await.unwrap());
		}
		assert!(!throttle.allow_request("user").await.unwrap());

		// Advance time by 1 second (2 requests should leak)
		time_provider.advance(std::time::Duration::from_secs(1));

		// Should allow 2 more requests
		assert!(throttle.allow_request("user").await.unwrap());
		assert!(throttle.allow_request("user").await.unwrap());
		assert!(!throttle.allow_request("user").await.unwrap());
	}

	#[tokio::test]
	async fn test_leaky_bucket_smoothing() {
		use tokio::time::Instant;
		let time_provider = Arc::new(MockTimeProvider::new(Instant::now()));
		let backend = Arc::new(MemoryBackend::with_time_provider(time_provider.clone()));
		let config = LeakyBucketConfig::per_second(5.0, 10);
		let throttle = LeakyBucketThrottle::with_time_provider(
			"test".to_string(),
			backend,
			config,
			time_provider.clone(),
		);

		// Burst of requests
		for _ in 0..10 {
			assert!(throttle.allow_request("user").await.unwrap());
		}

		// Bucket full
		assert!(!throttle.allow_request("user").await.unwrap());

		// Advance time by 2 seconds (10 requests leak)
		time_provider.advance(std::time::Duration::from_secs(2));

		// Bucket should be empty, allow full capacity again
		for _ in 0..10 {
			assert!(throttle.allow_request("user").await.unwrap());
		}
	}

	#[tokio::test]
	async fn test_leaky_bucket_level() {
		use tokio::time::Instant;
		let time_provider = Arc::new(MockTimeProvider::new(Instant::now()));
		let backend = Arc::new(MemoryBackend::with_time_provider(time_provider.clone()));
		let config = LeakyBucketConfig::new(10, 2.0);
		let throttle = LeakyBucketThrottle::with_time_provider(
			"test".to_string(),
			backend,
			config,
			time_provider.clone(),
		);

		// Initial level should be 0
		assert_eq!(throttle.level().await, 0.0);

		// Add 5 requests
		for _ in 0..5 {
			throttle.allow_request("user").await.unwrap();
		}
		assert_eq!(throttle.level().await, 5.0);

		// Advance time by 1 second (2 requests leak)
		time_provider.advance(std::time::Duration::from_secs(1));
		assert_eq!(throttle.level().await, 3.0);
	}

	#[tokio::test]
	async fn test_leaky_bucket_reset() {
		use tokio::time::Instant;
		let time_provider = Arc::new(MockTimeProvider::new(Instant::now()));
		let backend = Arc::new(MemoryBackend::with_time_provider(time_provider.clone()));
		let config = LeakyBucketConfig::new(10, 1.0);
		let throttle = LeakyBucketThrottle::with_time_provider(
			"test".to_string(),
			backend,
			config,
			time_provider,
		);

		// Fill bucket
		for _ in 0..10 {
			throttle.allow_request("user").await.unwrap();
		}
		assert!(throttle.level().await > 0.0);

		// Reset
		throttle.reset().await;
		assert_eq!(throttle.level().await, 0.0);
	}

	#[tokio::test]
	async fn test_leaky_bucket_wait_time() {
		use tokio::time::Instant;
		let time_provider = Arc::new(MockTimeProvider::new(Instant::now()));
		let backend = Arc::new(MemoryBackend::with_time_provider(time_provider.clone()));
		let config = LeakyBucketConfig::new(5, 1.0);
		let throttle = LeakyBucketThrottle::with_time_provider(
			"test".to_string(),
			backend,
			config,
			time_provider,
		);

		// Fill bucket
		for _ in 0..5 {
			throttle.allow_request("user").await.unwrap();
		}

		// Should have wait time
		let wait = throttle.wait_time("user").await.unwrap();
		assert!(wait.is_some());
		assert!(wait.unwrap() > 0);
	}

	#[test]
	fn test_leaky_bucket_config_per_second() {
		let config = LeakyBucketConfig::per_second(10.0, 20);
		assert_eq!(config.leak_rate, 10.0);
		assert_eq!(config.capacity, 20);
	}

	#[test]
	fn test_leaky_bucket_config_per_minute() {
		let config = LeakyBucketConfig::per_minute(60.0, 100);
		assert_eq!(config.leak_rate, 1.0);
		assert_eq!(config.capacity, 100);
	}
}