forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (60 loc) · 1.82 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
package main
import (
"fmt"
"math/rand"
"time"
"github.com/halturin/ergo"
"github.com/halturin/ergo/etf"
)
func main() {
// create nodes for producer and consumers
fmt.Println("Starting nodes 'node_abc@localhost' and 'node_def@localhost'")
node_abc := ergo.CreateNode("node_abc@localhost", "cookies", ergo.NodeOptions{})
node_def := ergo.CreateNode("node_def@localhost", "cookies", ergo.NodeOptions{})
// create producer and consumer objects
producer := &Producer{}
consumer := &Consumer{}
fmt.Println("Spawn producer on 'node_abc@localhost'")
p1, errP := node_abc.Spawn("producer", ergo.ProcessOptions{}, producer, nil)
if errP != nil {
panic(errP)
}
fmt.Println("Spawn 2 consumers on 'node_def@localhost'")
c1, errC1 := node_def.Spawn("even", ergo.ProcessOptions{}, consumer, nil)
if errC1 != nil {
panic(errC1)
}
c2, errC2 := node_def.Spawn("odd", ergo.ProcessOptions{}, consumer, nil)
if errC2 != nil {
panic(errC2)
}
fmt.Println("Subscribe consumer 'even' with min events = 1 and max events 2 (even numbers only)")
c1_sub_opts := ergo.GenStageSubscribeOptions{
MinDemand: 1,
MaxDemand: 2,
Partition: 0,
}
consumer.Subscribe(c1, etf.Tuple{"producer", "node_abc@localhost"}, c1_sub_opts)
fmt.Println("Subscribe consumer 'odd' with min events = 2 and max events 4 (odd numbers only)")
c2_sub_opts := ergo.GenStageSubscribeOptions{
MinDemand: 2,
MaxDemand: 4,
Partition: 1,
}
consumer.Subscribe(c2, etf.Tuple{"producer", "node_abc@localhost"}, c2_sub_opts)
for {
n := rand.Intn(9) + 1
numbers := generateNumbers(n)
fmt.Println("Producer. Generate random numbers and send them to consumers...", numbers)
producer.SendEvents(p1, numbers)
time.Sleep(1 * time.Second)
}
}
func generateNumbers(n int) etf.List {
l := etf.List{}
for n > 0 {
l = append(l, rand.Intn(100))
n--
}
return l
}