Crate rust_rabbit

Crate rust_rabbit 

Source
Expand description

§rust-rabbit 🐰

A simple, reliable RabbitMQ client library for Rust. Focus on core functionality with minimal configuration.

§Features

  • 🚀 Simple API: Just Publisher and Consumer with essential methods
  • 🔄 Flexible Retry: Exponential, linear, or custom retry mechanisms
  • 🛠️ Auto-Setup: Automatic queue/exchange declaration and binding
  • ⚡ Built-in Reliability: Default ACK behavior with error handling

§Quick Start

§Publisher

use rust_rabbit::{Connection, Publisher, PublishOptions};
use serde::Serialize;

#[derive(Serialize)]
struct Order {
    id: u32,
    amount: f64,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let connection = Connection::new("amqp://localhost:5672").await?;
    let publisher = Publisher::new(connection);
     
    let order = Order { id: 123, amount: 99.99 };
     
    // Publish to exchange
    publisher.publish_to_exchange("orders", "new.order", &order, None).await?;
     
    // Publish directly to queue
    publisher.publish_to_queue("order_queue", &order, None).await?;
     
    Ok(())
}

§Consumer with Retry

use rust_rabbit::{Connection, Consumer, RetryConfig};
use serde::Deserialize;

#[derive(Deserialize)]
struct Order {
    id: u32,
    amount: f64,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let connection = Connection::new("amqp://localhost:5672").await?;
     
    let consumer = Consumer::builder(connection, "order_queue")
        .retry(RetryConfig::exponential_default()) // 1s->2s->4s->8s->16s
        .bind_to_exchange("orders")
        .concurrency(5)
        .build()
        .await?;
     
    consumer.consume(|order: Order| async move {
        println!("Processing order {}: ${}", order.id, order.amount);
        // Your business logic here
        Ok(()) // ACK message
    }).await?;
     
    Ok(())
}

§Retry Configurations

use rust_rabbit::RetryConfig;
use std::time::Duration;

// Exponential: 1s -> 2s -> 4s -> 8s -> 16s (5 retries)
let exponential = RetryConfig::exponential_default();

// Custom exponential: 2s -> 4s -> 8s -> 16s -> 32s (with cap at 60s)
let custom_exp = RetryConfig::exponential(5, Duration::from_secs(2), Duration::from_secs(60));

// Linear: 10s -> 10s -> 10s (3 retries)  
let linear = RetryConfig::linear(3, Duration::from_secs(10));

// Custom delays: 1s -> 5s -> 30s
let custom = RetryConfig::custom(vec![
    Duration::from_secs(1),
    Duration::from_secs(5),
    Duration::from_secs(30),
]);

// No retries
let no_retry = RetryConfig::no_retry();

Modules§

prelude
Prelude module for convenient imports

Structs§

Connection
Simple RabbitMQ connection wrapper
ConnectionBuilder
Connection builder for fluent configuration
ConnectionConfig
Simple connection configuration
Consumer
Simplified Consumer for message consumption
ConsumerBuilder
Consumer configuration builder
Message
Message wrapper with retry tracking
PublishOptions
Publish options builder
Publisher
Simplified Publisher for message publishing
RetryConfig
Simple retry configuration

Enums§

RetryMechanism
Retry mechanism configuration
RustRabbitError
Main error type for rust-rabbit library

Type Aliases§

Result
Result type alias for convenience