Deployment and operations
APP_KEY and the endpoint prefix
The render fragment and Linker POST routes share one site-specific prefix derived from APP_KEY (/{prefix}/render, /{prefix}/linker…):
prefix = hex(HMAC-SHA256(APP_KEY, "cg:endpoint-prefix:v1"))[..16]
meta name = "m-" + hex(HMAC-SHA256(APP_KEY, "cg:endpoint-meta:v1"))[..16]- Every server for one site must use the same key, or a page served by machine A gets 404 from machine B.
- After changing the key, the prefix, sessions and signed URLs are all invalid; pages already open must be reloaded.
- With the same key as the PHP version you get the same prefix (watch for route conflicts when both run on one domain).
- The prefix isn't a secret; it just makes each site's endpoints live somewhere different, so generic scanning is harder.
endpoints::prefix() returns the current prefix, endpoints::meta_name() the meta name, and endpoints::derive(purpose, key) is the underlying function. With an empty APP_KEY the prefix is sixteen 0s; always set a key in production.
Linker storage
Stateful instances and locks use Laravel for Rust's default cache store (CACHE_STORE in .env; the starter defaults to database).
| Item | Value | Description |
|---|---|---|
| Instance key | linker:{user}:{uuid} | Stores the serialized struct, rev and expiry |
| Lock key | linker:dispatch:{user}:{instance} | Queues calls on the same instance |
| Idle timeout | 30 minutes (store::IDLE) | Slides forward on each call |
| Renewal interval | 90 seconds (store::RENEW_INTERVAL) | Frequent calls don't rewrite the expiry every time |
| Maximum lifetime | 8 hours (store::MAX_LIFETIME) | Counted from mount; an absolute limit |
| Lock lease | 60 seconds | Must exceed the longest single call |
| Lock wait | 3 seconds | On timeout returns service ("操作忙碌中", busy) |
These are constants, not settings. To change them, edit crates/cg-component/src/runtime/linker/store.rs and mod.rs.
- Several machines: the cache store must be shared (Redis or the same database);
file/memorywon't do. - Clearing the cache clears every in-progress flow, and users get
expiredon their next action. - Deploying a change to a Linker struct's fields: old instances fail to deserialize →
expired.#[serde(default)]lets new fields read old data, for a smooth upgrade.
Don't lengthen lifetimes to let users "resume"
Server instances don't survive a reload (the uuid goes away with the DOM). To let users resume, write a DB draft row.
Sessions and render tokens
Render tokens live in the session (key cg_render_before, at most 16). So:
- The session driver must be shared across machines (the starter defaults to
database). - Session expires → tokens are invalid → fragment requests get 419 and the browser console shows an error; the user has to reload the page.
Production
APP_DEBUG=false: removes error details from Linkerfatalresponses and error pages.- When HTTPS is terminated by a proxy in front, check Laravel for Rust's trusted proxy settings, or the scheme of signed URLs won't match (Stateless calls get 403).
- Build the front-end assets with
npm run build;resources/js/cg/andcg-component.cssare already in the Vite entries. /upis the starter's built-in health check route.
Health checks and CI
Rust has no php artisan linker:list --health. Instead:
cargo test --workspacetests/runtime.rswalks through the whole protocol: meta tags, one-time tokens, the prefix, Stateful state and rev, other users' instances, logged-out calls, Stateless signature renewal.crates/cg-component/tests/php_parity.rschecks that component output matches the PHP snapshots.- The one-to-one check of the action table is in Commands.
In CI, in-memory SQLite is enough for tests:
DB_CONNECTION=sqlite DATABASE_URL='sqlite::memory:' cargo test --workspace