Stateful 與 Stateless
兩種模式都能查詢、建立、更新、刪除資料;差別只有 Linker transport 是否保存元件 state。
| Stateful(預設) | Stateless | |
|---|---|---|
| 適合 | 精靈、草稿、跨呼叫流程 guard | 後端運算、查詢、單次命令 |
| 掛載資料 | alias + uuid + rev | alias + 有時效的簽章網址 |
| 狀態 | 存在 cache(linker:{user}:{uuid}),每次呼叫前還原 | 不存,每次呼叫 C::default() 新建 |
| 端點 | POST /{前綴}/linker | POST /{前綴}/linker/stateless/{alias}/call?user=…&absolute_expires_at=…&expires=…&signature=… |
| 請求 | {component, instance, rev, method, args} | {method, args} |
| 回應多帶 | instance、rev | 續期後的 url |
| 掛載參數 | 可以(mount(&mut self, ctx, params)) | 不接受(mount 傳了參數會回錯誤) |
| 並行 | 同 instance 鎖住;舊 rev 回 conflict | 同網址可並行;由 DB transaction、unique constraint、idempotency 保證 |
| 重整 | 建立全新 instance,未落 DB 的 state 消失 | 產生全新網址 |
| struct 欄位 | 會被存回去 | 呼叫結束就丟掉 |
切換成 Stateless
rust
use cg_component::runtime::linker::{Action, Args, LinkerComponent, LinkerContext, LinkerMode, LinkerResponse};
#[derive(Default, Serialize, Deserialize)]
pub struct Quote;
impl LinkerComponent for Quote {
const ALIAS: &'static str = "quote";
const MODE: LinkerMode = LinkerMode::Stateless;
const METHODS: &'static [&'static str] = &["price"];
async fn call(&mut self, _: &LinkerContext, _: &str, args: Args) -> laravel::Result<LinkerResponse> {
let qty = args.get("qty").and_then(|v| v.as_str()?.parse::<u32>().ok()).filter(|q| *q >= 1);
let Some(qty) = qty else {
return Ok(LinkerResponse::validation([("qty", "數量至少 1。")]));
};
Ok(LinkerResponse::ok().data("total", qty * 120))
}
}掛載方式一樣:linker::mount::<Quote, User>(&user, linker::Args::new())。範例專案的 src/cg/greeter.rs 就是 Stateless。
- Stateless 不接受掛載參數;範圍改放進每次呼叫、可驗證的參數。
- 簽章網址只證明完整性與期限,不是授權。登入、CSRF、簽章裡的 user 與目前登入者一致、Referer、
METHODS仍逐層檢查,業務權限還是要你在call裡做。 - 換了使用者(簽章裡的
user≠ 登入者)或網址被竄改回 403 policy。
有效期限
| 閒置 | 續期 | 最長 | |
|---|---|---|---|
| Stateful 實例 | 30 分鐘(store::IDLE) | 成功呼叫時;驗證失敗時最多每 90 秒(RENEW_INTERVAL) | 8 小時(MAX_LIFETIME) |
| Stateless 網址 | 30 分鐘 | 成功或驗證失敗時回新網址 | 8 小時(簽在 absolute_expires_at) |
PHP 版這些是 config/linker.php 的設定;Rust 版是 runtime/linker/store.rs 的常數。互動最低送出間隔 0.8 秒是 linker::MIN_DELAY_MS,由 mount 寫進 data-linker-min-delay。
Stateful 一次呼叫
POST /{prefix}/linker
→ 前綴不對 → 404
→ 沒登入 → 401 policy「請先登入。」
→ 欄位不合法(component / instance / rev / method / args)→ 422
→ alias 沒註冊、或不是 Stateful → 404 policy
→ cache 鎖 linker:dispatch:{user}:{instance}(租 60 秒,等 3 秒)→ 等不到 → service「操作忙碌中」
→ 讀實例:不存在、逾期、alias 不符(含別人的 uuid)→ 409 expired
→ rev 不符 → 409 conflict,帶伺服器上的 rev
→ serde 還原 struct(失敗 → expired)→ booted() → referer → METHODS → call()
→ 成功:rev + 1、存回 state、續期 → 200 { ok, data, pipeline, instance, rev }
→ 驗證失敗:state 不動、rev 不動、續期
→ 其他失敗:state 不動、rev 不動run_stateful 只有在呼叫成功時才存回 struct。call 裡改了欄位之後回 validation 或 policy,那些改動會被丟掉 —— 這是好事:失敗不留下半套狀態。
怎麼選
這個元件需要記住上一次呼叫的結果嗎?
├─ 否 → Stateless
└─ 是 → 那份記憶要撐過重新整理嗎?
├─ 是 → 寫 DB 草稿列(元件可以是 Stateless)
└─ 否 → 伺服器要擋跳步嗎?
├─ 是 → Stateful
└─ 否 → 客戶端 state 就夠,最後一次送出範例:多步驟精靈的選項 A 是 Stateful,選項 B 可以是 Stateless。
Stateful 的部署注意
實例存在 Laravel 的 cache(預設是資料庫的 cache 表,DB_CACHE_TABLE)。
- 清 cache 會把進行中的表單一起清掉。
- 水平擴充時所有節點要共用同一個 cache store,鎖也要是共用的。
- 改了 struct 的欄位後,舊實例 serde 還原失敗會回
expired,使用者重新整理即可。加欄位時給#[serde(default)]可以讓舊實例繼續用。
見部署與營運。