manager_handlers
A scalable, async-driven microservice framework for Rust that enables dynamic handler registration with HTTP endpoints and internal pub/sub messaging.
Overview
manager_handlers is built on top of Actix Web and Tokio, providing a robust foundation for building microservice architectures. It allows you to create handlers that process HTTP requests and communicate with each other through an internal message bus, making it highly modular and scalable.
Features
- 🚀 Dynamic Handler Registration: Register handlers at runtime with configurable replica counts for horizontal scaling
- 📬 Dual Communication: Internal pub/sub messaging bus + Redis pub/sub for distributed systems
- 🔒 Security First: Built-in TLS support with optional client certificate authentication
- 📁 File Operations: Streaming file upload/download with metadata support
- 🎯 Concurrency Control: Semaphore-based request limiting with per-handler concurrency settings
- 💾 Shared State: Thread-safe state management with support for primitives and function pointers
- 🔄 Async by Design: Built on Tokio for high-performance async I/O operations
- ⚡ Zero-Copy Streaming: Efficient file handling without loading entire files into memory
- 🛑 Graceful Shutdown: Coordinated service termination with cleanup
Requirements
- Rust 1.85+ (2024 edition)
- Tokio runtime
- OpenSSL (for TLS support)
Installation
Add this to your Cargo.toml:
[]
= "0.7.0"
= "0.1"
Or use cargo:
Quick Start
use Manager;
use handler;
use async_trait;
// Define a simple handler using the macro
handler!;
async
Core Concepts
Handlers
Handlers are the core processing units that:
- Process incoming HTTP requests
- Communicate with other handlers via message bus
- Access shared state
- Handle file operations
Message Bus
The internal MultiBus provides:
- Async message passing between handlers
- Request/response pattern with
publish() - Fire-and-forget pattern with
dispatch() - Backpressure and timeout handling
Shared State
Thread-safe storage supporting:
- Primitive types (Int, Float, String, etc.)
- Synchronous and async function pointers
- Custom types via
AnyTypewrapper
Usage Examples
Creating Custom Handlers
Implement the Base trait for full control:
use async_trait;
use Arc;
use ;
use Duration;
use sleep;
use MultiBus;
use ;
;
Using the Handler Macro
For simpler handlers, use the handler! macro:
use handler;
// Simple handler
handler!;
// Handler with state access
handler!;
Manager Configuration
use Manager;
use HashMap;
async
Advanced Features
Redis Integration
Enable distributed pub/sub with Redis:
// Configure Redis URL
manager.with_redis_url;
// In your handler, use Redis pub/sub
let response = self.publish_redis.await;
// Subscribe to Redis topics
let request = self.subscribe_topic_redis.await?;
File Operations
Implement file handling capabilities:
-
UploadHandler: Implement this trait to customize how files are uploaded and stored.
-
DownloadHandler: Implement this trait to customize how files are downloaded.
-
MetadataHandler: Implement this trait to customize how file metadata is retrieved.
API Reference
HTTP Endpoints
Handler Endpoints
POST /{handler_name}
Send a request to a registered handler.
Example:
File Operations
Upload a File: POST /stream/upload/{file_name}
Upload files to the server.
Example:
Download a File: GET /stream/download/{file_id}
Download a previously uploaded file.
Example:
Retrieve File Metadata: GET /stream/metadata/{file_id}
Get metadata for a file.
Example:
System Management
Shutdown Server: POST /shutdown
Gracefully shut down the server.
Example:
Security
Authentication Methods
-
API Key Authentication
- Set via
with_api_key() - Pass as Bearer token in Authorization header
- Set via
-
TLS/SSL Support
- Configure with
with_tls() - Optional client certificate verification
- Configure with
-
Client Certificate Authentication
- Specify allowed certificate names with
with_allowed_names() - Requires CA certificate configuration
- Specify allowed certificate names with
Example Security Configuration
let mut manager = new_default;
// Enable API key authentication
manager.with_api_key;
// Configure TLS with client certificates
manager.with_tls;
// Allow specific client certificates
manager.with_allowed_names;
Performance Tuning
Concurrency Settings
// Limit total concurrent HTTP requests
manager.with_max_requests;
// Configure handler replicas for load distribution
manager.;
// Set keep-alive for connection reuse
manager.with_keep_alive;
Resource Limits
- Maximum payload size: 10 GiB
- Maximum JSON size: 1 GiB
- Default request timeout: 120 seconds
- WebSocket ping interval: 10 seconds
Error Handling
The framework provides comprehensive error handling:
// Handler errors are automatically caught and returned
async
Common Error Responses
// Handler not found
// Authentication failure
// Internal error
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Matei Aruxandei - stefmatei22@gmail.com