-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: sort component servers into dedicated files (#523)
- Loading branch information
1 parent
bd34515
commit b4aabe3
Showing
10 changed files
with
161 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use async_trait::async_trait; | ||
use tracing::{error, info}; | ||
|
||
use crate::component_runner::ComponentStarter; | ||
|
||
#[async_trait] | ||
pub trait ComponentServerStarter: Send + Sync { | ||
async fn start(&mut self); | ||
} | ||
|
||
pub async fn start_component<Component>(component: &mut Component) -> bool | ||
where | ||
Component: ComponentStarter + Sync + Send, | ||
{ | ||
if let Err(err) = component.start().await { | ||
error!("ComponentServer::start() failed: {:?}", err); | ||
return false; | ||
} | ||
|
||
info!("ComponentServer::start() completed."); | ||
true | ||
} |
25 changes: 25 additions & 0 deletions
25
crates/mempool_infra/src/component_server/empty_component_server.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use async_trait::async_trait; | ||
|
||
use super::definitions::{start_component, ComponentServerStarter}; | ||
use crate::component_runner::ComponentStarter; | ||
|
||
pub struct EmptyServer<T: ComponentStarter + Send + Sync> { | ||
component: T, | ||
} | ||
|
||
impl<T: ComponentStarter + Send + Sync> EmptyServer<T> { | ||
pub fn new(component: T) -> Self { | ||
Self { component } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<T: ComponentStarter + Send + Sync> ComponentServerStarter for EmptyServer<T> { | ||
async fn start(&mut self) { | ||
start_component(&mut self.component).await; | ||
} | ||
} | ||
|
||
pub fn create_empty_server<T: ComponentStarter + Send + Sync>(component: T) -> EmptyServer<T> { | ||
EmptyServer::new(component) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod definitions; | ||
pub mod empty_component_server; | ||
pub mod local_component_server; | ||
pub mod remote_component_server; |
97 changes: 97 additions & 0 deletions
97
crates/mempool_infra/src/component_server/remote_component_server.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::marker::PhantomData; | ||
use std::net::{IpAddr, SocketAddr}; | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use bincode::{deserialize, serialize}; | ||
use hyper::body::to_bytes; | ||
use hyper::header::CONTENT_TYPE; | ||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::{Body, Request as HyperRequest, Response as HyperResponse, Server, StatusCode}; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::Mutex; | ||
|
||
use super::definitions::ComponentServerStarter; | ||
use crate::component_definitions::{ | ||
ComponentRequestHandler, ServerError, APPLICATION_OCTET_STREAM, | ||
}; | ||
|
||
pub struct RemoteComponentServer<Component, Request, Response> | ||
where | ||
Component: ComponentRequestHandler<Request, Response> + Send + 'static, | ||
Request: for<'a> Deserialize<'a> + Send + 'static, | ||
Response: Serialize + 'static, | ||
{ | ||
socket: SocketAddr, | ||
component: Arc<Mutex<Component>>, | ||
_req: PhantomData<Request>, | ||
_res: PhantomData<Response>, | ||
} | ||
|
||
impl<Component, Request, Response> RemoteComponentServer<Component, Request, Response> | ||
where | ||
Component: ComponentRequestHandler<Request, Response> + Send + 'static, | ||
Request: for<'a> Deserialize<'a> + Send + 'static, | ||
Response: Serialize + 'static, | ||
{ | ||
pub fn new(component: Component, ip_address: IpAddr, port: u16) -> Self { | ||
Self { | ||
component: Arc::new(Mutex::new(component)), | ||
socket: SocketAddr::new(ip_address, port), | ||
_req: PhantomData, | ||
_res: PhantomData, | ||
} | ||
} | ||
|
||
async fn handler( | ||
http_request: HyperRequest<Body>, | ||
component: Arc<Mutex<Component>>, | ||
) -> Result<HyperResponse<Body>, hyper::Error> { | ||
let body_bytes = to_bytes(http_request.into_body()).await?; | ||
let http_response = match deserialize(&body_bytes) { | ||
Ok(component_request) => { | ||
// Acquire the lock for component computation, release afterwards. | ||
let component_response = | ||
{ component.lock().await.handle_request(component_request).await }; | ||
HyperResponse::builder() | ||
.status(StatusCode::OK) | ||
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM) | ||
.body(Body::from( | ||
serialize(&component_response) | ||
.expect("Response serialization should succeed"), | ||
)) | ||
} | ||
Err(error) => { | ||
let server_error = ServerError::RequestDeserializationFailure(error.to_string()); | ||
HyperResponse::builder().status(StatusCode::BAD_REQUEST).body(Body::from( | ||
serialize(&server_error).expect("Server error serialization should succeed"), | ||
)) | ||
} | ||
} | ||
.expect("Response building should succeed"); | ||
|
||
Ok(http_response) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<Component, Request, Response> ComponentServerStarter | ||
for RemoteComponentServer<Component, Request, Response> | ||
where | ||
Component: ComponentRequestHandler<Request, Response> + Send + 'static, | ||
Request: for<'a> Deserialize<'a> + Send + Sync + 'static, | ||
Response: Serialize + Send + Sync + 'static, | ||
{ | ||
async fn start(&mut self) { | ||
let make_svc = make_service_fn(|_conn| { | ||
let component = Arc::clone(&self.component); | ||
async { | ||
Ok::<_, hyper::Error>(service_fn(move |req| { | ||
Self::handler(req, Arc::clone(&component)) | ||
})) | ||
} | ||
}); | ||
|
||
Server::bind(&self.socket.clone()).serve(make_svc).await.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters