Gyre: A Lightweight, High-Performance Publish-Subscribe Library for Tokio
English | 简体中文
Gyre is a high-performance, Disruptor-style event publish-subscribe library built on Tokio. It is designed to provide a low-latency, high-throughput multicast message channel for the Rust asynchronous ecosystem.
Core Concepts
Gyre's design is heavily inspired by the LMAX Disruptor and is built around several core principles:
- Minimalism: The API is extremely simple. Create a
PublisherandConsumerpair withgyre::channel(n), and you're ready to publish and receive events. No complex configurations, no macro magic. - Performance-First: The core data structure is a pre-allocated ring buffer. All coordination is done through atomic operations, avoiding the overhead of traditional locks. Critical state is cache-padded to prevent false sharing.
- Multicast & Independent Consumption: An event can be broadcast to any number of consumers. Each consumer tracks its own progress independently, so a slow consumer won't block faster ones.
- Automatic Backpressure: When a publisher produces faster than the slowest consumer can keep up,
publishcalls will automatically and asynchronously wait until space becomes available in the buffer. This backpressure mechanism is built-in and requires no manual management. - RAII-Driven Progress Management:
consumer.next().awaitreturns anEventGuard. The consumer's cursor only advances when this guard is dropped. This guarantees that an event is considered "consumed" only after it has been fully processed, ensuring message handling integrity at a fundamental level.
Features
- Lightweight: The core API consists of just two main structs:
PublisherandConsumer. - Type-Safe: Leverages Rust's type system to ensure correctness in event delivery.
- High-Performance: Lock-free design, optimized for low-latency and high-throughput scenarios.
- Dynamic Subscription: Consumers can dynamically join the event bus at runtime via
publisher.subscribe()orconsumer.clone(). - Seamless Tokio Integration: Built entirely on Tokio, Gyre integrates naturally into any Tokio-based project.
Quick Start
Let's create a simple single-producer, dual-consumer system.
1. Add Dependencies:
Add gyre and tokio to your Cargo.toml.
[]
= "1"
= { = "1", = ["full"] }
2. Example Code:
use ;
use join;
async
Expected Output: (The order of printed lines may vary due to asynchronous scheduling.)
Publishing: 0
Consumer 1 received: 0
Consumer 2 received: 0
Publishing: 1
Consumer 1 received: 1
Consumer 2 received: 1
Publishing: 2
Consumer 1 received: 2
Consumer 2 received: 2
Contributing
Contributions of any kind are welcome! Whether it's submitting issues, opening pull requests, or improving documentation, we appreciate your help.
License
This project is distributed under either the MIT license or the Apache 2.0 license, at your option: