forked from asynkron/protoactor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.44 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
package main
import (
"fmt"
console "github.com/AsynkronIT/goconsole"
"github.com/ChaokunChang/protoactor-go/actor"
)
type hello struct{ Who string }
type parentActor struct{}
func (state *parentActor) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *hello:
props := actor.PropsFromProducer(newChildActor)
child := context.Spawn(props)
context.Send(child, msg)
}
}
func newParentActor() actor.Actor {
return &parentActor{}
}
type childActor struct{}
func (state *childActor) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *actor.Started:
fmt.Println("Starting, initialize actor here")
case *actor.Stopping:
fmt.Println("Stopping, actor is about to shut down")
case *actor.Stopped:
fmt.Println("Stopped, actor and its children are stopped")
case *actor.Restarting:
fmt.Println("Restarting, actor is about to restart")
case *hello:
fmt.Printf("Hello %v\n", msg.Who)
panic("Ouch")
}
}
func newChildActor() actor.Actor {
return &childActor{}
}
func main() {
decider := func(reason interface{}) actor.Directive {
fmt.Println("handling failure for child")
return actor.StopDirective
}
supervisor := actor.NewOneForOneStrategy(10, 1000, decider)
rootContext := actor.EmptyRootContext
props := actor.
PropsFromProducer(newParentActor).
WithSupervisor(supervisor)
pid := rootContext.Spawn(props)
rootContext.Send(pid, &hello{Who: "Roger"})
console.ReadLine()
}