Last active
February 14, 2019 06:03
-
-
Save oklahomer/2cd42e0c19abf3d10b671f5a98e71571 to your computer and use it in GitHub Desktop.
An example code for https://blog.oklahome.net/2018/09/protoactor-go-messaging-protocol.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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