Async Duplex Channel Library
This library provides an asynchronous duplex communication channel between multiple clients and a single responder in different asynchronous blocks. It enables multiple clients to communicate with a single responder, allowing a client to send requests and receive responses, while the responder processes requests and sends back responses. It is designed for high-concurrency scenarios and supports both single and batch requests, as well as configurable timeouts and concurrency strategies.
Key Features
- Asynchronous Communication: Built on top of
tokioandfutures, enabling non-blocking I/O. - Thread Safety: Uses
ArcandMutexto ensure thread-safe communication. - Flexible Concurrency: Supports both fixed and dynamic concurrency strategies for request processing.
- Batch Requests: Allows sending multiple requests in a batch with configurable concurrency.
- Configurable Timeouts: Provides customizable timeouts for requests and responses.
- Error Handling: Detailed error types for easy debugging and error recovery.
Usage
Basic Example
use ;
use Duration;
use Runtime;
Batch Requests Example
use ;
use Duration;
use Runtime;
Dynamic Concurrency Example
use ;
use Duration;
use Runtime;
Error Handling
The library provides detailed error types through the Error enum, which covers common failure
scenarios such as send failures, receive failures, timeouts, and internal errors. Each error variant
includes a descriptive message for easy debugging.
Concurrency Strategies
The responder supports two concurrency strategies:
- Fixed Concurrency: Processes a fixed number of requests concurrently.
- Dynamic Concurrency: Adjusts the concurrency level based on response times, ensuring optimal resource utilization under varying workloads.
Performance Considerations
- Use
request_batchfor high-throughput scenarios to reduce overhead. - Choose the appropriate concurrency strategy based on your workload characteristics.
- Monitor and adjust timeouts to balance responsiveness and resource usage.
Dependencies
tokio: For asynchronous runtime support.futures: For future and stream utilities.sharded_slab: For efficient storage of pending requests.thiserror: For ergonomic error handling.
License
This library is licensed under the MIT License. See the LICENSE file for details.