Skip to content

Commit

Permalink
docs: add documentation for component server
Browse files Browse the repository at this point in the history
commit-id:a2509b74
  • Loading branch information
Itay-Tsabary-Starkware committed Jun 20, 2024
1 parent f00b4a3 commit 6f5765a
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions crates/mempool_infra/src/component_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentRequestAndResponseSender<Request,
/// Response>>`.
///
/// # 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<MyRequest, MyResponse> 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<MyRequest, MyResponse>,
/// >(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<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response>,
Expand Down

0 comments on commit 6f5765a

Please sign in to comment.