Skip to content

Instantly share code, notes, and snippets.

Last active February 14, 2019 06:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oklahomer/3b9b22ab159d473a13068cf2853a4b67 to your computer and use it in GitHub Desktop.
package main
import (
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 work.
//future := p.pongPid.RequestFuture(&ping{}, time.Second)
future := ctx.RequestFuture(p.pongPid, &ping{}, time.Second)
result, err := future.Result()
if err != nil {
log.Printf("Received %#v", result)
case *pong:
// Never comes here.
// When the pong actor responds to the sender,
// the sender is not a ping actor but a future process.
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 in this example, but their behavior slightly differ.
// ctx.Sender().Tell() panics and recovers if the sender is nil;
// while ctx.Respond() checks the presence of sender and redirects the message to dead letter process
// when sender is absent.
pongPid := actor.Spawn(pongProps)
pingProps := actor.FromProducer(func() actor.Actor {
return &pingActor{
pongPid: pongPid,
pingPid := actor.Spawn(pingProps)
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