-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
main.go
90 lines (71 loc) · 1.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"github.com/AsynkronIT/goconsole"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/AsynkronIT/protoactor-go/cluster"
"github.com/AsynkronIT/protoactor-go/cluster/consul"
"github.com/AsynkronIT/protoactor-go/examples/cluster-broadcast/shared"
"github.com/AsynkronIT/protoactor-go/remote"
"log"
"time"
)
func main () {
startNode(8081)
fmt.Print("\nBoot other nodes and press Enter\n")
console.ReadLine()
calcAdd("Eggs", 1)
calcAdd("Eggs", 10)
calcAdd("Bananas", 1000)
calcAdd("Meat", 3)
calcAdd("Meat", 9000)
getAll()
console.ReadLine()
cluster.Shutdown(true)
}
func startNode(port int64) {
// how long before the grain poisons itself
timeout := 10 * time.Minute
// this node knows about Hello kind
remote.Register("Calculator", actor.PropsFromProducer(func() actor.Actor {
return &shared.CalculatorActor{
Timeout: &timeout,
}
}))
// this node knows about Hello kind
remote.Register("Tracker", actor.PropsFromProducer(func() actor.Actor {
return &shared.TrackerActor{
Timeout: &timeout,
}
}))
shared.CalculatorFactory(func() shared.Calculator {
return &shared.CalcGrain{}
})
shared.TrackerFactory(func() shared.Tracker {
return &shared.TrackGrain{}
})
cp, err := consul.New()
if err != nil {
log.Fatal(err)
}
cluster.Start("mycluster", fmt.Sprintf("127.0.0.1:%v", port), cp)
}
func calcAdd(grainId string, addNumber int64) {
calcGrain := shared.GetCalculatorGrain(grainId)
total1, err := calcGrain.Add(&shared.NumberRequest{ Number: addNumber})
if err != nil {
panic(err)
}
fmt.Printf("Grain: %v - Total: %v \n", calcGrain.ID, total1.Number)
}
func getAll() {
trackerGrain := shared.GetTrackerGrain("singleTrackerGrain")
totals, err := trackerGrain.BroadcastGetCounts(&shared.Noop{})
if err != nil {
panic(err)
}
fmt.Println("--- Totals ---")
for grainId, count := range totals.Totals {
fmt.Printf("%v : %v\n", grainId, count)
}
}