Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add documentation for component server #222

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/mempool_infra/src/component_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@ use crate::component_definitions::{
ComponentRequestAndResponseSender, ServerError, APPLICATION_OCTET_STREAM,
};

/// The `ComponentClient` struct is a generic client for sending component requests and receiving
/// responses asynchronously.
///
/// # Type Parameters
/// - `Request`: The type of the request. This type must implement both `Send` and `Sync` traits.
/// - `Response`: The type of the response. This type must implement both `Send` and `Sync` traits.
///
/// # Fields
/// - `tx`: An asynchronous sender channel for transmitting
/// `ComponentRequestAndResponseSender<Request, Response>` messages.
///
/// # Example
/// ```rust
/// // Example usage of the ComponentClient
/// use tokio::sync::mpsc::Sender;
///
/// use crate::starknet_mempool_infra::component_client::ComponentClient;
/// use crate::starknet_mempool_infra::component_definitions::ComponentRequestAndResponseSender;
///
/// // Define your request and response types
/// struct MyRequest {
/// pub content: String,
/// }
///
/// struct MyResponse {
/// content: String,
/// }
///
/// #[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 client.
/// let client = ComponentClient::new(tx);
///
/// // Instantiate a request.
/// let request = MyRequest { content: "Hello, world!".to_string() };
///
/// // Send the request; typically, the client should await for a response.
/// client.send(request);
/// }
/// ```
///
/// # Notes
/// - The `ComponentClient` struct is designed to work in an asynchronous environment, utilizing
/// Tokio's async runtime and channels.
pub struct ComponentClient<Request, Response>
where
Request: Send + Sync,
Expand Down
99 changes: 99 additions & 0 deletions crates/mempool_infra/src/component_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,105 @@ use crate::component_definitions::{
};
use crate::component_runner::ComponentStarter;

/// 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 runs
/// indefinitely.
///
/// # 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 starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter};
/// use tokio::task;
///
/// use crate::starknet_mempool_infra::component_definitions::{
/// ComponentRequestAndResponseSender, ComponentRequestHandler,
/// };
/// use crate::starknet_mempool_infra::component_server::{
/// ComponentServer, ComponentServerStarter,
/// };
///
/// // Define your component
/// struct MyComponent {}
///
/// #[async_trait]
/// impl ComponentStarter for MyComponent {
/// async fn start(&mut self) -> Result<(), ComponentStartError> {
/// Ok(())
/// }
/// }
///
/// // 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;
/// });
///
/// // Ensure the server starts running.
/// task::yield_now().await;
///
/// // Create the request and the response channel.
/// let (res_tx, mut res_rx) = tokio::sync::mpsc::channel::<MyResponse>(1);
/// let request = MyRequest { content: "request example".to_string() };
/// let request_and_res_tx = ComponentRequestAndResponseSender { request, tx: res_tx };
///
/// // Send the request.
/// tx.send(request_and_res_tx).await.unwrap();
///
/// // Receive the response.
/// let response = res_rx.recv().await.unwrap();
/// assert!(response.content == "request example processed".to_string(), "Unexpected response");
/// }
/// ```
pub struct ComponentServer<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response> + ComponentStarter,
Expand Down
Loading