Overview
Mesophyll runs on the master process and maintains WebSocket connections to all worker processes. It enables:- Event dispatching from master to workers
- Response collection from workers back to master
- Heartbeat monitoring for connection health
- Database state synchronization
- Template shop updates across all workers
Mesophyll is currently implemented as part of the master process. Future plans include using it as a base unit for sandboxing through projects like Khronos DAPI.
Architecture
The Mesophyll system consists of two main components:Server Component
TheMesophyllServer runs on the master process and:
- Listens for WebSocket connections from workers (src/mesophyll/server.rs:42-43)
- Authenticates workers using token-based authentication (src/mesophyll/server.rs:86-96)
- Manages active connections in a concurrent map (src/mesophyll/server.rs:18)
- Provides HTTP endpoints for database operations (src/mesophyll/server.rs:44-48)
Client Component
TheMesophyllClient runs in each worker process and:
- Establishes WebSocket connection to the master (src/mesophyll/client.rs:45)
- Sends periodic heartbeats to maintain connection (src/mesophyll/client.rs:115-118)
- Receives and processes server messages (src/mesophyll/client.rs:52-94)
- Automatically reconnects on connection loss (src/mesophyll/client.rs:29-38)
WebSocket Protocol
Connection Establishment
Workers connect to the Mesophyll server using WebSocket:Message Format
All messages are serialized using MessagePack (rmp_serde) and sent as binary WebSocket frames (src/mesophyll/server.rs:524-538).Message Types
Server Messages
Messages sent from server to client (src/mesophyll/message.rs:6-33):Hello
Sent after connection establishment to configure the heartbeat interval:DispatchEvent
Dispatches a custom template event to a worker:- If
req_idisSome, the worker must send aDispatchResponse(src/mesophyll/server.rs:472-481) - If
req_idisNone, it’s a fire-and-forget dispatch (src/mesophyll/server.rs:496-500)
RunScript
Executes arbitrary Luau code in a worker (useful for internal tooling):DropWorker
Requests a worker to drop a tenant VM:Client Messages
Messages sent from client to server (src/mesophyll/message.rs:35-45):DispatchResponse
Response to a DispatchEvent or RunScript request:Heartbeat
Periodic heartbeat to keep connection alive:Request/Response Pattern
Mesophyll uses a request ID correlation system for async request/response:- Server generates a random u64 request ID (src/mesophyll/server.rs:457-464)
- Server registers a oneshot channel handler for the request ID
- Server sends the message with the request ID
- Client processes the message and sends back a DispatchResponse with the same request ID
- Server correlates the response using the request ID (src/mesophyll/server.rs:427-430)
- The awaiting task receives the response through the oneshot channel
Database HTTP Endpoints
In addition to WebSocket communication, Mesophyll provides HTTP/2 endpoints for database operations (src/mesophyll/server.rs:44-48):Tenant State Endpoints
GET /db/tenant-states- List all tenant states for a worker (src/mesophyll/server.rs:103-112)POST /db/tenant-state- Set tenant state for a specific tenant (src/mesophyll/server.rs:135-161)
Key-Value Endpoints
POST /db/kv- Perform key-value operations (Get, Set, Delete, Find, ListScopes) (src/mesophyll/server.rs:164-230)POST /db/public-global-kv- Public global KV operations (Find, Get) (src/mesophyll/server.rs:233-267)POST /db/global-kv- Global KV operations (Create, Delete) (src/mesophyll/server.rs:270-309)
Authentication
All HTTP endpoints require query parameters:id- Worker IDtoken- Authentication tokentenant_type&tenant_id- For tenant-specific operations
Request/Response Format
All HTTP requests and responses use MessagePack serialization (src/mesophyll/server.rs:541-558).Database Client
TheMesophyllDbClient provides a high-level interface for workers to access database operations (src/mesophyll/client.rs:126-323):
Example Usage
- Uses HTTP/2 with prior knowledge (src/mesophyll/client.rs:141-144)
- Constructs URLs with authentication parameters (src/mesophyll/client.rs:148-158)
- Encodes requests and decodes responses using MessagePack (src/mesophyll/client.rs:173-192)
Connection Management
Heartbeat System
Clients send heartbeats at regular intervals to maintain connection health:Automatic Reconnection
Clients automatically reconnect on connection failure:Reconnection Handling
When a worker reconnects, the server automatically replaces the old connection (src/mesophyll/server.rs:328-330):Concurrency
Mesophyll handles multiple concurrent operations:- Dispatches: Uses
FuturesUnorderedto process multiple event dispatches concurrently (src/mesophyll/client.rs:48-49) - Script runs: Separate
FuturesUnorderedfor script executions (src/mesophyll/client.rs:49) - Connection map: Uses
DashMapfor lock-free concurrent access to connections (src/mesophyll/server.rs:18)
Error Handling
Connection Errors
- Invalid worker ID returns
404 NOT_FOUND(src/mesophyll/server.rs:87-90) - Invalid token returns
401 UNAUTHORIZED(src/mesophyll/server.rs:92-96) - Connection failures trigger automatic reconnection on client side
Message Processing
- Failed message deserialization is logged but doesn’t close the connection (src/mesophyll/server.rs:422-424)
- Unmatched request IDs are silently ignored (src/mesophyll/server.rs:428-430)
Database Operations
- Operation failures return
500 INTERNAL_SERVER_ERRORwith error message - Invalid request bodies return
400 BAD_REQUEST
Transport Layer
Mesophyll also provides a generic RPC transport abstraction (src/mesophyll/transport.rs) for future extensibility:- In-memory transport for testing
- HTTP/2 transport for production
- Easy addition of new RPC endpoints
The transport layer abstraction is currently defined but not actively used. The primary communication uses the WebSocket-based protocol described above.
Future Enhancements
- Sandboxing capabilities through Khronos DAPI
- Template shop update broadcasting across workers
- Enhanced monitoring and metrics collection
- Distributed Mesophyll server deployment