Skip to content

Commit

Permalink
feat: adding component start(initialization) to the server start
Browse files Browse the repository at this point in the history
commit-id:77f186d4
  • Loading branch information
lev-starkware committed Jul 11, 2024
1 parent 6f0ced5 commit 2a9c292
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crates/mempool/src/communication.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use starknet_mempool_infra::component_definitions::ComponentRequestHandler;
use starknet_mempool_infra::component_runner::ComponentRunner;
use starknet_mempool_infra::component_server::ComponentServer;
use starknet_mempool_types::communication::{
MempoolRequest, MempoolRequestAndResponseSender, MempoolResponse,
Expand Down Expand Up @@ -52,3 +53,6 @@ impl ComponentRequestHandler<MempoolRequest, MempoolResponse> for MempoolCommuni
}
}
}

#[async_trait]
impl ComponentRunner for MempoolCommunicationWrapper {}
6 changes: 4 additions & 2 deletions crates/mempool_infra/src/component_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum ComponentStartError {
/// Interface to start components.
#[async_trait]
pub trait ComponentRunner {
/// Start the component. Normally this function should never return.
async fn start(&mut self) -> Result<(), ComponentStartError>;
/// Start the component. By default do nothing.
async fn start(&mut self) -> Result<(), ComponentStartError> {
Ok(())
}
}
14 changes: 11 additions & 3 deletions crates/mempool_infra/src/component_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::component_runner::ComponentRunner;

pub struct ComponentServer<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response>,
Component: ComponentRequestHandler<Request, Response> + ComponentRunner,
Request: Send + Sync,
Response: Send + Sync,
{
Expand All @@ -31,7 +31,7 @@ where

impl<Component, Request, Response> ComponentServer<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response>,
Component: ComponentRequestHandler<Request, Response> + ComponentRunner,
Request: Send + Sync,
Response: Send + Sync,
{
Expand All @@ -52,11 +52,19 @@ pub trait ComponentServerStarter: Send + Sync {
impl<Component, Request, Response> ComponentServerStarter
for ComponentServer<Component, Request, Response>
where
Component: ComponentRequestHandler<Request, Response> + Send + Sync,
Component: ComponentRequestHandler<Request, Response> + ComponentRunner + Send + Sync,
Request: Send + Sync,
Response: Send + Sync,
{
async fn start(&mut self) {
match self.component.start().await {
Ok(_) => info!("ComponentServer::start() completed."),
Err(err) => {
error!("ComponentServer::start() failed: {:?}", err);
return;
}
}

while let Some(request_and_res_tx) = self.rx.recv().await {
let request = request_and_res_tx.request;
let tx = request_and_res_tx.tx;
Expand Down
9 changes: 8 additions & 1 deletion crates/mempool_infra/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use starknet_mempool_infra::component_client::ClientResult;

use starknet_mempool_infra::component_runner::ComponentRunner;
pub(crate) type ValueA = u32;
pub(crate) type ValueB = u8;

Expand Down Expand Up @@ -55,6 +55,9 @@ impl ComponentA {
}
}

#[async_trait]
impl ComponentRunner for ComponentA {}

pub(crate) struct ComponentB {
value: ValueB,
_a: Box<dyn ComponentAClientTrait>,
Expand All @@ -69,3 +72,7 @@ impl ComponentB {
self.value
}
}

#[async_trait]
impl ComponentRunner for ComponentB {}

0 comments on commit 2a9c292

Please sign in to comment.