folk_api/rpc.rs
1//! RPC method registration: plugins expose methods that callers (PHP workers
2//! or external admin tools) can invoke on the master.
3
4use std::future::Future;
5use std::pin::Pin;
6use std::sync::Arc;
7
8use anyhow::Result;
9use async_trait::async_trait;
10use bytes::Bytes;
11
12/// Static description of an RPC method (advertised at server start; used
13/// for introspection and `folk admin list-methods`).
14#[derive(Debug, Clone)]
15pub struct RpcMethodDef {
16 /// Method name, dotted: `"http.connections"`, `"jobs.stats"`.
17 pub name: String,
18 /// Human-readable description for documentation / admin UIs.
19 pub description: String,
20}
21
22impl RpcMethodDef {
23 pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
24 Self {
25 name: name.into(),
26 description: description.into(),
27 }
28 }
29}
30
31/// Boxed future returned by an RPC handler.
32pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
33
34/// Type alias for an RPC handler: takes raw request bytes, returns raw response bytes.
35pub type RpcHandler = Arc<dyn Fn(Bytes) -> BoxFuture<'static, Result<Bytes>> + Send + Sync>;
36
37/// Allows plugins to register RPC method handlers at boot.
38///
39/// `folk-core` provides the concrete implementation.
40#[async_trait]
41pub trait RpcRegistrar: Send + Sync + 'static {
42 /// Register a method by name. The handler receives raw request bytes and
43 /// returns raw response bytes (typically MessagePack-encoded payloads).
44 async fn register_raw(&self, name: String, handler: RpcHandler);
45}