Then_some in the insight about-drop, small refusal enum in worker_for
Browse files at this commit5bb7de4d43df739526828d5738502d26a8a4f93eParent: ee1f8f9- fix
- then_some in the insight about-drop, small refusal enum in worker_for
2 changed files +30 -10
- src/web/insights.rs +1 -1
- src/web/podcast.rs +29 -9
src/web/insights.rsmodified+1 -1
| @@ -692,7 +692,7 @@ async fn cards_of( | |||
| 692 | // ever set it — so the value is dropped for a student caller, not | 692 | // ever set it — so the value is dropped for a student caller, not |
| 693 | // merely absent. | 693 | // merely absent. |
| 694 | about: (role != Role::Student) | 694 | about: (role != Role::Student) |
| 695 | - .then(|| row.about) | 695 | + .then_some(row.about) |
| 696 | .flatten() | 696 | .flatten() |
| 697 | .map(|about| about.key().to_string()), | 697 | .map(|about| about.key().to_string()), |
| 698 | audience_role: row.audience_role, | 698 | audience_role: row.audience_role, |
src/web/podcast.rsmodified+29 -9
| @@ -180,7 +180,7 @@ async fn submit( | |||
| 180 | ) -> Result<Response, AppError> { | 180 | ) -> Result<Response, AppError> { |
| 181 | let bridge = match worker_for(&st, AI_PODCAST_SUBMIT_CAPABILITY) { | 181 | let bridge = match worker_for(&st, AI_PODCAST_SUBMIT_CAPABILITY) { |
| 182 | Ok(bridge) => bridge, | 182 | Ok(bridge) => bridge, |
| 183 | - Err(refusal) => return Ok(refusal), | 183 | + Err(no_worker) => return Ok(no_worker.refusal()), |
| 184 | }; | 184 | }; |
| 185 | 185 | ||
| 186 | // Whitespace-only is empty: the service applies the same rule, so checking | 186 | // Whitespace-only is empty: the service applies the same rule, so checking |
| @@ -226,7 +226,7 @@ async fn status( | |||
| 226 | ) -> Result<Response, AppError> { | 226 | ) -> Result<Response, AppError> { |
| 227 | let bridge = match worker_for(&st, AI_PODCAST_STATUS_CAPABILITY) { | 227 | let bridge = match worker_for(&st, AI_PODCAST_STATUS_CAPABILITY) { |
| 228 | Ok(bridge) => bridge, | 228 | Ok(bridge) => bridge, |
| 229 | - Err(refusal) => return Ok(refusal), | 229 | + Err(no_worker) => return Ok(no_worker.refusal()), |
| 230 | }; | 230 | }; |
| 231 | match podcast::status(&bridge, &slug, PodcastStatusPayload { job_id: id }).await { | 231 | match podcast::status(&bridge, &slug, PodcastStatusPayload { job_id: id }).await { |
| 232 | Ok(reply) => Ok(Json(snapshot(reply)).into_response()), | 232 | Ok(reply) => Ok(Json(snapshot(reply)).into_response()), |
| @@ -259,7 +259,7 @@ async fn result( | |||
| 259 | ) -> Result<Response, AppError> { | 259 | ) -> Result<Response, AppError> { |
| 260 | let bridge = match worker_for(&st, AI_PODCAST_RESULT_CAPABILITY) { | 260 | let bridge = match worker_for(&st, AI_PODCAST_RESULT_CAPABILITY) { |
| 261 | Ok(bridge) => bridge, | 261 | Ok(bridge) => bridge, |
| 262 | - Err(refusal) => return Ok(refusal), | 262 | + Err(no_worker) => return Ok(no_worker.refusal()), |
| 263 | }; | 263 | }; |
| 264 | match podcast::result(&bridge, &slug, PodcastResultPayload { job_id: id }).await { | 264 | match podcast::result(&bridge, &slug, PodcastResultPayload { job_id: id }).await { |
| 265 | Ok(reply) => Ok(Json(artifacts(reply)).into_response()), | 265 | Ok(reply) => Ok(Json(artifacts(reply)).into_response()), |
| @@ -291,7 +291,7 @@ async fn cancel( | |||
| 291 | ) -> Result<Response, AppError> { | 291 | ) -> Result<Response, AppError> { |
| 292 | let bridge = match worker_for(&st, AI_PODCAST_CANCEL_CAPABILITY) { | 292 | let bridge = match worker_for(&st, AI_PODCAST_CANCEL_CAPABILITY) { |
| 293 | Ok(bridge) => bridge, | 293 | Ok(bridge) => bridge, |
| 294 | - Err(refusal) => return Ok(refusal), | 294 | + Err(no_worker) => return Ok(no_worker.refusal()), |
| 295 | }; | 295 | }; |
| 296 | match podcast::cancel(&bridge, &slug, PodcastCancelPayload { job_id: id }).await { | 296 | match podcast::cancel(&bridge, &slug, PodcastCancelPayload { job_id: id }).await { |
| 297 | Ok(reply) => Ok(Json(verdict(reply)).into_response()), | 297 | Ok(reply) => Ok(Json(verdict(reply)).into_response()), |
| @@ -468,19 +468,39 @@ fn pump(file: tokio::fs::File) -> impl Stream<Item = Result<Bytes, std::io::Erro | |||
| 468 | ReceiverStream::new(rx) | 468 | ReceiverStream::new(rx) |
| 469 | } | 469 | } |
| 470 | 470 | ||
| 471 | +/// Why a dispatching door has no worker to talk to. Deliberately small: the | ||
| 472 | +/// refusal is not a response — the caller builds the `503` at its own call | ||
| 473 | +/// site, so a large, rarely-taken `Response` never rides the `Result` type of | ||
| 474 | +/// a hot path. | ||
| 475 | +enum NoWorker { | ||
| 476 | + /// No AI service is configured on this deployment at all. | ||
| 477 | + Disabled, | ||
| 478 | + /// A bridge is configured, but no connected worker declares this door's | ||
| 479 | + /// capability. | ||
| 480 | + Unconnected, | ||
| 481 | +} | ||
| 482 | + | ||
| 483 | +impl NoWorker { | ||
| 484 | + /// The `503` the caller returns for this refusal. | ||
| 485 | + fn refusal(self) -> Response { | ||
| 486 | + ai_unavailable(match self { | ||
| 487 | + NoWorker::Disabled => "the AI service is not enabled on this deployment", | ||
| 488 | + NoWorker::Unconnected => "no AI service is connected right now", | ||
| 489 | + }) | ||
| 490 | + } | ||
| 491 | +} | ||
| 492 | + | ||
| 471 | /// The 503 gate every dispatching door shares: no bridge configured, or no | 493 | /// The 503 gate every dispatching door shares: no bridge configured, or no |
| 472 | /// worker declaring this door's capability. `has_capability` is documented | 494 | /// worker declaring this door's capability. `has_capability` is documented |
| 473 | /// racy — fine here: it never guards a write, and the dispatch that follows | 495 | /// racy — fine here: it never guards a write, and the dispatch that follows |
| 474 | /// re-checks for real; this only spares a caller a round trip into a service | 496 | /// re-checks for real; this only spares a caller a round trip into a service |
| 475 | /// that cannot answer. | 497 | /// that cannot answer. |
| 476 | -fn worker_for(st: &AppState, capability: &str) -> Result<AiBridge, Response> { | 498 | +fn worker_for(st: &AppState, capability: &str) -> Result<AiBridge, NoWorker> { |
| 477 | let Some(bridge) = st.ai.clone() else { | 499 | let Some(bridge) = st.ai.clone() else { |
| 478 | - return Err(ai_unavailable( | 500 | + return Err(NoWorker::Disabled); |
| 479 | - "the AI service is not enabled on this deployment", | ||
| 480 | - )); | ||
| 481 | }; | 501 | }; |
| 482 | if !bridge.has_capability(capability) { | 502 | if !bridge.has_capability(capability) { |
| 483 | - return Err(ai_unavailable("no AI service is connected right now")); | 503 | + return Err(NoWorker::Unconnected); |
| 484 | } | 504 | } |
| 485 | Ok(bridge) | 505 | Ok(bridge) |
| 486 | } | 506 | } |