Skip to main content

graphrt_provider_api/
provider.rs

1//! The run-time `Provider` and deploy-time `DeployDriver` traits (plan §6).
2//!
3//! Both are object safe (`Box<dyn Provider>`): async methods return a boxed
4//! future instead of using `async fn` in the trait, so the gateway can pick a
5//! provider at run time from its config without generics, and without the
6//! `async-trait` crate. Implementations write `Box::pin(async move { ... })`.
7
8use std::future::Future;
9use std::pin::Pin;
10
11use crate::error::{DeployError, ProviderError};
12use crate::invoke::{InvokeRequest, InvokeResponse};
13use crate::types::{Capabilities, DeploymentRef, DeploymentSpec, Endpoint, ReleaseMode, ReplicaRef, ReplicaState};
14
15/// A boxed `Send` future, the return type of every async trait method here.
16pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
17
18/// Run-time operations only: wake, release, list. Creating or updating a
19/// deployment is `graphrt deploy`'s job with deploy-time credentials
20/// ([`DeployDriver`]); the gateway's credentials can only wake / scale / stop / list.
21///
22/// Contract (checked by the conformance harness):
23/// - `wake(d, n)` is **idempotent and absolute**: it makes at least `n`
24///   replicas of `d` ready and returns the endpoints of all ready replicas
25///   (≥ `n`). Twenty concurrent `wake(d, 1)` from zero end with one replica
26///   (C1). For `Addressing::Url` it may be a no-op returning the one URL.
27/// - `release(r, mode)` gives back exactly `r`; releasing a replica that is
28///   already gone is `Ok`. For `Addressing::Url` it is a no-op.
29/// - `list(d)` reports every replica the provider knows for `d`, including
30///   ones the gateway did not start (reconciliation, C5 and C12).
31/// - Errors are classified ([`crate::ProviderErrorKind`]); credentials never
32///   appear in errors or logs (C8, C10).
33pub trait Provider: Send + Sync {
34    /// Static capabilities.
35    fn capabilities(&self) -> Capabilities;
36
37    /// Ensures at least `replicas` ready replicas and returns their endpoints.
38    fn wake<'a>(
39        &'a self,
40        deployment: &'a DeploymentRef,
41        replicas: u32,
42    ) -> BoxFuture<'a, Result<Vec<Endpoint>, ProviderError>>;
43
44    /// Releases one replica (`Suspend`, `Stop` or `ScaleIn`).
45    fn release<'a>(&'a self, replica: &'a ReplicaRef, mode: ReleaseMode) -> BoxFuture<'a, Result<(), ProviderError>>;
46
47    /// Lists the replicas of a deployment version for reconciliation.
48    fn list<'a>(&'a self, deployment: &'a DeploymentRef) -> BoxFuture<'a, Result<Vec<ReplicaState>, ProviderError>>;
49
50    /// Sends a gateway → agent request through the platform instead of HTTP
51    /// (see [`crate::invoke`]). `None` (the default) means the endpoint is
52    /// reached over HTTP at its address.
53    fn invoke<'a>(
54        &'a self,
55        _endpoint: &'a Endpoint,
56        _request: InvokeRequest,
57    ) -> Option<BoxFuture<'a, Result<InvokeResponse, ProviderError>>> {
58        None
59    }
60}
61
62/// Creates or updates a deployment version (`graphrt deploy --target <t>`).
63/// Writes secrets to the provider's secret store as references; the gateway
64/// never receives them.
65pub trait DeployDriver: Send + Sync {
66    /// Applies `spec` and returns the reference the gateway will wake.
67    fn apply<'a>(&'a self, spec: &'a DeploymentSpec) -> BoxFuture<'a, Result<DeploymentRef, DeployError>>;
68}
69
70impl<P: Provider + ?Sized> Provider for std::sync::Arc<P> {
71    fn capabilities(&self) -> Capabilities {
72        (**self).capabilities()
73    }
74    fn wake<'a>(
75        &'a self,
76        deployment: &'a DeploymentRef,
77        replicas: u32,
78    ) -> BoxFuture<'a, Result<Vec<Endpoint>, ProviderError>> {
79        (**self).wake(deployment, replicas)
80    }
81    fn release<'a>(&'a self, replica: &'a ReplicaRef, mode: ReleaseMode) -> BoxFuture<'a, Result<(), ProviderError>> {
82        (**self).release(replica, mode)
83    }
84    fn list<'a>(&'a self, deployment: &'a DeploymentRef) -> BoxFuture<'a, Result<Vec<ReplicaState>, ProviderError>> {
85        (**self).list(deployment)
86    }
87    fn invoke<'a>(
88        &'a self,
89        endpoint: &'a Endpoint,
90        request: InvokeRequest,
91    ) -> Option<BoxFuture<'a, Result<InvokeResponse, ProviderError>>> {
92        (**self).invoke(endpoint, request)
93    }
94}