Skip to main content
Actors can change their message-handling logic at runtime using behaviors. A behavior is a function func(ctx *ReceiveContext) that processes messages. The default behavior is the actor’s Receive method.

Behavior type

Behaviors are stored on a stack. The top of the stack handles each incoming message.

API

Become vs BecomeStacked

  • Become — Swap behavior. Use when transitioning to a new state and you do not need to return to the previous one.
  • BecomeStacked — Push behavior. Use when you need to temporarily handle messages differently, then pop back (e.g., a modal dialog, a temporary protocol phase).

Example

An account actor that switches behaviors based on authentication and transfer confirmation:

Stashing with behaviors

When switching behaviors, you may want to defer processing of the current message. Use ctx.Stash() to buffer it, then ctx.Unstash() or ctx.UnstashAll() after changing behavior. Requires WithStashing() at spawn. See Stashing.