Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: archetypes handler takes its own sub-state via FromRef

This commit is contained in:
atlas 2026-11-29 06:47:25 +01:00 • committed by mara
commit c43c35e05b

View file

@ -9,10 +9,11 @@
//! failed or unanswered evaluation keeps the stored record and is asked again
//! after [`RETRY_FIRST`], doubling up to [`REFRESH_INTERVAL`].
use std::sync::Arc;
use std::time::Duration;
use axum::Json;
use axum::extract::State;
use axum::extract::{FromRef, State};
use hive_types::Permission;
use problem_details::ProblemDetails;
use serde::{Deserialize, Serialize};
@ -28,7 +29,7 @@ use swarm_wire::notify_flows::{Flow, MAX_FLOWS};
use utoipa::ToSchema;
use crate::routes::{AppState, error_problem};
use crate::schedules::{queue, read_or_default, refused, write};
use crate::schedules::{queue_of, read_or_default, refused, write};
use crate::{agent_limits, agent_permissions, inference};
/// How often the builder is asked. Its source only changes when its host is
@ -336,6 +337,24 @@ fn view(key: &str, record: SourceRecord) -> Archetypes {
out
}
/// What the archetype route may reach of [`AppState`]: the queue's status
/// reader, which holds the connection the archetype store lives on.
///
/// Derived per request from [`AppState`] (`FromRef`), so the field is a clone
/// of the same `Arc` and nothing is held twice.
#[derive(Clone)]
pub(crate) struct ArchetypesState {
pub(crate) status: Option<Arc<crate::status::StatusReader>>,
}
impl FromRef<AppState> for ArchetypesState {
fn from_ref(state: &AppState) -> Self {
Self {
status: state.status.clone(),
}
}
}
#[utoipa::path(
get,
path = "/api/archetypes",
@ -350,9 +369,9 @@ fn view(key: &str, record: SourceRecord) -> Archetypes {
tag = "agents"
)]
pub(crate) async fn get_archetypes(
State(state): State<AppState>,
State(state): State<ArchetypesState>,
) -> Result<Json<Archetypes>, ProblemDetails> {
let client = queue(&state).map_err(refused)?;
let client = queue_of(state.status.as_ref()).map_err(refused)?;
let failed = |e: anyhow::Error| {
tracing::warn!(error = %format!("{e:#}"), "archetypes: read failed");
error_problem(
@ -375,7 +394,42 @@ pub(crate) async fn get_archetypes(
#[cfg(test)]
mod tests {
use super::*;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use serde_json::json;
use tower::ServiceExt as _;
async fn get(state: AppState) -> (StatusCode, serde_json::Value) {
let req = Request::get("/api/archetypes")
.body(Body::empty())
.expect("a request");
let res = crate::startup::build_app(state)
.oneshot(req)
.await
.expect("infallible");
let status = res.status();
let bytes = axum::body::to_bytes(res.into_body(), usize::MAX)
.await
.expect("a body");
(status, serde_json::from_slice(&bytes).unwrap_or_default())
}
#[tokio::test]
async fn the_route_is_503_without_a_queue() {
let (state, _) = crate::routes::tests::state_with_roster();
let (status, _) = get(state).await;
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
}
/// Presence control for the test above: with the queue `AppState` holds
/// the route reaches the store, which holds nothing yet.
#[tokio::test]
async fn the_route_reads_the_queue_appstate_holds() {
let (_nats, state, _) = crate::routes::tests::state_with_roster_queued().await;
let (status, body) = get(state).await;
assert_eq!(status, StatusCode::OK, "{body}");
assert_eq!(body, json!({ "archetypes": [], "refused": [] }));
}
fn evaluated(archetypes: serde_json::Value) -> SourceRecord {
let serde_json::Value::Object(map) = archetypes else {