-
Notifications
You must be signed in to change notification settings - Fork 75
starknet_transaction_prover: add /health endpoint via HealthLayer #14163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,64 @@ | ||
| //! HTTP `/health` endpoint as a tower middleware layer. | ||
| //! | ||
| //! Short-circuits `GET /health` before the jsonrpsee service sees the request | ||
| //! (which would 405 a GET). Any other request passes through unchanged. | ||
|
|
||
| use std::task::{Context, Poll}; | ||
|
|
||
| use bytes::Bytes; | ||
| use futures::future::{ready, Either, Ready}; | ||
| use http::{header, Method, Request, Response, StatusCode}; | ||
| use http_body_util::Full; | ||
| use jsonrpsee::server::HttpBody; | ||
| use tower::{Layer, Service}; | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "health_test.rs"] | ||
| mod health_test; | ||
|
|
||
| pub const HEALTH_PATH: &str = "/health"; | ||
|
|
||
| const HEALTHY_BODY: &[u8] = br#"{"status":"ok"}"#; | ||
|
|
||
| #[derive(Clone, Copy, Default)] | ||
| pub struct HealthLayer; | ||
|
|
||
| impl<S> Layer<S> for HealthLayer { | ||
| type Service = HealthService<S>; | ||
|
|
||
| fn layer(&self, inner: S) -> Self::Service { | ||
| HealthService { inner } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct HealthService<S> { | ||
| inner: S, | ||
| } | ||
|
|
||
| impl<S, ReqB> Service<Request<ReqB>> for HealthService<S> | ||
| where | ||
| S: Service<Request<ReqB>, Response = Response<HttpBody>>, | ||
| { | ||
| type Response = Response<HttpBody>; | ||
| type Error = S::Error; | ||
| type Future = Either<Ready<Result<Self::Response, Self::Error>>, S::Future>; | ||
|
|
||
| fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| // Always ready: the health fast-path doesn't need the inner service. | ||
| // Inner backpressure is driven on demand by `inner.call` below. | ||
| Poll::Ready(Ok(())) | ||
| } | ||
|
|
||
| fn call(&mut self, request: Request<ReqB>) -> Self::Future { | ||
| if request.method() == Method::GET && request.uri().path() == HEALTH_PATH { | ||
| let response = Response::builder() | ||
| .status(StatusCode::OK) | ||
| .header(header::CONTENT_TYPE, "application/json") | ||
| .body(HttpBody::new(Full::new(Bytes::from_static(HEALTHY_BODY)))) | ||
| .expect("response build with a static body is infallible"); | ||
| return Either::Left(ready(Ok(response))); | ||
| } | ||
| Either::Right(self.inner.call(request)) | ||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
crates/starknet_transaction_prover/src/server/health_test.rs
This file contains hidden or 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,69 @@ | ||
| use bytes::Bytes; | ||
| use http::{Method, Request, Response, StatusCode}; | ||
| use http_body_util::{BodyExt, Full}; | ||
| use jsonrpsee::server::HttpBody; | ||
| use tower::{Layer, ServiceExt}; | ||
|
|
||
| use crate::server::health::{HealthLayer, HEALTH_PATH}; | ||
|
|
||
| /// Inner stub returning 418 so we can tell whether `HealthLayer` short-circuited. | ||
| fn fallthrough_service() -> impl tower::Service< | ||
| Request<HttpBody>, | ||
| Response = Response<HttpBody>, | ||
| Error = std::convert::Infallible, | ||
| Future = futures::future::Ready<Result<Response<HttpBody>, std::convert::Infallible>>, | ||
| > + Clone { | ||
| tower::service_fn(|_req: Request<HttpBody>| { | ||
| let response = Response::builder() | ||
| .status(StatusCode::IM_A_TEAPOT) | ||
| .body(HttpBody::new(Full::new(Bytes::from_static(b"fallthrough")))) | ||
| .expect("static body is infallible"); | ||
| futures::future::ready(Ok::<_, std::convert::Infallible>(response)) | ||
| }) | ||
| } | ||
|
|
||
| fn empty_request(method: Method, path: &str) -> Request<HttpBody> { | ||
| Request::builder() | ||
| .method(method) | ||
| .uri(path) | ||
| .body(HttpBody::new(Full::new(Bytes::new()))) | ||
| .expect("static body is infallible") | ||
| } | ||
|
|
||
| async fn read_body(response: Response<HttpBody>) -> (StatusCode, Vec<u8>, http::HeaderMap) { | ||
| let (parts, body) = response.into_parts(); | ||
| let bytes = body.collect().await.expect("body collect").to_bytes().to_vec(); | ||
| (parts.status, bytes, parts.headers) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn get_health_returns_200_with_json_body() { | ||
| let svc = HealthLayer.layer(fallthrough_service()); | ||
|
|
||
| let response = svc.oneshot(empty_request(Method::GET, HEALTH_PATH)).await.unwrap(); | ||
|
|
||
| let (status, body, headers) = read_body(response).await; | ||
| assert_eq!(status, StatusCode::OK); | ||
| assert_eq!(body, br#"{"status":"ok"}"#); | ||
| assert_eq!(headers.get(http::header::CONTENT_TYPE).unwrap(), "application/json"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn non_get_health_falls_through() { | ||
| let svc = HealthLayer.layer(fallthrough_service()); | ||
|
|
||
| let response = svc.oneshot(empty_request(Method::POST, HEALTH_PATH)).await.unwrap(); | ||
|
|
||
| let (status, _body, _) = read_body(response).await; | ||
| assert_eq!(status, StatusCode::IM_A_TEAPOT); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn get_other_path_falls_through() { | ||
| let svc = HealthLayer.layer(fallthrough_service()); | ||
|
|
||
| let response = svc.oneshot(empty_request(Method::GET, "/")).await.unwrap(); | ||
|
|
||
| let (status, _body, _) = read_body(response).await; | ||
| assert_eq!(status, StatusCode::IM_A_TEAPOT); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.