From 6465a3f5497ba48f5347641665d289c93b090460 Mon Sep 17 00:00:00 2001 From: frbimo Date: Mon, 19 Oct 2020 13:39:06 +0800 Subject: [PATCH] refactor /health on meilisearch-http that complies: 1. NEEDS to ensure that service is completely up if it returns 204 2. DOES NOT block service process (write transaction) 3. NEEDS to use the less network bandwidth as possible when it's triggered 4. NEEDS to use the less service resources as possible when it's triggered 5. DOES NOT NEED any authentication 6. MAY be named /health --- meilisearch-http/src/routes/health.rs | 44 +++------------------------ meilisearch-http/tests/health.rs | 29 +----------------- 2 files changed, 6 insertions(+), 67 deletions(-) diff --git a/meilisearch-http/src/routes/health.rs b/meilisearch-http/src/routes/health.rs index a8abece8b..c57e4c7e9 100644 --- a/meilisearch-http/src/routes/health.rs +++ b/meilisearch-http/src/routes/health.rs @@ -1,47 +1,13 @@ +use actix_web::get; use actix_web::{web, HttpResponse}; -use actix_web::{get, put}; -use serde::Deserialize; -use crate::error::{Error, ResponseError}; -use crate::helpers::Authentication; -use crate::Data; +use crate::error::ResponseError; pub fn services(cfg: &mut web::ServiceConfig) { - cfg.service(get_health).service(change_healthyness); + cfg.service(get_health); } #[get("/health")] -async fn get_health(data: web::Data) -> Result { - let reader = data.db.main_read_txn()?; - if let Ok(Some(_)) = data.db.get_health(&reader) { - return Err(Error::Maintenance.into()); - } - Ok(HttpResponse::Ok().finish()) -} - -async fn set_healthy(data: web::Data) -> Result { - data.db.main_write(|w| data.db.set_healthy(w))?; - Ok(HttpResponse::Ok().finish()) -} - -async fn set_unhealthy(data: web::Data) -> Result { - data.db.main_write(|w| data.db.set_unhealthy(w))?; - Ok(HttpResponse::Ok().finish()) -} - -#[derive(Deserialize, Clone)] -struct HealthBody { - health: bool, -} - -#[put("/health", wrap = "Authentication::Private")] -async fn change_healthyness( - data: web::Data, - body: web::Json, -) -> Result { - if body.health { - set_healthy(data).await - } else { - set_unhealthy(data).await - } +async fn get_health() -> Result { + Ok(HttpResponse::NoContent().finish()) } diff --git a/meilisearch-http/tests/health.rs b/meilisearch-http/tests/health.rs index 295a41a25..f72127431 100644 --- a/meilisearch-http/tests/health.rs +++ b/meilisearch-http/tests/health.rs @@ -1,6 +1,3 @@ -use serde_json::json; -use std::convert::Into; - mod common; #[actix_rt::test] @@ -10,29 +7,5 @@ async fn test_healthyness() { // Check that the server is healthy let (_response, status_code) = server.get_health().await; - assert_eq!(status_code, 200); - - // Set the serve Unhealthy - let body = json!({ - "health": false, - }); - let (_response, status_code) = server.update_health(body).await; - assert_eq!(status_code, 200); - - // Check that the server is unhealthy - - let (_response, status_code) = server.get_health().await; - assert_eq!(status_code, 503); - - // Set the server healthy - let body = json!({ - "health": true, - }); - let (_response, status_code) = server.update_health(body).await; - assert_eq!(status_code, 200); - - // Check if the server is healthy - - let (_response, status_code) = server.get_health().await; - assert_eq!(status_code, 200); + assert_eq!(status_code, 204); }