-
Notifications
You must be signed in to change notification settings - Fork 97
/
main.go
74 lines (63 loc) · 1.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"time"
"github.com/anthdm/hollywood/actor"
)
var restarts = 0
type barReceiver struct {
data string
}
func newBarReceiver(data string) actor.Producer {
return func() actor.Receiver {
return &barReceiver{
data: data,
}
}
}
func (r *barReceiver) Receive(ctx *actor.Context) {
switch msg := ctx.Message().(type) {
case actor.Started:
time.Sleep(time.Second)
if restarts < 2 {
restarts++
panic("I will need to restart")
}
fmt.Println("bar recovered and started with initial state:", r.data)
case message:
fmt.Println(msg.data)
case actor.Stopped:
fmt.Println("bar stopped")
}
}
type fooReceiver struct {
barPID *actor.PID
}
func newFooReceiver() actor.Receiver {
return &fooReceiver{}
}
func (r *fooReceiver) Receive(ctx *actor.Context) {
switch msg := ctx.Message().(type) {
case actor.Started:
fmt.Println("foo started")
case message:
r.barPID = ctx.SpawnChild(newBarReceiver(msg.data), "bar", actor.WithID(msg.data))
fmt.Println("received and starting bar:", r.barPID)
case actor.Stopped:
fmt.Println("foo will stop")
}
}
type message struct {
data string
}
func main() {
e, err := actor.NewEngine(actor.NewEngineConfig())
if err != nil {
panic(err)
}
pid := e.Spawn(newFooReceiver, "foo")
e.Send(pid, message{data: fmt.Sprintf("msg_%d", 1)})
time.Sleep(time.Second * 8)
e.Poison(pid)
time.Sleep(time.Second)
}