operations.rs

benches/rrt/canister/src/workflows/sequence/operations.rs

operations.rs168 lines← Benchmarks
1#![allow(non_snake_case)]2use super::{generated, memory::current as M};3use crate::{4    wire,5    workflows::{self as shared, Seed, Services},6};7use cdk_rsx::Principal;8use cdk_rsx_takt as flow;9pub struct Operations<S: Services> {10    services: S,11}12impl<S: Services> Operations<S> {13    pub fn new(services: S) -> Self {14        Self { services }15    }16    fn local(17        &self,18        context: &flow::Context,19        r: &M::Request,20        kind: wire::Kind,21    ) -> flow::Known<M::Request, M::Error> {22        match shared::dispatch(&self.services, r.caller, context.now, command(r, kind))23            .expect("business storage failure")24        {25            Ok(block) => {26                let mut r = r.clone();27                assert!(r.blocks.len() < 32, "bounded graph block references");28                r.blocks.push(shared::number(&block));29                flow::Known::Ok(r)30            }31            Err(e) => flow::Known::Err(classify(&e)),32        }33    }34}35impl From<Seed> for M::Request {36    fn from(s: Seed) -> Self {37        Self {38            caller: s.caller,39            run: s.run,40            job: s.job,41            left: s.left,42            right: s.right,43            child: s.child,44            units: s.units,45            wake_at: s.wake_at,46            created_at: s.created_at,47            expires_at: s.expires_at,48            topic: shared::bounded_topic(s.topic),49            blocks: Default::default(),50        }51    }52}53fn command(r: &M::Request, kind: wire::Kind) -> shared::CommandInput<'_> {54    shared::command(55        "sequence",56        r.run,57        r.job,58        r.units,59        r.topic.as_slice(),60        r.created_at,61        r.expires_at,62        kind,63        None,64    )65}66fn classify(error: &wire::DispatchError) -> M::Error {67    if shared::is_unavailable(error) {68        M::Error::Unavailable69    } else {70        M::Error::Rejected71    }72}73impl<S: Services> generated::Bindings for Operations<S> {74    fn deliver(75        &self,76        context: &flow::Context,77        value: &M::Request,78    ) -> flow::Known<M::Request, M::Error> {79        self.local(context, value, wire::Kind::Deliver)80    }81    fn audit(82        &self,83        context: &flow::Context,84        value: &M::Request,85    ) -> flow::Known<M::Request, M::Error> {86        self.local(context, value, wire::Kind::Audit)87    }88    type OperationReserveRemote<'a>89        = Remote<'a, S>90    where91        Self: 'a;92    fn reserveRemote(&self) -> Remote<'_, S> {93        Remote {94            services: &self.services,95            kind: wire::Kind::Reserve,96            destination: |r| r.left,97            priority: flow::Priority::Interactive,98        }99    }100    type OperationCloseRemote<'a>101        = Remote<'a, S>102    where103        Self: 'a;104    fn closeRemote(&self) -> Remote<'_, S> {105        Remote {106            services: &self.services,107            kind: wire::Kind::Close,108            destination: |r| r.right,109            priority: flow::Priority::Interactive,110        }111    }112}113pub struct Remote<'a, S: Services> {114    services: &'a S,115    kind: wire::Kind,116    destination: fn(&M::Request) -> Principal,117    priority: flow::Priority,118}119impl<S: Services> flow::RemoteOperation<M::Progress, M::Error> for Remote<'_, S> {120    const SAFETY: flow::Safety = flow::Safety::Idempotent;121    type Input = M::Request;122    type Output = M::Request;123    fn input(&self) -> flow::Port<M::Progress, M::Request> {124        generated::ports::request()125    }126    fn output(&self) -> flow::Port<M::Progress, M::Request> {127        generated::ports::request()128    }129    fn prepare(130        &self,131        _: &flow::Context,132        r: &M::Request,133    ) -> flow::RemotePreparation<M::Request, M::Error> {134        let mut context = self.services.reactor();135        context.priority = self.priority;136        flow::RemotePreparation::Request(shared::saved_request(137            context,138            (self.destination)(r),139            r.caller,140            "work.dispatch",141            command(r, self.kind).encode_route(),142            r.expires_at,143        ))144    }145    fn reply(146        &self,147        r: &M::Request,148        bytes: &[u8],149        _: &flow::Context,150    ) -> flow::Reply<M::Request, M::Error> {151        let Ok((result, _caller)) = shared::dispatch_reply(bytes) else {152            return flow::Reply::Unknown;153        };154        match result {155            Ok(block) => {156                let mut r = r.clone();157                assert!(r.blocks.len() < 32, "bounded graph block references");158                r.blocks.push(block);159                flow::Reply::Ok(r)160            }161            Err(e) if shared::is_too_old(&e) => flow::Reply::Unknown,162            Err(e) => flow::Reply::Resolved(flow::Known::Err(classify(&e))),163        }164    }165    fn safe_until(&self, r: &M::Request) -> Option<u64> {166        Some(r.expires_at)167    }168}