Crate tokio_prometheus_metered_channel

Crate tokio_prometheus_metered_channel 

Source
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

§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", &registry).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§

pub use broadcast::channel as broadcast_channel;
pub use watch::channel as watch_channel;

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§

ChannelMetrics
Metrics for channel monitoring
MpscReceiver
A receiver handle to a channel
MpscSender
A sender handle to a channel

Enums§

SendError
Error type for channel operations.

Traits§

WithPermit
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