Skip to content

abhiyana/tokio-events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tokio-events

A modern, type-safe async event bus for Rust applications built on Tokio.

image

Quick Example

// Create an event bus
let bus = EventBus::builder()
    .with_metrics()
    .build();

// Subscribe to events
bus.subscribe(|event: UserRegistered| async {
    println!("New user: {}", event.email);
}).await?;

// Publish events
bus.publish(UserRegistered {
    id: 123,
    email: "user@example.com".into(),
}).await?;

Core Components

1. EventBus – The Main API

  • Single instance shared across your application
  • Provides publish() and subscribe() methods
  • Manages component lifecycle

2. Registry – Type Mapping

  • Maps event types to their subscribers
  • Uses TypeId for type-safe routing
  • Thread-safe using DashMap

3. Dispatcher – Event Queue

  • MPSC channel for queuing events
  • Worker thread processes events
  • Configurable queue size and backpressure

4. Subscription Manager – Handler Execution

  • Stores active handlers
  • Spawns async tasks for parallel execution
  • Manages subscription lifecycle

image

publish(UserRegistered) 
    ↓
Create EventEnvelope (type erasure)
    ↓
Send to channel (Arc wrap)
    ↓
Worker receives
    ↓
Lookup by TypeId → Find handlers
    ↓
Spawn tasks (parallel)
    ↓
Each handler:
  - Downcast to UserRegistered
  - Execute user function
  - Update metrics
    ↓
Cleanup (Arc refcount → 0)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages