flux-limiter 0.7.2

A rate limiter based on the Generic Cell Rate Algorithm (GCRA).
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
# Contributing to Flux Limiter


Thank you for your interest in contributing to Flux Limiter! This document provides guidelines and information for contributors.

## Table of Contents


- [Code of Conduct]#code-of-conduct
- [Getting Started]#getting-started
- [Development Workflow]#development-workflow
- [Code Standards]#code-standards
- [Testing Guidelines]#testing-guidelines
- [Documentation]#documentation
- [Performance Considerations]#performance-considerations
- [Submitting Changes]#submitting-changes
- [Release Process]#release-process

## Code of Conduct


We are committed to providing a welcoming and inclusive environment for all contributors. Please be respectful and constructive in all interactions.

## Getting Started


### Prerequisites


- **Rust**: Latest stable version (check with `rustc --version`)
- **Cargo**: Comes with Rust
- **Git**: For version control

### Setting Up Your Development Environment


1. **Fork and clone the repository**:
   ```bash
   git clone https://github.com/YOUR_USERNAME/flux-limiter.git

   cd flux-limiter

   ```

2. **Build the project**:
   ```bash
   cargo build

   ```

3. **Run the test suite**:
   ```bash
   cargo test

   ```

4. **Check code formatting**:
   ```bash
   cargo fmt --check

   ```

5. **Run clippy for linting**:
   ```bash
   cargo clippy -- -D warnings

   ```

### Project Structure


```
flux-limiter/
├── src/
│   ├── lib.rs              # Main library exports
│   ├── flux_limiter.rs     # Core rate limiter implementation
│   ├── config.rs           # Configuration types
│   ├── errors.rs           # Error handling
│   └── clock.rs            # Clock abstraction
├── tests/
│   └── ratelimiter/        # Integration tests
│       ├── fixtures/       # Test utilities
│       └── *.rs           # Test modules
├── docs/                   # mdbook documentation
│   ├── book.toml          # mdbook configuration
│   └── src/               # Documentation source
│       ├── SUMMARY.md     # Table of contents
│       └── *.md          # Documentation chapters
├── Cargo.toml
├── README.md
└── CONTRIBUTING.md
```

## Development Workflow


### Branch Naming


Use descriptive branch names with prefixes:
- `feature/add-new-algorithm` - New features
- `fix/clock-error-handling` - Bug fixes  
- `docs/update-readme` - Documentation updates
- `refactor/simplify-config` - Code refactoring
- `perf/optimize-cleanup` - Performance improvements

### Commit Messages


Follow conventional commit format:
```
type(scope): brief description

Longer explanation if needed

Fixes #123
```

Examples:
- `feat(core): add support for custom client ID types`
- `fix(clock): handle system time going backwards`
- `docs(readme): add error handling examples`
- `test(cleanup): add comprehensive cleanup tests`
- `perf(gcra): optimize nanosecond calculations`

## Code Standards


### Rust Style Guidelines


1. **Formatting**: Use `cargo fmt` for consistent formatting
2. **Linting**: Fix all `cargo clippy` warnings
3. **Naming**: Follow Rust naming conventions
   - `snake_case` for functions, variables, modules
   - `PascalCase` for types, traits, enums
   - `SCREAMING_SNAKE_CASE` for constants

### Code Quality Standards


1. **Error Handling**:
   - Use `Result<T, E>` for fallible operations
   - Provide meaningful error messages
   - Document error conditions in function docs

2. **Documentation**:
   - All public items must have doc comments
   - Include examples for public APIs
   - Document panics, errors, and safety requirements

3. **Safety**:
   - Minimize use of `unsafe` code
   - Document any `unsafe` blocks with safety invariants
   - Prefer safe alternatives when possible

4. **Performance**:
   - Avoid allocations in hot paths
   - Use appropriate data structures
   - Consider concurrent access patterns

### Example Code Style


