Lab 08: Actor Model
Overview
Step 1: Actor Interface
package actor
import "context"
// Message: anything sent to an actor's mailbox
type Message interface{}
// Actor: processes messages sequentially from its mailbox
type Actor interface {
Receive(ctx context.Context, msg Message) error
}
// ActorRef: handle to an actor (send messages without direct access)
type ActorRef struct {
name string
mailbox chan envelope
}
type envelope struct {
msg Message
sender *ActorRef // For request-reply
replyTo chan<- Message
}
func (ref *ActorRef) Send(msg Message) {
ref.mailbox <- envelope{msg: msg}
}
func (ref *ActorRef) Ask(msg Message) <-chan Message {
ch := make(chan Message, 1)
ref.mailbox <- envelope{msg: msg, replyTo: ch}
return ch
}
func (ref *ActorRef) Name() string { return ref.name }Step 2: ActorSystem
Step 3: Request-Reply Pattern
Step 4: Actor Hierarchy
Step 5: Typed Messages with Generic Actor
Step 6: Actor Benchmarks
Step 7: Stash and Become Patterns
Step 8: Capstone — Actor System with 3 Actors
Summary
Component
Pattern
Notes
Last updated
