From 12244d7a53840c96eb88b85e03ceeb154fa6e60b Mon Sep 17 00:00:00 2001 From: avalonche Date: Sat, 26 Oct 2024 02:52:31 +1100 Subject: [PATCH 1/2] Add health check endpoint --- src/proxy.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/proxy.rs b/src/proxy.rs index 3b1c674..70529a5 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -58,6 +58,11 @@ where } fn call(&mut self, req: HttpRequest) -> Self::Future { + match req.uri().path() { + "/healthz" => return Box::pin(async { Ok(Self::Response::new(HttpBody::from("OK"))) }), + _ => {} + }; + let target_url = self.target_url.clone(); let client = self.client.clone(); let mut inner = self.inner.clone(); @@ -110,6 +115,7 @@ where mod tests { use std::net::SocketAddr; + use http_body_util::BodyExt; use jsonrpsee::{ core::{client::ClientT, ClientError}, http_client::HttpClient, @@ -130,6 +136,7 @@ mod tests { proxy_success().await; proxy_failure().await; does_not_proxy_engine_method().await; + health_check().await; } async fn proxy_success() { @@ -154,6 +161,25 @@ mod tests { assert_eq!(response.unwrap(), "engine response"); } + async fn health_check() { + let proxy_server = spawn_proxy_server().await; + // Create a new HTTP client + let client: Client = + Client::builder(TokioExecutor::new()).build_http(); + + // Test the health check endpoint + let health_check_url = format!("http://{ADDR}:{PORT}/healthz"); + let health_response = client.get(health_check_url.parse::().unwrap()).await; + assert!(health_response.is_ok()); + let b = health_response.unwrap().into_body().collect().await.unwrap().to_bytes(); + // Convert the collected bytes to a string + let body_string = String::from_utf8(b.to_vec()).unwrap(); + assert_eq!(body_string, "OK"); + + proxy_server.stop().unwrap(); + proxy_server.stopped().await; + } + async fn send_request(method: &str) -> Result { let server = spawn_server().await; let proxy_server = spawn_proxy_server().await; From 9bb0d31328769ca0679da8d24bd0abc2f92a617f Mon Sep 17 00:00:00 2001 From: avalonche Date: Sat, 26 Oct 2024 07:30:55 +1100 Subject: [PATCH 2/2] fix build --- src/proxy.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index 70529a5..a1e2156 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -60,6 +60,7 @@ where fn call(&mut self, req: HttpRequest) -> Self::Future { match req.uri().path() { "/healthz" => return Box::pin(async { Ok(Self::Response::new(HttpBody::from("OK"))) }), + "/metrics" => {} _ => {} }; @@ -91,7 +92,10 @@ where message = "received json rpc request for", method = method.method ); - if PROXY_METHODS.iter().any(|&m| method.method.starts_with(m)) { + if MULTIPLEX_METHODS + .iter() + .any(|&m| method.method.starts_with(m)) + { // let rpc server handle engine rpc requests let res = inner.call(req).await.map_err(|e| e.into())?; Ok(res) @@ -136,6 +140,7 @@ mod tests { proxy_success().await; proxy_failure().await; does_not_proxy_engine_method().await; + does_not_proxy_eth_send_raw_transaction_method().await; health_check().await; } @@ -161,6 +166,12 @@ mod tests { assert_eq!(response.unwrap(), "engine response"); } + async fn does_not_proxy_eth_send_raw_transaction_method() { + let response = send_request("eth_sendRawTransaction").await; + assert!(response.is_ok()); + assert_eq!(response.unwrap(), "raw transaction response"); + } + async fn health_check() { let proxy_server = spawn_proxy_server().await; // Create a new HTTP client @@ -171,7 +182,13 @@ mod tests { let health_check_url = format!("http://{ADDR}:{PORT}/healthz"); let health_response = client.get(health_check_url.parse::().unwrap()).await; assert!(health_response.is_ok()); - let b = health_response.unwrap().into_body().collect().await.unwrap().to_bytes(); + let b = health_response + .unwrap() + .into_body() + .collect() + .await + .unwrap() + .to_bytes(); // Convert the collected bytes to a string let body_string = String::from_utf8(b.to_vec()).unwrap(); assert_eq!(body_string, "OK"); @@ -234,6 +251,11 @@ mod tests { module .register_method("engine_method", |_, _, _| "engine response") .unwrap(); + module + .register_method("eth_sendRawTransaction", |_, _, _| { + "raw transaction response" + }) + .unwrap(); module .register_method("non_existent_method", |_, _, _| "no proxy response") .unwrap();