SessionActor Pattern
The SessionActor pattern is the core of Monolex’s terminal architecture. It eliminates all locks by ensuring a single owner for all session state.The Problem: Lock Contention
Traditional terminal architectures suffer from lock contention:- Lock contention under high load
- Potential for deadlocks
- Complex reasoning about concurrent access
- Performance degradation
The Solution: Single Owner
SessionActor owns all state. No locks required.Implementation
Core Structure
Command Pattern
All operations are sent as commands through MPSC channels:Event Loop
Benefits
| Aspect | With Locks | SessionActor |
|---|---|---|
| Deadlocks | Possible | Impossible |
| Lock contention | Under load | None |
| Reasoning | Complex | Simple |
| Performance | Degrades | Consistent |
| Code complexity | High | Low |
GridWorker: Per-Session Processing
Each session has its own GridWorker running in a separate task:Message Flow Example
Why Not Other Patterns?
Why not RwLock?
- Still has contention between readers and writers
- Deadlock potential with multiple locks
- Complex upgrade/downgrade semantics
Why not Actor per Operation?
- Too fine-grained, message passing overhead
- Loses locality of related state
- Harder to reason about ordering
Why SessionActor?
- Natural boundary: one actor per “thing” (terminal sessions)
- All related state co-located
- Simple event loop, easy to understand
- Guaranteed ordering within actor
SMPC/OFAC Applied
| Principle | Application |
|---|---|
| SMPC | Single owner = simple reasoning |
| MPSC channels = simple communication | |
| No locks = no complexity | |
| OFAC | Actor pattern emerged from need |
| Per-session workers = natural parallelism | |
| Commands = organized chaos |