From e90209c052577ccfba80fa494770bfc9bec7cd04 Mon Sep 17 00:00:00 2001 From: Itay Tsabary Date: Thu, 6 Jun 2024 12:23:51 +0300 Subject: [PATCH] docs: add documentation for component server commit-id:a2509b74 --- crates/mempool_infra/src/component_server.rs | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/crates/mempool_infra/src/component_server.rs b/crates/mempool_infra/src/component_server.rs index 0a726100e..721f08829 100644 --- a/crates/mempool_infra/src/component_server.rs +++ b/crates/mempool_infra/src/component_server.rs @@ -2,6 +2,80 @@ use tokio::sync::mpsc::Receiver; use crate::component_definitions::{ComponentRequestAndResponseSender, ComponentRequestHandler}; +/// The `ComponentServer` struct is a generic server that handles requests and responses for a +/// specified component. It receives requests, processes them using the provided component, and +/// sends back responses. The server needs to be started using the `start` function, which should be +/// invoked in a different task/thread. +/// +/// # Type Parameters +/// +/// - `Component`: The type of the component that will handle the requests. This type must implement +/// the `ComponentRequestHandler` trait, which defines how the component processes requests and +/// generates responses. +/// - `Request`: The type of requests that the component will handle. This type must implement the +/// `Send` and `Sync` traits to ensure safe concurrency. +/// - `Response`: The type of responses that the component will generate. This type must implement +/// the `Send` and `Sync` traits to ensure safe concurrency. +/// +/// # Fields +/// +/// - `component`: The component responsible for handling the requests and generating responses. +/// - `rx`: A receiver that receives incoming requests along with a sender to send back the +/// responses. This receiver is of type ` Receiver>`. +/// +/// # Example +/// ```rust +/// // Example usage of the ComponentServer +/// use std::sync::mpsc::{channel, Receiver}; +/// +/// use async_trait::async_trait; +/// use tokio::task; +/// +/// use crate::starknet_mempool_infra::component_definitions::{ +/// ComponentRequestAndResponseSender, ComponentRequestHandler, +/// }; +/// use crate::starknet_mempool_infra::component_server::ComponentServer; +/// +/// // Define your component +/// struct MyComponent {} +/// +/// // Define your request and response types +/// struct MyRequest { +/// pub content: String, +/// } +/// +/// struct MyResponse { +/// pub content: String, +/// } +/// +/// // Define your request processing logic +/// #[async_trait] +/// impl ComponentRequestHandler for MyComponent { +/// async fn handle_request(&mut self, request: MyRequest) -> MyResponse { +/// MyResponse { content: request.content.clone() + " processed" } +/// } +/// } +/// +/// #[tokio::main] +/// async fn main() { +/// // Create a channel for sending requests and receiving responses +/// let (tx, rx) = tokio::sync::mpsc::channel::< +/// ComponentRequestAndResponseSender, +/// >(100); +/// +/// // Instantiate the component. +/// let component = MyComponent {}; +/// +/// // Instantiate the server. +/// let mut server = ComponentServer::new(component, rx); +/// +/// // Start the server in a new task. +/// task::spawn(async move { +/// server.start().await; +/// }); +/// } +/// ``` pub struct ComponentServer where Component: ComponentRequestHandler,