Skip to content

Commit

Permalink
chore(mempool_node): add component replacer for servers
Browse files Browse the repository at this point in the history
commit-id:f81de1c2
  • Loading branch information
Itay-Tsabary-Starkware committed Sep 27, 2024
1 parent 22749dd commit dacc2d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/mempool_infra/src/component_server/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::any::type_name;

use thiserror::Error;
use tokio::sync::mpsc::Receiver;
use tracing::info;

Expand All @@ -26,3 +27,13 @@ pub async fn request_response_loop<Request, Response, Component>(
tx.send(res).await.expect("Response connection should be open.");
}
}

#[derive(Clone, Debug, Error)]
pub enum ReplaceComponentError {
#[error("Internal error.")]
InternalError,
}

pub trait ComponentReplacer<Component> {
fn replace(&mut self, component: Component) -> Result<(), ReplaceComponentError>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::any::type_name;
use async_trait::async_trait;
use tracing::info;

use crate::component_server::{ComponentReplacer, ReplaceComponentError};
use crate::errors::{ComponentError, ComponentServerError};
use crate::starters::Startable;

Expand All @@ -29,3 +30,10 @@ impl<Component: Startable<ComponentError> + Send + Sync> Startable<ComponentServ
pub fn create_empty_server<Component: Send + Sync>(component: Component) -> EmptyServer<Component> {
EmptyServer::new(component)
}

impl<Component> ComponentReplacer<Component> for EmptyServer<Component> {
fn replace(&mut self, component: Component) -> Result<(), ReplaceComponentError> {
self.component = component;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tracing::error;

use super::definitions::request_response_loop;
use crate::component_definitions::{ComponentRequestAndResponseSender, ComponentRequestHandler};
use crate::component_server::{ComponentReplacer, ReplaceComponentError};
use crate::errors::{ComponentError, ComponentServerError};
use crate::starters::Startable;

Expand Down Expand Up @@ -181,3 +182,16 @@ where
Self { component, rx, _local_server_type: PhantomData }
}
}

impl<Component, Request, Response, LocalServerType> ComponentReplacer<Component>
for BaseLocalComponentServer<Component, Request, Response, LocalServerType>
where
Component: ComponentRequestHandler<Request, Response>,
Request: Send + Sync,
Response: Send + Sync,
{
fn replace(&mut self, component: Component) -> Result<(), ReplaceComponentError> {
self.component = component;
Ok(())
}
}

0 comments on commit dacc2d5

Please sign in to comment.