Event Chains
A flexible and robust event processing pipeline library for Rust with middleware support and configurable fault tolerance.
Features
- Chainable Events - Build event pipelines with a fluent API
- Middleware Support - Wrap events with reusable middleware (logging, timing, auth, etc.)
- Fault Tolerance - Configure how your chain handles failures (strict, lenient, best-effort)
- Flexible Context - Pass typed data between events using a type-safe context
- Zero Cost Abstractions - Efficient execution with minimal overhead
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use EventChain;
use EventContext;
use EventResult;
use FaultToleranceMode;
use ChainableEvent;
// Define your event
;
// Build and execute the chain
Core Concepts
Events
Events are the building blocks of your pipeline. They execute in FIFO order (first added → first executed).
;
Middleware
Middleware wraps around events to add cross-cutting concerns. They execute in LIFO order (last added → first executed).
use EventMiddleware;
;
Execution Order
let chain = new
.middleware // Executes 1st (outermost)
.middleware // Executes 2nd
.event // Executes 3rd
.event; // Executes 4th
Flow:
TimingMiddleware (before)
→ LoggingMiddleware (before)
→ ValidateEvent.execute()
← LoggingMiddleware (after)
← TimingMiddleware (after)
TimingMiddleware (before)
→ LoggingMiddleware (before)
→ ProcessEvent.execute()
← LoggingMiddleware (after)
← TimingMiddleware (after)
Context
Share data between events using the type-safe context:
let mut context = new;
// Set values
context.set;
context.set;
// Get values
let user_id: u64 = context.get.unwrap;
let email: String = context.get.unwrap;
// Check existence
if context.has
Fault Tolerance
Configure how your chain handles failures:
// Strict: Stop execution immediately on first failure (default)
// Semantic Intent: Ensure critical tasks halt when any failure appears.
let chain = new
.event
.event
.with_fault_tolerance;
// Lenient: Continue execution, collect failures for later review
// Semantic Intent: Handle non-critical failures where some operations can fail without compromising the overall goal
let chain = new
.event
.event
.with_fault_tolerance;
// BestEffort: Attempt all events regardless of failures, maximum resilience
// Semantic Intent: Ensure all cleanup/recovery operations are attempted, even if some fail
let chain = new
.event
.event
.with_fault_tolerance;
Complete Example
use EventChain;
use EventContext;
use EventResult;
use FaultToleranceMode;
use ChainableEvent;
use EventMiddleware;
// Events
;
;
;
// Middleware
;
Built-in Middleware
- LoggingMiddleware - Logs event execution with configurable log levels.
- TimingMiddleware - Measures and logs event execution time.
- RetryMiddleware - Retries failed events with configurable backoff strategies.
- MetricsMiddleware - Collects execution statistics for events.
- RateLimitMiddleware - Enforces rate limits on event execution using token bucket algorithm.
- CircuitBreakerMiddleware - Implements the circuit breaker pattern to prevent cascading failures.
Combining Middleware
Middleware can be stacked together. Remember: LIFO execution order (last added executes first).
use *;
let metrics = new;
let chain = new
.middleware // 1st: Collect metrics
.middleware // 2nd: Measure time
.middleware
.middleware
.middleware // 5th: Logging (innermost)
.event;
// Execution flow:
// Metrics → Timing → CircuitBreaker → Retry → Logging → Event
Thread Safety
All middleware is thread-safe and can be shared across threads using Arc:
Use Cases (Non-exhaustive list)
- Request Processing Pipelines - Validation → Authentication → Business Logic → Response
- Data Processing - Extract → Transform → Validate → Load
- Workflow Orchestration - Multi-step business processes with rollback support
- Event Sourcing - Chain of domain events with middleware for logging/persistence
- Plugin Systems - Extensible processing with middleware hooks
License
Licensed under:
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.