```rust
/// Checks if a request should be allowed for the given client.
///
/// This method implements the GCRA algorithm to determine if the request
/// conforms to the configured rate and burst limits.
///
/// # Arguments
///
/// * `client_id` - Unique identifier for the client making the request
///
/// # Returns
///
/// * `Ok(FluxLimiterDecision)` - Rate limiting decision with metadata
/// * `Err(FluxLimiterError)` - Clock error or configuration issue
///
/// # Examples
///
/// ```rust
/// use flux_limiter::{FluxLimiter, FluxLimiterConfig, SystemClock};
///
/// let config = FluxLimiterConfig::new(10.0, 5.0);
/// let limiter = FluxLimiter::with_config(config, SystemClock)?;
///
/// match limiter.check_request("user_123") {
///     Ok(decision) if decision.allowed => {
///         // Process request
///     }
///     Ok(_) => {
///         // Rate limited
///     }
///     Err(e) => {
///         // Handle error
///     }
/// }
/// ```
pub fn check_request(&self, client_id: T) -> Result<FluxLimiterDecision, FluxLimiterError> {
    let current_time_nanos = self.clock.now().map_err(FluxLimiterError::ClockError)?;
    
    // Implementation...
}
```

## Testing Guidelines


### Test Organization


Tests are organized in the `tests/ratelimiter/` directory:
- **Unit tests**: Test individual functions/methods
- **Integration tests**: Test complete workflows
- **Error tests**: Test error conditions and recovery
- **Performance tests**: Test performance characteristics

### Writing Tests


1. **Test Structure**:
   ```rust
   #[test]
   fn descriptive_test_name() {
       // Arrange
       let clock = TestClock::new(0.0);
       let config = FluxLimiterConfig::new(10.0, 5.0);
       let limiter = FluxLimiter::with_config(config, clock).unwrap();
       
       // Act
       let result = limiter.check_request("client1");
       
       // Assert
       assert!(result.is_ok());
       assert!(result.unwrap().allowed);
   }
   ```

2. **Test Coverage Requirements**:
   - All public APIs must have tests
   - Error conditions must be tested
   - Edge cases should be covered
   - Performance characteristics should be validated

3. **Test Data**:
   - Use meaningful test data
   - Test boundary conditions
   - Test with realistic client ID types

### Using Test Fixtures


Use the provided `TestClock` for deterministic testing:

```rust
use crate::fixtures::test_clock::TestClock;

#[test]

fn test_time_progression() {
    let clock = TestClock::new(0.0);
    
    // First request
    let result1 = limiter.check_request("client1").unwrap();
    assert!(result1.allowed);
    
    // Advance time and test again
    clock.advance(1.0);
    let result2 = limiter.check_request("client1").unwrap();
    assert!(result2.allowed);
}
```

### Error Testing


Test error conditions using `TestClock` failure simulation:

```rust
#[test]

fn test_clock_error_handling() {
    let clock = TestClock::new(0.0);
    let limiter = FluxLimiter::with_config(config, clock.clone()).unwrap();
    
    clock.fail_next_call();
    let result = limiter.check_request("client1");
    assert!(result.is_err());
}
```

### Running Tests


```bash
# Run all tests

cargo test

# Run specific test module

cargo test gcra_algorithm_tests

# Run with output

cargo test -- --nocapture

# Run performance tests

cargo test performance_tests

# Test with different client ID types

cargo test -- --test-threads=1  # For timing-sensitive tests
```

## Documentation


### Documentation Requirements


1. **API Documentation**:
   - All public items must have rustdoc comments
   - Include usage examples
   - Document error conditions
   - Explain performance characteristics

2. **README Updates**:
   - Update examples for new features
   - Keep README concise - detailed docs go in mdbook
   - Link to appropriate mdbook sections

3. **mdbook Documentation**:
   - Update relevant sections for new features
   - Add examples to appropriate chapters
   - Keep architecture docs current

4. **CHANGELOG**:
   - Follow [Keep a Changelog]https://keepachangelog.com/ format
   - Document breaking changes
   - Include migration guidance

### Documentation Commands


```bash
# Generate and open API documentation

