fixes history loading slowly due to context switches

This commit is contained in:
2025-01-04 19:13:32 -05:00
parent c69022448f
commit 4dd7cea97d
12 changed files with 278 additions and 122 deletions

View File

@@ -5,6 +5,8 @@ use chrono::{DateTime, TimeDelta, Utc};
use log::trace;
use serde::Deserialize;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::timeout;
#[get("/tlm/info/{name:[\\w\\d/_-]+}")]
async fn get_tlm_definition(
@@ -16,7 +18,6 @@ async fn get_tlm_definition(
let Some(data) = data.get_by_name(&string) else {
return Err(HttpServerResultError::TlmNameNotFound { tlm: string });
};
Ok(web::Json(data.definition.clone()))
}
@@ -29,7 +30,7 @@ struct HistoryQuery {
#[get("/tlm/history/{uuid:[0-9a-f]+}")]
async fn get_tlm_history(
data: web::Data<Arc<TelemetryManagementService>>,
data_arc: web::Data<Arc<TelemetryManagementService>>,
uuid: web::Path<String>,
info: web::Query<HistoryQuery>,
) -> Result<impl Responder, HttpServerResultError> {
@@ -54,14 +55,18 @@ async fn get_tlm_history(
};
let maximum_resolution = TimeDelta::milliseconds(info.resolution);
let history_service = data.history_service();
let data = data.pin();
let history_service = data_arc.history_service();
let data = data_arc.pin();
match data.get_by_uuid(&uuid) {
None => Err(HttpServerResultError::TlmUuidNotFound { uuid }),
Some(tlm) => Ok(web::Json(
tlm.get(from, to, maximum_resolution, &history_service)
.await,
)),
Some(tlm) => timeout(
Duration::from_secs(10),
tlm.get(from, to, maximum_resolution, &history_service),
)
.await
.map(|result| Ok(web::Json(result)))
.unwrap_or_else(|_| Err(HttpServerResultError::Timeout)),
}
}

View File

@@ -15,6 +15,8 @@ pub enum HttpServerResultError {
TlmUuidNotFound { uuid: String },
#[error("DateTime Parsing Error: {date_time}")]
InvalidDateTime { date_time: String },
#[error("Timed out")]
Timeout,
}
impl ResponseError for HttpServerResultError {
@@ -23,6 +25,7 @@ impl ResponseError for HttpServerResultError {
HttpServerResultError::TlmNameNotFound { .. } => StatusCode::NOT_FOUND,
HttpServerResultError::TlmUuidNotFound { .. } => StatusCode::NOT_FOUND,
HttpServerResultError::InvalidDateTime { .. } => StatusCode::BAD_REQUEST,
HttpServerResultError::Timeout { .. } => StatusCode::GATEWAY_TIMEOUT,
}
}
fn error_response(&self) -> HttpResponse {

View File

@@ -6,7 +6,7 @@ use crate::http::api::setup_api;
use crate::http::websocket::setup_websocket;
use crate::telemetry::management_service::TelemetryManagementService;
use actix_web::{web, App, HttpServer};
use log::info;
use log::{error, info};
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
@@ -29,5 +29,7 @@ pub async fn setup(
.run()
.await?;
error!("http setup end");
Ok(())
}