Actors in Go: How lockstep Runs One Actor in Your Program and in Tests
说明与用途
文章展示了如何使用 lockstep 在 Go 中实现演员模型:类型化消息、受限邮箱、非阻塞效果和确定性测试。
问题
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