Enabling stashing
PassWithStashing() as a SpawnOption:
WithStashing(), calls to Stash, Unstash, or UnstashAll record ErrStashBufferNotSet.
API
| Method | Purpose |
|---|---|
ctx.Stash() | Buffer the current message. It will not be processed until unstashed. The message is moved to a side buffer; the actor continues with the next message. |
ctx.Unstash() | Dequeue the oldest stashed message and prepend it to the mailbox. Processes one message. |
ctx.UnstashAll() | Move all stashed messages back to the mailbox in arrival order. |
When to use
- Behavior transitions β Stash messages you cannot handle in the current behavior; unstash after switching.
- Conditional processing β Stash messages until a condition is met (e.g., dependency ready, lock acquired).
- Reentrancy β With
StashNonReentrantmode, in-flight Ask requests cause user messages to be stashed automatically until the request completes.
Example
Stash size
pid.StashSize() returns the number of messages currently in the stash. Useful for metrics or backpressure.