Manager Crate
Overview
Manager is a scalable, async-driven system that handles requests and communication between different components using a pub/sub model in Rust. It is built on top of the Rocket framework for HTTP server handling and utilizes the Tokio runtime for asynchronous tasks. This crate allows you to create handlers that process requests and handle messages via a message bus, making it highly modular and easy to extend.
Features
- Dynamic Handler Registration: Register new handlers dynamically that can process requests and publish messages.
- Pub/Sub Messaging: Implement publish/subscribe messaging between different services or components.
- Concurrency Control: Uses a semaphore to limit to one request at the time from http request. The handlers inside the service can replicate as many times as they are configured.
- Graceful Shutdown: Includes an HTTP shutdown mechanism for controlled termination of the service.
- Asynchronous Processing: All handlers and requests are processed asynchronously using Tokio's async runtime.
Prerequisites
Before you begin, ensure you have met the following requirements:
- Rust version >= 1.56 (due to the use of the
async_traitcrate and async/await syntax). Rocketversion 0.5 or later.
Installation
To use this crate in your project, add the following dependencies to your Cargo.toml file:
[]
= { = "0.5", = ["json"] }
= { = "1", = ["full"] }
= "0.1"
= "1.0"
= { = "1.0", = ["derive"] }
How to Use
Define a Handler
To create a custom handler, you need to implement the Base trait. The Base trait requires two functions:
run: Handles the actual business logic of processing incoming messages.create: Handles the creation of the struct
It also gives two ways to communicate between handlers:
publish: Publishes messages to other components via theMultiBusand awaits the response.dispatch: Dispatches messages to another service or handler.
use async_trait;
use Arc;
use MultiBus;
;
Create and Start the Manager
The Manager is responsible for initializing all the handlers and launching the HTTP server. Add your custom handlers to the manager using add_handler.
use Manager;
async
HTTP Endpoints
This crate provides a couple of HTTP endpoints that allow interaction with the system.
-
POST
/shutdown: Shuts down the server gracefully.Example:
-
POST
/handler_name: Sends a request to a registered handler with a string body. You can after that process into a JSON.Example:
Error Handling
Errors during request processing or message dispatching are handled gracefully, and appropriate error messages are returned via JSON. If a handler encounters an error, it logs the issue and returns an error message.
Example Error Response
License
This crate is open-sourced under the MIT license.