Expand description
Metered channels with Prometheus metrics integration.
This crate provides channel implementations that combine Tokio’s asynchronous channels with Prometheus metrics integration. The channels support proper backpressure through permit-based operations and comprehensive metrics tracking.
§Features
- Prometheus metrics integration for monitoring channel behavior
- Cancel-safe permit operations for reliable backpressure handling
- Multiple channel types (mpsc, broadcast, watch) with consistent interfaces
- Comprehensive error handling with detailed error types
- Full test coverage ensuring reliability
§Channel Types
mpsc_channel: Multi-producer, single-consumer channel with metricsbroadcast_channel: Multi-producer, multi-consumer broadcast channelwatch_channel: Single-producer, multi-consumer watch channel
§Example
use tokio_prometheus_metered_channel::{mpsc_channel, ChannelMetrics};
use prometheus::Registry;
#[tokio::main]
async fn main() {
// Create a new registry and metrics
let registry = Registry::new();
let metrics = ChannelMetrics::new_basic("example", "example channel", ®istry).unwrap();
// Create a channel with capacity 10
let (tx, mut rx) = mpsc_channel(10, metrics);
// Send a value
tx.send(42).await.unwrap();
// Receive the value
let value = rx.recv().await.unwrap();
assert_eq!(value, 42);
}§Credits
This implementation is inspired by and builds upon work from:
- Mysten Labs’ Narwhal project
- Diem’s channel implementations
Re-exports§
Modules§
- broadcast
- Broadcast channel implementation with prometheus metrics integration.
- prelude
- Re-exports of commonly used types
- watch
- Watch channel implementation with prometheus metrics integration.
Structs§
- Channel
Metrics - Metrics for channel monitoring
- Mpsc
Receiver - A receiver handle to a channel
- Mpsc
Sender - A sender handle to a channel
Enums§
- Send
Error - Error type for channel operations.
Traits§
- With
Permit - Trait for types that support permit-based sending
Functions§
- mpsc_
channel - Creates a new channel with the given buffer size and metrics
- mpsc_
channel_ with_ total - Creates a new channel with total message counting