mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-07-27 08:41:00 +00:00
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
This commit is contained in:
@ -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<Data>) -> Result<HttpResponse, ResponseError> {
|
||||
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<Data>) -> Result<HttpResponse, ResponseError> {
|
||||
data.db.main_write(|w| data.db.set_healthy(w))?;
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
||||
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<Data>,
|
||||
body: web::Json<HealthBody>,
|
||||
) -> Result<HttpResponse, ResponseError> {
|
||||
if body.health {
|
||||
set_healthy(data).await
|
||||
} else {
|
||||
set_unhealthy(data).await
|
||||
}
|
||||
async fn get_health() -> Result<HttpResponse, ResponseError> {
|
||||
Ok(HttpResponse::NoContent().finish())
|
||||
}
|
||||
|
Reference in New Issue
Block a user