Actors in Go: How lockstep Runs One Actor in Your Program and in Tests
Описание и применение
В статье описывается реализация модели акторов в Go с помощью библиотеки lockstep: типизированные сообщения, ограниченные почтовые ящики, асинхронные эффекты и детерминированные тесты.
Задача
Implementing actor semantics in Go without a dedicated runtime, managing state, bounded mailboxes, and non-blocking I/O.
Результат
The shop actor correctly reserves stock before charging, uses lockstep.Do for non-blocking I/O, and runs deterministically in tests.
Рабочий процесс
- Define message types and actor struct
- Implement Receive to handle Buy, charged, and Sold messages
- Use lockstep.Spawn to create actor and obtain typed PID
- Send Buy messages to actor; lockstep.Do starts Charge effect
- Handle charged result in a later turn
- Reply with Sold message via ReplyTo
- Run same code under go test with deterministic seed
Доказательства
lockstep runs them in your program on a pool of worker goroutines.
The same actor code also runs in go test, in a simulation with its own clock and network.
lockstep.Do starts an effect and returns at once.
- Дата записи: 2026-11-02T18:14:12.456Z