Skip to content

Commit

Permalink
Rpc server (#825)
Browse files Browse the repository at this point in the history
* chore: add distinct ways to deal with probes

* lint

* chore: use layers instead of match to hold probe logic

* lint
  • Loading branch information
renancloudwalk authored May 11, 2024
1 parent 640fb52 commit efcbb46
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use jsonrpsee::server::Server;
use jsonrpsee::types::Params;
use jsonrpsee::IntoSubscriptionCloseResponse;
use jsonrpsee::PendingSubscriptionSink;
use serde::Deserialize;
use serde_json::json;
use serde_json::Value as JsonValue;
use tokio::select;
Expand Down Expand Up @@ -88,7 +87,10 @@ pub async fn serve_rpc(

// configure middleware
let rpc_middleware = RpcServiceBuilder::new().layer_fn(RpcMiddleware::new);
let http_middleware = tower::ServiceBuilder::new().layer(ProxyGetRequestLayer::new("/health", "net_listening").unwrap());
let http_middleware = tower::ServiceBuilder::new()
.layer(ProxyGetRequestLayer::new("/startup", "startup").unwrap())
.layer(ProxyGetRequestLayer::new("/readiness", "readiness").unwrap())
.layer(ProxyGetRequestLayer::new("/liveness", "liveness").unwrap());

// serve module
let server = Server::builder()
Expand Down Expand Up @@ -132,6 +134,9 @@ fn register_methods(mut module: RpcModule<RpcContext>) -> anyhow::Result<RpcModu
// blockchain
module.register_async_method("net_version", net_version)?;
module.register_async_method("net_listening", net_listening)?;
module.register_async_method("startup", startup)?;
module.register_async_method("readiness", readiness)?;
module.register_async_method("liveness", liveness)?;
module.register_async_method("eth_chainId", eth_chain_id)?;
module.register_async_method("web3_clientVersion", web3_client_version)?;

Expand Down Expand Up @@ -201,23 +206,20 @@ async fn evm_set_next_block_timestamp(params: Params<'_>, ctx: Arc<RpcContext>)
Ok(serde_json::to_value(timestamp).unwrap())
}

#[derive(Debug, Deserialize, Default)]
struct NetListeningParams {
probe_type: Option<String>, // This will be optional
// Status
async fn net_listening(params: Params<'_>, arc: Arc<RpcContext>) -> anyhow::Result<JsonValue, RpcError> {
readiness(params, arc).await
}

// Status
async fn net_listening(params: Params<'_>, _: Arc<RpcContext>) -> anyhow::Result<JsonValue, RpcError> {
let (_, probe_params) = next_rpc_param_or_default::<NetListeningParams>(params.sequence())?;

// Log the probe type if it's provided
match probe_params.probe_type.as_deref() {
Some("liveness") => tracing::info!("Performing liveness check"),
Some("startup") => tracing::info!("Performing startup check"),
Some("readiness") => tracing::info!("Performing readiness check"), //TODO if it falls out of sync with substrate, we need to switch ready to false until its back again
_ => tracing::info!("General check or unspecified probe type"),
}
async fn startup(_: Params<'_>, _: Arc<RpcContext>) -> anyhow::Result<JsonValue, RpcError> {
Ok(json!(true))
}

async fn readiness(_: Params<'_>, _: Arc<RpcContext>) -> anyhow::Result<JsonValue, RpcError> {
Ok(json!(true))
}

async fn liveness(_: Params<'_>, _: Arc<RpcContext>) -> anyhow::Result<JsonValue, RpcError> {
Ok(json!(true))
}

Expand Down

0 comments on commit efcbb46

Please sign in to comment.