folk_api/role.rs
1//! Process role and placement — the master/worker contract (phase 79).
2//!
3//! Folk's runtime model is **fork-after-warm**: a single-threaded master
4//! process warms PHP + bootstraps the framework, then `fork()`s N worker
5//! processes. The master never executes PHP — it supervises (respawn,
6//! per-worker memory, force-kill) and owns shared state (the metrics
7//! shared-memory segment, the in-memory jobs queue). Each worker owns its
8//! requests end-to-end and dispatches into the PHP of *its own* process.
9//!
10//! A plugin therefore needs to know two things:
11//!
12//! * **where it runs** — declared up front via [`Plugin::placement`](crate::Plugin::placement);
13//! the core only boots a plugin in the process(es) its placement names;
14//! * **which process it is in right now** — observed at boot via
15//! [`PluginContext::role`](crate::PluginContext::role), so a `Both` plugin can
16//! branch on master vs worker behaviour.
17
18/// Which process a plugin instance is executing in.
19///
20/// Supplied per-process in [`PluginContext::role`](crate::PluginContext::role):
21/// the master gets [`PluginRole::Master`], each forked worker gets
22/// [`PluginRole::Worker`] carrying its slot id.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum PluginRole {
25 /// The supervisor process. Holds shared state (metrics segment, in-memory
26 /// jobs queue) and supervises the worker pool. **Does not execute PHP** —
27 /// the `executor` in its [`PluginContext`](crate::PluginContext) is a
28 /// [`NoopExecutor`](crate::NoopExecutor) and returns `Err` if called.
29 Master,
30 /// A forked worker process. Its `executor` dispatches into the PHP runtime
31 /// of *this* process (zero-copy, one request at a time).
32 Worker {
33 /// Stable per-worker slot id (`0..count`). The master uses the same id
34 /// when respawning a dead worker into the slot.
35 worker_id: u32,
36 },
37}
38
39impl PluginRole {
40 /// `true` for [`PluginRole::Master`].
41 pub fn is_master(&self) -> bool {
42 matches!(self, PluginRole::Master)
43 }
44
45 /// `true` for [`PluginRole::Worker`].
46 pub fn is_worker(&self) -> bool {
47 matches!(self, PluginRole::Worker { .. })
48 }
49
50 /// The worker slot id, or `None` in the master.
51 pub fn worker_id(&self) -> Option<u32> {
52 match self {
53 PluginRole::Worker { worker_id } => Some(*worker_id),
54 PluginRole::Master => None,
55 }
56 }
57}
58
59/// Where a plugin should be booted in the fork-after-warm model.
60///
61/// Declared via [`Plugin::placement`](crate::Plugin::placement). The core boots
62/// a plugin only in the process(es) named here, passing the matching
63/// [`PluginRole`] in the [`PluginContext`](crate::PluginContext).
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum Placement {
66 /// Boot only in the master (e.g. `process` supervisor, the `metrics`
67 /// scrape server). The plugin never dispatches PHP.
68 MasterOnly,
69 /// Boot only in each worker (e.g. `http`/`grpc` listeners on
70 /// `SO_REUSEPORT`, `jobs` competing-consumers). This is the default.
71 WorkerOnly,
72 /// Boot in both master and every worker; the plugin branches on
73 /// [`PluginContext::role`](crate::PluginContext::role).
74 Both,
75}
76
77impl Placement {
78 /// Whether a plugin with this placement should be booted under `role`.
79 pub fn includes(&self, role: PluginRole) -> bool {
80 match self {
81 Placement::MasterOnly => role.is_master(),
82 Placement::WorkerOnly => role.is_worker(),
83 Placement::Both => true,
84 }
85 }
86}