rust-rabbit
A simple and reliable RabbitMQ client library for Rust. Easy to use with minimal configuration and flexible retry mechanisms.
Key Features
- Simple API with just Publisher and Consumer
- Flexible retry mechanisms: exponential, linear, or custom delays
- Automatic queue and exchange declaration
- Built-in reliability with intelligent error handling
- MassTransit integration for C# interoperability
- Production-ready with persistent messages and proper ACK handling
Quick Start
Installation
Add to your Cargo.toml:
[]
= "1.2"
= { = "1.0", = ["full"] }
= { = "1.0", = ["derive"] }
Basic Publisher Example
use ;
use Serialize;
async
Basic Consumer Example
use ;
use ;
async
Documentation
User Guides
Comprehensive guides for common use cases and patterns:
| Guide | Description |
|---|---|
| Retry Configuration Guide | Learn about retry mechanisms, delay strategies, and DLQ configuration |
| Queues and Exchanges Guide | Understanding queue binding, exchange types, and routing patterns |
| Error Handling Guide | Error types, classification, and recovery strategies |
| Best Practices Guide | Production patterns, performance optimization, and operational tips |
API Reference
Full API documentation is available at docs.rs/rust-rabbit.
Examples
See the examples/ directory for complete working examples:
- basic_publisher.rs - Simple message publishing
- basic_consumer.rs - Simple message consumption
- retry_examples.rs - Different retry configurations
- delayed_exchange_example.rs - Using delayed exchange plugin
- dlq_ttl_example.rs - Auto-cleanup DLQ with TTL
- masstransit_option_example.rs - MassTransit integration
- production_setup.rs - Production-ready configuration
Core Concepts
Retry Mechanisms
rust-rabbit provides flexible retry mechanisms for handling message processing failures:
use RetryConfig;
use Duration;
// Exponential backoff: 1s, 2s, 4s, 8s, 16s
let exponential = exponential_default;
// Custom exponential with base and max delay
let custom_exp = exponential;
// Linear retry: same delay for each attempt
let linear = linear;
// Custom delays for each retry
let custom = custom;
// No retries
let no_retry = no_retry;
See the Retry Configuration Guide for detailed information.
Delay Strategies
Two strategies for implementing message delays:
TTL Strategy (Default)
- Uses RabbitMQ's TTL feature
- No plugin required
- Works out-of-the-box
DelayedExchange Strategy
- Uses rabbitmq_delayed_message_exchange plugin
- More precise timing
- Better for high-volume scenarios
- Requires plugin installation
use ;
// TTL strategy (default)
let config = exponential_default
.with_delay_strategy;
// Delayed exchange strategy (requires plugin)
let config = exponential_default
.with_delay_strategy;
See the Retry Configuration Guide for setup instructions.
Dead Letter Queue
Failed messages that exceed max retries are automatically sent to a Dead Letter Queue. You can configure automatic cleanup:
let retry_config = exponential_default
.with_dlq_ttl; // Auto-cleanup after 1 day
let consumer = builder
.with_retry
.build;
MassTransit Integration
rust-rabbit seamlessly integrates with C# services using MassTransit.
Publishing to MassTransit services:
use PublishOptions;
publisher.publish_to_exchange.await?;
Consuming MassTransit messages:
Messages published by MassTransit are automatically detected and unwrapped. Your handler receives just the payload:
consumer.consume.await?;
Access envelope metadata:
Use consume_envelopes() to access correlation IDs, timestamps, and other metadata:
use MessageEnvelope;
consumer.consume_envelopes.await?;
Publisher Options
use PublishOptions;
let options = new
.mandatory
.priority;
publisher.publish_to_queue.await?;
Consumer Configuration
let consumer = builder
.with_retry
.bind_to_exchange
.with_prefetch
.build;
Requirements
- Rust 1.70 or higher
- RabbitMQ 3.8 or higher
- Tokio async runtime
Testing
Run the tests:
For integration tests with real RabbitMQ:
# Start RabbitMQ with Docker
# Run tests
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome. Please read our contributing guide and submit pull requests.
Support
- Issues: GitHub Issues
- Documentation: docs.rs