Skip to content

Instantly share code, notes, and snippets.

@oklahomer
Last active February 14, 2019 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oklahomer/2cd42e0c19abf3d10b671f5a98e71571 to your computer and use it in GitHub Desktop.
Save oklahomer/2cd42e0c19abf3d10b671f5a98e71571 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/AsynkronIT/protoactor-go/actor"
"time"
)
type ping struct{}
type pong struct{}
func main() {
props := actor.FromFunc(func(ctx actor.Context) {
switch ctx.Message().(type) {
case *ping:
// This fails to get sender
// because the message came
// from outside of actor system
//
// Below execution leads to dead letter
// 2018/09/14 22:40:02 [ACTOR] [DeadLetter] pid="nil" message=&{} sender="nil"
ctx.Respond(&pong{})
// Below execution causes a panic since Sender() returns nil.
// Actor crashes and that causes supervisor to restart this failing actor.
// 2018/09/14 22:40:02 [MAILBOX] [ACTOR] Recovering actor="nonhost/$1" reason="runtime error: invalid memory address or nil pointer dereference" stacktrace="github.com/AsynkronIT/protoactor-go/actor.(*PID).ref:26"
// 2018/09/14 22:40:02 [ACTOR] [SUPERVISION] actor="nonhost/$1" directive="RestartDirective" reason="runtime error: invalid memory address or nil pointer dereference"
ctx.Sender().Tell(&pong{})
}
})
pid := actor.Spawn(props)
pid.Tell(&ping{})
time.Sleep(1 * time.Second) // Just to make sure system ends after actor execution
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment