Overview
Mesophyll is the coordination layer that enables communication between the master process and worker processes in the Template Worker system. It replaces the older HTTP/2-based WorkerProcessComm system with a more flexible WebSocket protocol.Name Origin: In plant biology, mesophyll is the internal tissue of a leaf that contains chloroplasts and performs photosynthesis. Similarly, Mesophyll in Template Worker is the “internal tissue” that enables the system to function by coordinating between components.
Architecture
MesophyllServer
TheMesophyllServer runs in the master process and manages connections from all worker processes.
Structure
Components
- idents: Maps worker IDs to authentication tokens (64-character random strings)
- conns: Active WebSocket connections indexed by worker ID
- db_state: Shared database state for all workers
Initialization
The server generates unique tokens for each worker:HTTP API
Mesophyll exposes several HTTP/2 endpoints for worker communication:/ws - WebSocket Upgrade
/ws - WebSocket Upgrade
Upgrades HTTP connection to WebSocket for real-time communication.Query Parameters:
id: Worker IDtoken: Authentication token
/db/tenant-states - List Tenant States
/db/tenant-states - List Tenant States
Returns all tenant states assigned to a specific worker.Used during worker initialization to populate the tenant state cache.Reference: src/mesophyll/server.rs:103-112
/db/tenant-state - Set Tenant State
/db/tenant-state - Set Tenant State
Updates the state for a specific tenant.Query Parameters:
tenant_type: “guild” or “user”tenant_id: Tenant identifier
TenantStateReference: src/mesophyll/server.rs:135-161/db/kv - Key-Value Operations
/db/kv - Key-Value Operations
Performs key-value operations (get, set, delete, find) for tenant-scoped data.Body: MessagePack-encoded
KeyValueOpReference: src/mesophyll/server.rs:164-230/db/public-global-kv - Public Global KV
/db/public-global-kv - Public Global KV
Read-only access to global key-value data (template shop, etc.).Reference: src/mesophyll/server.rs:233-267
/db/global-kv - Global KV Management
/db/global-kv - Global KV Management
Create and delete global key-value entries.Reference: src/mesophyll/server.rs:270-309
MesophyllServerConn
Represents a single WebSocket connection from a worker to the server.Structure
Message Dispatching
The server can dispatch events to workers and wait for responses:Request Tracking: Each request gets a unique 64-bit random ID to correlate requests with responses. The
DispatchHandlerDropGuard ensures handlers are cleaned up even if the future is dropped.MesophyllClient
TheMesophyllClient runs in worker processes and maintains a WebSocket connection to the server.
Structure
Connection Management
The client automatically reconnects on disconnect:Message Processing
The client processes incoming messages in a select loop:Message Protocol
Mesophyll uses MessagePack encoding for efficient binary serialization.Server Messages
Hello
Hello
Sent by server upon connection to configure heartbeat interval.Default: 5000ms (MESOPHYLL_DEFAULT_HEARTBEAT_MS)
DispatchEvent
DispatchEvent
Requests worker to dispatch an event to a tenant’s VM.
req_id: Some(id): Wait for responsereq_id: None: Fire-and-forget
RunScript
RunScript
Requests worker to execute arbitrary Luau code.Used by Fauxpas staff API.
DropWorker
DropWorker
Requests worker to drop a tenant’s VM and clean up resources.
Client Messages
- Heartbeat: Sent periodically to keep connection alive
- DispatchResponse: Response to DispatchEvent, RunScript, or DropWorker
MesophyllDbClient
Workers useMesophyllDbClient to access database operations via HTTP/2:
Database Operations
- Tenant State
- Key-Value Store
- Global Key-Value
WorkerDB Abstraction
TheWorkerDB enum provides a unified interface for database access:
- Thread Pool Mode: Direct database access via
DbState - Process Pool Mode: Database access via Mesophyll HTTP/2 API
- Transparent Switching: Application code doesn’t need to know which mode is used
Heartbeat System
Mesophyll uses heartbeats to detect disconnected workers:Security Model
Authentication
- Master generates 64-character random token for each worker
- Token passed via environment variable
MESOPHYLL_CLIENT_TOKEN(src/main.rs:436) - Worker includes token in WebSocket upgrade and HTTP requests
- Server validates token before accepting connection
Token Generation
Future Enhancements
From the module documentation:In the future, it is a goal for Mesophyll to be a base unit of sandboxing as well through projects like khronos dapiPlanned improvements:
- Enhanced sandboxing capabilities
- Multi-master support for high availability
- Worker-to-worker direct communication
- Template shop update broadcasts
Performance Characteristics
- Protocol: WebSocket (persistent connections) + HTTP/2 (database operations)
- Serialization: MessagePack (efficient binary format)
- Connection Pooling: reqwest HTTP/2 client with prior knowledge
- Message Pattern: Request-response with async correlation
- Reconnection: Automatic with exponential backoff
Next Steps
Worker System
Understand how workers use Mesophyll
Components
Explore other system components