cargo doc --open

# Check for missing documentation

cargo doc --document-private-items

# Test documentation examples

cargo test --doc

# Build mdbook documentation (requires mdbook: cargo install mdbook)

mdbook build docs

# Serve mdbook locally with live reload

mdbook serve docs --open
```

## Performance Considerations


### Performance Requirements


- `check_request()` should complete in O(1) time
- Memory usage should be O(number of active clients)
- Lock-free operations preferred for hot paths
- Nanosecond precision must be maintained

### Benchmarking


When making performance-related changes:

1. **Before/After Benchmarks**:
   ```bash
   # Create simple benchmark

   cargo test performance_tests --release -- --nocapture

   ```

2. **Memory Usage**:
   - Monitor memory growth in long-running tests
   - Test cleanup effectiveness

3. **Concurrency**:
   - Test with multiple threads
   - Verify lock-free behavior

### Performance Guidelines


- Avoid allocations in `check_request()`
- Use efficient data structures (DashMap vs HashMap)
- Minimize atomic operations
- Consider cache locality for hot data

## Submitting Changes


### Pull Request Process


1. **Before Creating a PR**:
   - Ensure all tests pass: `cargo test`
   - Run formatting: `cargo fmt`
   - Fix clippy warnings: `cargo clippy -- -D warnings`
   - Update documentation if needed
   - Add tests for new functionality

2. **Creating the PR**:
   - Use descriptive title and description
   - Reference related issues
   - Include test results
   - Explain design decisions for complex changes

3. **PR Description Template**:
   ```markdown
   ## Summary
   Brief description of changes
   
   ## Changes Made
   - List of specific changes
   - New features added
   - Bugs fixed
   
   ## Testing
   - Tests added/modified
   - Manual testing performed
   
   ## Breaking Changes
   - List any breaking changes
   - Migration guidance
   
   ## Related Issues
   Fixes #123
   ```

### Review Process


1. **Automated Checks**: CI will run tests, formatting, and linting
2. **Code Review**: Maintainers will review code quality and design
3. **Testing**: Verify tests are comprehensive and passing
4. **Documentation**: Check that documentation is updated appropriately

## Release Process


### Versioning


We follow [Semantic Versioning](https://semver.org/):
- **MAJOR**: Incompatible API changes
- **MINOR**: New functionality (backward compatible)  
- **PATCH**: Bug fixes (backward compatible)

### Release Checklist


1. Update version in `Cargo.toml`
2. Update `CHANGELOG.md`
3. Ensure all tests pass
4. Update documentation
5. Create release tag
6. Publish to crates.io (maintainers only)

## Getting Help


- **Questions**: Open a discussion or issue on GitHub
- **Bugs**: Create an issue with reproduction steps
- **Feature Requests**: Open an issue with detailed description
- **Security Issues**: Contact maintainers privately

## Areas for Contribution


We welcome contributions in these areas:

### High Priority

- **Algorithm Optimizations**: Improve performance while maintaining correctness
- **Error Handling**: Enhance robustness and error recovery
- **Documentation**: Improve examples and guides
- **Testing**: Add more comprehensive test coverage

### Medium Priority

- **Platform Support**: Testing on different platforms
- **Integration Examples**: More framework integrations
- **Monitoring**: Better observability features
- **Configuration**: Additional configuration options

### Future Enhancements

- **Distributed Rate Limiting**: Multi-node coordination
- **Persistence**: Optional state persistence
- **Alternative Algorithms**: Support for other rate limiting algorithms
- **Async Support**: Native async/await integration

Thank you for contributing to Flux Limiter! Your contributions help make rate limiting more reliable and accessible for the Rust community.