Skip to content

Instantly share code, notes, and snippets.

@oklahomer
Last active February 14, 2019 06:02
Show Gist options
  • Save oklahomer/44ef467691f4e45337b3bb742b3f5fd2 to your computer and use it in GitHub Desktop.
Save oklahomer/44ef467691f4e45337b3bb742b3f5fd2 to your computer and use it in GitHub Desktop.
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 both send a message with sender information
ctx.Request(p.pongPid, &ping{})
p.pongPid.Request(&ping{}, ctx.Self())
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")
// Below both work
ctx.Respond(&pong{})
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