Request Coalescer
A Rust library for coalescing identical asynchronous operations to prevent redundant work in concurrent systems.
Overview
request_coalescer provides a CoalescingService that prevents redundant work in concurrent asynchronous systems by ensuring that the underlying expensive operation is executed only once when multiple requests for the same "key" arrive simultaneously.
This is particularly useful for:
- Deduplicating database reads or API calls for identical resources
- Throttling access to rate-limited external services
- Optimizing expensive computations
- Reducing load on downstream services
Features
- Asynchronous: Built on Tokio for efficient async operation
- Key-based Coalescing: Deduplicate operations based on any hashable key
- Optional Timeouts: Set global or per-operation timeouts
- Detailed Statistics: Track coalesced, failed, and timed-out operations
- Error Handling: Propagate errors to all waiting clients
- Configurable: Customize behavior with various configuration options
Installation
Add the following to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] } # Or specific features
= "1"
# Optional for logging/tracing:
# tracing = "0.1"
# tracing-subscriber = "0.3" # For development/examples only
Basic Usage
Here's a simple example of how to use the CoalescingService:
use CoalescingService;
use Arc;
use Result;
async
async
Advanced Usage
Custom Configuration
You can customize the CoalescingService with various configuration options:
use ;
use Duration;
async
Timeout Handling
The library provides built-in timeout handling:
use CoalescingService;
use Duration;
async
API Documentation
For detailed API documentation, please run cargo doc --open or visit docs.rs (once published).
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License or Apache License 2.0, at your option.
Acknowledgments
- This library is built on top of the Tokio async runtime.
- Thanks to the Rust community for providing excellent tools and libraries that made this project possible.