Diffinery

homeflare/celldpublic

celld/crates/celld/actor/local_request.rs
119 lines · 4.5 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3use super::{ActivityGuard, AppHandle};
4use crate::js::{answer_ticket, GatedAnswer, RequestId};
5use celld_logic::RequestError;
6use std::future::Future;
7
8/// A locally routed request and its activity lifetime.
9pub struct LocalRequest<'a> {
10 app: &'a AppHandle,
11 activity: ActivityGuard,
12 #[cfg(all(test, celld_internal_tests))]
13 omit_read_position_for_tooth: bool,
14 #[cfg(all(test, celld_internal_tests))]
15 mutation_position_for_tooth: Option<Option<u64>>,
16}
17
18/// A handler failure or a rejected output, with the original failure retained.
19#[derive(Debug)]
20pub enum LocalRequestFailure {
21 Handler(anyhow::Error),
22 OutputGate {
23 verdict: RequestError,
24 handler: Option<anyhow::Error>,
25 },
26}
27
28/// A gated result and the activity that must follow its response body.
29pub struct LocalRequestCompletion<T> {
30 pub result: Result<T, LocalRequestFailure>,
31 pub activity: ActivityGuard,
32}
33
34impl AppHandle {
35 /// Bind a locally admitted request to its handler and response lifetime.
36 pub fn local_request(
37 &self,
38 request: u64,
39 cell: String,
40 request_id: Option<RequestId>,
41 origin: &'static str,
42 ) -> LocalRequest<'_> {
43 LocalRequest {
44 app: self,
45 activity: self.activity_for(request, cell, request_id, origin),
46 #[cfg(all(test, celld_internal_tests))]
47 omit_read_position_for_tooth: false,
48 #[cfg(all(test, celld_internal_tests))]
49 mutation_position_for_tooth: None,
50 }
51 }
52}
53
54impl LocalRequest<'_> {
55 // Reproduce the old read ticket at the request boundary. Keep the handler's
56 // original sample so the omitted position remains detectable.
57 #[cfg(all(test, celld_internal_tests))]
58 pub(crate) fn without_read_position_for_tooth(mut self) -> Self {
59 self.omit_read_position_for_tooth = true;
60 self
61 }
62
63 // Damage only the production ticket after the handler has returned its
64 // actual SQLite sample. Rewriting both would conceal the missing proof.
65 #[cfg(all(test, celld_internal_tests))]
66 pub(crate) fn with_mutation_position_for_tooth(mut self, position: Option<u64>) -> Self {
67 self.mutation_position_for_tooth = Some(position);
68 self
69 }
70
71 /// Execute a handler and hold its answer until its output ticket resolves.
72 pub async fn run<T: GatedAnswer>(
73 self,
74 handler: impl Future<Output = anyhow::Result<T>>,
75 ) -> LocalRequestCompletion<T> {
76 let result = handler.await;
77 // A failed handler can reveal a committed or observed position too.
78 // Gating successes alone lets its error escape before that position is
79 // durable and leaves later read-only requests without a barrier.
80 let ticket = answer_ticket(&result);
81 #[cfg(all(test, celld_internal_tests))]
82 let ticket = ticket.map(|mut ticket| {
83 if self.omit_read_position_for_tooth && ticket.position.is_none() {
84 ticket.observed = None;
85 }
86 if let (Some(_), Some(position)) = (ticket.position, self.mutation_position_for_tooth) {
87 ticket.position = position;
88 }
89 ticket
90 });
91 let result = match ticket {
92 Some(ticket) => {
93 self.activity.set_phase("output_gate", false, false);
94 if let Some(position) = ticket.position {
95 self.activity.gate_started(position);
96 }
97 // The activity owns the request used by the gate. Accepting
98 // another request here could acknowledge a different write or
99 // release this cell while its answer still waits for proof.
100 let gated = self.app.gate_output(self.activity.request, ticket).await;
101 self.activity.gate_finished(gated.is_ok());
102 match gated {
103 Ok(()) => result.map_err(LocalRequestFailure::Handler),
104 Err(verdict) => Err(LocalRequestFailure::OutputGate {
105 verdict,
106 handler: result.err(),
107 }),
108 }
109 }
110 None => result.map_err(LocalRequestFailure::Handler),
111 };
112 // A streaming answer still pins the cell. Returning only its value
113 // would drop the guard before the response-body owner can retain it.
114 LocalRequestCompletion {
115 result,
116 activity: self.activity,
117 }
118 }
119}