Skip to content

Commit

Permalink
fix: Ready endpoint should return 503 if Edge is Not ready (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk authored May 14, 2024
1 parent ee92711 commit 80d6798
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 12 additions & 1 deletion server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;
use serde_json::json;
use tracing::debug;

use crate::types::{EdgeToken, UnleashBadRequest};
use crate::types::{EdgeToken, Status, UnleashBadRequest};

pub const TRUST_PROXY_PARSE_ERROR: &str =
"needs to be a valid ip address (ipv4 or ipv6) or a valid cidr (ipv4 or ipv6)";
Expand Down Expand Up @@ -109,6 +109,7 @@ pub enum EdgeError {
JsonParseError(String),
NoFeaturesFile,
NoTokenProvider,
NotReady,
ReadyCheckError(String),
TlsError,
TokenParseError(String),
Expand Down Expand Up @@ -200,6 +201,9 @@ impl Display for EdgeError {
EdgeError::FrontendExpectedToBeHydrated(message) => {
write!(f, "{}", message)
}
EdgeError::NotReady => {
write!(f, "Edge is not ready to serve requests")
}
}
}
}
Expand Down Expand Up @@ -235,6 +239,7 @@ impl ResponseError for EdgeError {
EdgeError::ClientHydrationFailed(_) => StatusCode::INTERNAL_SERVER_ERROR,
EdgeError::ClientCacheError => StatusCode::INTERNAL_SERVER_ERROR,
EdgeError::FrontendExpectedToBeHydrated(_) => StatusCode::INTERNAL_SERVER_ERROR,
EdgeError::NotReady => StatusCode::SERVICE_UNAVAILABLE,
}
}

Expand All @@ -259,6 +264,12 @@ impl ResponseError for EdgeError {
"status_code": status_code.as_str()
}))
}
EdgeError::NotReady => {
HttpResponseBuilder::new(self.status_code()).json(json!({
"error": "Edge is not ready to serve requests",
"status": Status::NotReady
}))
}
_ => HttpResponseBuilder::new(self.status_code()).json(json!({
"error": self.to_string()
}))
Expand Down
5 changes: 3 additions & 2 deletions server/src/internal_backstage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use crate::auth::token_validator::TokenValidator;
use crate::error::EdgeError;
use crate::http::feature_refresher::FeatureRefresher;
use crate::metrics::actix_web_metrics::PrometheusMetricsHandler;
use crate::metrics::client_metrics::MetricsCache;
Expand Down Expand Up @@ -54,7 +55,7 @@ pub async fn ready(
features_cache: web::Data<DashMap<String, ClientFeatures>>,
) -> EdgeJsonResult<EdgeStatus> {
if features_cache.is_empty() {
Ok(Json(EdgeStatus::not_ready()))
Err(EdgeError::NotReady)
} else {
Ok(Json(EdgeStatus::ready()))
}
Expand Down Expand Up @@ -199,7 +200,7 @@ mod tests {
.insert_header(ContentType::json())
.to_request();
let resp = test::call_service(&app, req).await;
assert!(resp.status().is_success());
assert!(resp.status().is_server_error());
let status: EdgeStatus = test::read_body_json(resp).await;
assert_eq!(status.status, Status::NotReady);
}
Expand Down

0 comments on commit 80d6798

Please sign in to comment.