Last active
February 14, 2019 06:03
-
-
Save oklahomer/7cd359f407f1cd59df7d73a20e1bafa0 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" | |
"log" | |
"time" | |
) | |
type pong struct { | |
} | |
type ping struct { | |
} | |
type pingActor struct { | |
pongPid *actor.PID | |
} | |
func (p *pingActor) Receive(ctx actor.Context) { | |
switch ctx.Message().(type) { | |
case struct{}: | |
// Below does not set ctx.Self() as sender, | |
// and hence the recipient has no knowledge of the sender | |
// even though the message is sent from another actor via actor.Context. | |
// | |
ctx.Tell(p.pongPid, &ping{}) | |
case *pong: | |
log.Print("Received pong message") | |
} | |
} | |
func main() { | |
pongProps := actor.FromFunc(func(ctx actor.Context) { | |
switch ctx.Message().(type) { | |
case *ping: | |
log.Print("Received ping message") | |
// 2018/09/15 02:01:27 [ACTOR] [DeadLetter] pid="nil" message=&{} sender="nil" | |
ctx.Respond(&pong{}) | |
// 2018/09/15 02:01:27 [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/15 02:01:27 [ACTOR] [SUPERVISION] actor="nonhost/$1" directive="RestartDirective" reason="runtime error: invalid memory address or nil pointer dereference" | |
ctx.Sender().Tell(&pong{}) | |
default: | |
} | |
}) | |
pongPid := actor.Spawn(pongProps) | |
pingProps := actor.FromProducer(func() actor.Actor { | |
return &pingActor{ | |
pongPid: pongPid, | |
} | |
}) | |
pingPid := actor.Spawn(pingProps) | |
pingPid.Tell(struct{}{}) | |
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