Skip to content

Commit

Permalink
Merge pull request #29 from flashbots/health-check
Browse files Browse the repository at this point in the history
Add health check endpoint
  • Loading branch information
avalonche authored Oct 25, 2024
2 parents 00c87f0 + 9bb0d31 commit d189f39
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ where
}

fn call(&mut self, req: HttpRequest<HttpBody>) -> Self::Future {
match req.uri().path() {
"/healthz" => return Box::pin(async { Ok(Self::Response::new(HttpBody::from("OK"))) }),
"/metrics" => {}
_ => {}
};

let target_url = self.target_url.clone();
let client = self.client.clone();
let mut inner = self.inner.clone();
Expand Down Expand Up @@ -86,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)
Expand All @@ -110,6 +119,7 @@ where
mod tests {
use std::net::SocketAddr;

use http_body_util::BodyExt;
use jsonrpsee::{
core::{client::ClientT, ClientError},
http_client::HttpClient,
Expand All @@ -130,6 +140,8 @@ 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;
}

async fn proxy_success() {
Expand All @@ -154,6 +166,37 @@ 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
let client: Client<HttpConnector, HttpBody> =
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::<Uri>().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<String, ClientError> {
let server = spawn_server().await;
let proxy_server = spawn_proxy_server().await;
Expand Down Expand Up @@ -208,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();
Expand Down

0 comments on commit d189f39

Please sign in to comment.