DropSlot
A high-performance publish-subscribe library with latest-only delivery semantics for Rust. Built on top of Tokio with zero-copy operations and optimized for both high throughput and low latency scenarios.
โจ Key Features
- Latest-only delivery: Subscribers receive only the most recent message, perfect for real-time applications
- Zero-copy operations: Optimized for
bytes::Bytesand other efficient data types - String-keyed topics: Simple and intuitive topic naming system
- High performance: Optimized data structures, memory layout, and CPU cache utilization
- Async/sync APIs: Both
asyncand non-blocking synchronous operations - Thread-safe: Built with concurrent access in mind using lock-free data structures
- Memory efficient: Weak references prevent memory leaks with manual cleanup available
๐ Quick Start
Add dropslot to your Cargo.toml:
[]
= "0.2"
Basic Usage
use *;
use Bytes;
async
Performance Configurations
use *;
use Bytes;
// High throughput: optimized for many topics (large capacity)
let ht_bus = with_capacity;
// Low latency: optimized for few topics (small capacity)
let ll_bus = with_capacity;
// Custom capacity
let custom_bus = with_capacity;
๐ Performance
DropSlot is designed for high-performance scenarios and delivers exceptional performance:
Key Performance Metrics
| Operation | Latency | Throughput | Notes |
|---|---|---|---|
| Topic Creation | ~136 ns | ~7.4M ops/sec | Ultra-fast topic instantiation |
| Message Publishing | ~467 ns | ~2.1M ops/sec | Direct publish to topic |
| Subscriber Creation | ~510 ns | ~2.0M ops/sec | Fast subscriber setup |
| Message Retrieval | ~477 ns | ~2.1M ops/sec | Non-blocking message access |
| Topic Lookup | ~40 ns | ~25M ops/sec | Optimized topic resolution |
| Error Handling | ~330 ps | ~3.0B ops/sec | Near-zero overhead |
Scalability Performance
| Scenario | Performance | Details |
|---|---|---|
| 10 Topics | ~3.6 ฮผs | Excellent small-scale performance |
| 100 Topics | ~43 ฮผs | Linear scaling maintained |
| 1000 Topics | ~458 ฮผs | Consistent performance at scale |
| High Frequency | ~11 ฮผs/batch | 1000 message batches |
| Concurrent (16 threads) | ~3.3 ms | Excellent multi-threaded performance |
Memory & Concurrency
- Memory cleanup: ~3.3 ฮผs for unused topic cleanup
- Concurrent publishing: Linear scaling up to 16 threads
- Zero-copy operations: ~612 ns for
bytes::Byteshandling - Topic management: ~529 ns for topic removal operations
Architecture Optimizations
- Lock-free concurrent access using
DashMap - Optimized hashing with
AHash - CPU cache-friendly memory layout and prefetching
- Zero-copy operations for byte data
- Efficient memory management with
Arcand weak references
Benchmarks run on CI environment with optimized builds. Your mileage may vary based on hardware and workload.
๐ง Advanced Usage
Custom Message Types
use *;
use ;
let bus = new;
let topic = bus.topic;
let event = Event ;
topic.publish;
Error Handling
use *;
let bus = new;
let topic = bus.topic;
let mut subscriber = topic.subscribe;
match subscriber.try_get_message
Multiple Subscribers
use *;
let bus = new;
let topic = bus.topic;
// Multiple subscribers to the same topic
let mut email_sub = topic.subscribe;
let mut sms_sub = topic.subscribe;
let mut push_sub = topic.subscribe;
// All subscribers receive the same (latest) message
topic.publish;
Topic Management
use *;
let bus = new;
// Check topic count
println!;
// Get all topic names
let names = bus.topic_names;
println!;
// Manually clean up unused topics (no automatic cleanup)
let removed = bus.cleanup_unused_topics;
println!;
๐ฏ Use Cases
DropSlot is perfect for:
- Real-time notifications (email, SMS, push notifications)
- Live data feeds (stock prices, sensor data, metrics)
- Event sourcing with latest-state semantics
- Microservice communication for status updates
- Game state synchronization
- IoT device coordination
๐ฉ Architecture
Core Components
Bus<T>: Main message broker managing topicsTopic<T>: Individual message topics with publishers and subscribersSub<T>: Subscriber receiving messages from topicsBusError: Unified error handling
Design Principles
- Latest-only semantics: Built on
tokio::sync::watchchannels - Memory safety: Extensive use of
ArcandWeakreferences - Performance first: Optimized data structures and algorithms
- Zero-copy where possible: Efficient handling of byte data
๐ ๏ธ Features
Prelude
For convenience, you can import all commonly used types with the prelude:
use *; // Imports Bus, Topic, Sub, and BusError
Default Features
bytes- Zero-copy operations forbytes::Bytes
Optional Features
serde- Serialization support for complex message types
Enable features in your Cargo.toml:
[]
= { = "0.2", = ["serde"] }
๐ Benchmarks
Run benchmarks with:
๐งช Testing
Run tests with:
Run examples:
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
๐ Changelog
See CHANGELOG.md for recent changes.
Note: This library implements latest-only delivery semantics, meaning subscribers only receive the most recent message. Topic cleanup is manual via bus.cleanup_unused_topics() - call this periodically in long-running applications to prevent memory leaks. For all-message delivery, consider using tokio::sync::broadcast or similar alternatives.