wasmcloud_core/
rpc.rs

1//! Core reusable types related to performing [RPC calls on a wasmCloud lattice][docs-wasmcloud-rpc]
2//!
3//! Wasmbus is the name of the NATS-powered RPC transport mechanism primarily used by wasmCloud.
4//!
5//! Various wasmCloud workloads (capability providers, components) use Wasmbus (and thus NATS)
6//! to communicate and send RPCs -- often over well known topics (some of which are detailed in this module).
7//!
8//! [docs-wasmcloud-rpc]: <https://wasmcloud.com/docs/hosts/lattice-protocols/rpc>
9
10use serde::{Deserialize, Serialize};
11
12#[derive(Clone, Debug, Default, Deserialize, Serialize)]
13pub struct HealthCheckRequest {}
14
15#[derive(Clone, Debug, Default, Deserialize, Serialize)]
16pub struct HealthCheckResponse {
17    /// A flag that indicates the component is healthy
18    #[serde(default)]
19    pub healthy: bool,
20    /// A message containing additional information about the components health
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub message: Option<String>,
23}
24
25/// Generate the wasmbus RPC subject for putting links on a NATS cluster
26///
27/// When messages are published on this subject, hosts set up and update (if necessary) link information,
28/// which may include calling `receive_link_config_*()` functions on relevant providers.
29#[must_use]
30pub fn link_put_subject(lattice: &str, provider_key: &str) -> String {
31    format!("wasmbus.rpc.{lattice}.{provider_key}.linkdefs.put")
32}
33
34/// Generate the wasmbus RPC subject for deleting links on a NATS cluster
35///
36/// When messages are published on this subject, hosts remove link information,
37/// which may include calling `delete_link()` on relevant providers.
38#[must_use]
39pub fn link_del_subject(lattice: &str, provider_key: &str) -> String {
40    format!("wasmbus.rpc.{lattice}.{provider_key}.linkdefs.del")
41}
42
43/// Generate the wasmbus RPC subject for retrieving health information for a given provider
44///
45/// When messages are published on this subject, hosts trigger health checks on providers (i.e. a [`HealthCheckRequest`])
46/// and return relevant results (i.e. a [`HealthCheckResponse`]).
47#[must_use]
48pub fn health_subject(lattice: &str, provider_key: &str) -> String {
49    format!("wasmbus.rpc.{lattice}.{provider_key}.health")
50}
51
52/// Generate the wasmbus RPC subject for shutting down a given provider
53///
54/// When messages are published on this subject, hosts perform shutdown (cleanly if possible).
55#[must_use]
56pub fn shutdown_subject(lattice: &str, provider_key: &str, link_name: &str) -> String {
57    format!("wasmbus.rpc.{lattice}.{provider_key}.{link_name}.shutdown")
58}
59
60/// Generate the wasmbus RPC subject for delivering config updates to a given provider
61///
62/// When messages are published on this subject, providers up the perform shutdown (cleanly if possible).
63///
64/// NOTE that the NATS message body limits (default 1MiB) apply to these messages
65#[must_use]
66pub fn provider_config_update_subject(lattice: &str, provider_key: &str) -> String {
67    format!("wasmbus.rpc.{lattice}.{provider_key}.config.update")
68}