Concept
By default, an actor processes messages strictly one at a time. Reentrancy allows an actor to process other messages while waiting for an async response fromRequest or RequestName.
Request and RequestName are the non-blocking counterparts of Ask and SendSync. They return immediately with a RequestCall handle instead of blocking until a reply arrives. The reply is delivered back through the actor’s mailbox, preserving single-threaded processing.
Modes
Enabling reentrancy
Reentrancy is opt-in. PassWithReentrancy when spawning the actor:
Without
WithReentrancy, calls to Request or RequestName fail with ErrReentrancyDisabled.
Request and RequestName
On failure to initiate the request,
Err is set on the context and the returned call is nil. Check ctx.Err() or use ctx.getError() in tests.
RequestCall
The returnedRequestCall lets you:
Continuations registered with
Then run on the actor’s mailbox thread when the request completes, preserving single-threaded access to actor state. Call Then from within Receive to ensure correct execution.
Per-call options
Errors
StashNonReentrant and stashing
InStashNonReentrant mode, user messages are automatically stashed while any stash-mode request is in flight. A stash buffer is created on demand; you do not need WithStashing() for reentrancy-driven stashing. When the last blocking request completes, stashed messages are unstashed and processed in order.
When to use
- Actors that make async requests and need to stay responsive (e.g. fan-out, long I/O via PipeTo).
- Avoiding deadlock in call cycles (A -> B -> A). Use
AllowAllso A can process B’s reply while waiting. - Strict ordering when you must not interleave user messages with async responses—use
StashNonReentrant.
Production notes
- Prefer AllowAll for throughput and to avoid deadlocks in call cycles.
- Use StashNonReentrant only when strict message ordering is required. Pair it with:
- A finite
MaxInFlightlimit to bound memory. - Per-request timeouts (
WithRequestTimeout) to avoid unbounded stashing if dependencies stall.
- A finite
AllowAllcan introduce state races if your logic assumes strict ordering between request and response.- Mixed-version clusters may decode unknown modes as
Off, disabling async requests.
Example
Reentrancy enables safe request/response cycles without deadlock. Flow: Client → ActorA → ActorB → ActorA → Client. ActorA usesRequestName (non-blocking); ActorB uses Ask (blocking). Without reentrancy, ActorA would block and the cycle would deadlock.