-
Notifications
You must be signed in to change notification settings - Fork 0
/
susi-cluster.go
173 lines (157 loc) · 4.45 KB
/
susi-cluster.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"errors"
"flag"
"log"
"os"
"strings"
"github.com/webvariants/susigo"
)
var conf = flag.String("conf", "/etc/susi/susi-cluster.json", "cluster config file")
var (
config *Config
local *susigo.Susi
nodes = make(map[string]*susigo.Susi)
)
type Config struct {
Local *NodeConfig
Nodes []*NodeConfig
}
func (cfg *Config) GetNode(id string) *NodeConfig {
for _, node := range cfg.Nodes {
if node.ID == id {
return node
}
}
return nil
}
type NodeConfig struct {
ID string `json:"id"`
Addr string `json:"addr"`
Cert string `json:"cert"`
Key string `json:"key"`
ForwardConsumers []*EventConfig `json:"forwardConsumers"`
ForwardProcessors []*EventConfig `json:"forwardProcessors"`
RegisterConsumers []*EventConfig `json:"registerConsumers"`
RegisterProcessors []*EventConfig `json:"registerProcessors"`
}
type EventConfig struct {
Topic string `json:"topic"`
StripSuffix string `json:"stripSuffix"`
}
func loadConfig() error {
f, err := os.Open(*conf)
if err != nil {
return err
}
defer f.Close()
decoder := json.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
return err
}
return nil
}
func setupLocal() error {
susi, err := susigo.NewSusi(config.Local.Addr, config.Local.Cert, config.Local.Key)
if err != nil {
return err
}
local = susi
local.Wait()
log.Print("successfully connected to local susi-core")
return nil
}
func setupNodes() error {
var occuredError error
for _, node := range config.Nodes {
log.Printf("setup connection to node %v", node.ID)
susi, err := susigo.NewSusi(node.Addr, node.Cert, node.Key)
if err != nil {
occuredError = errors.New("error creating susi client for " + node.ID + ": " + err.Error())
continue
}
nodes[node.ID] = susi
setupForwardConsumers(node)
setupForwardProcessors(node)
setupRegisterConsumers(node)
setupRegisterProcessors(node)
}
return occuredError
}
func setupForwardConsumers(node *NodeConfig) {
nodeSusi := nodes[node.ID]
for idx := range node.ForwardConsumers {
eventConfig := node.ForwardConsumers[idx]
local.RegisterConsumer(eventConfig.Topic, func(event *susigo.Event) {
if eventConfig.StripSuffix != "" {
event.Topic = strings.TrimSuffix(event.Topic, eventConfig.StripSuffix)
}
log.Printf("forward consumer event %v as %v to %v", eventConfig.Topic, event.Topic, node.ID)
event.AddHeader("Event-Control", "No-Ack")
nodeSusi.Publish(*event, nil)
})
}
}
func setupForwardProcessors(node *NodeConfig) {
nodeSusi := nodes[node.ID]
for idx := range node.ForwardProcessors {
eventConfig := node.ForwardProcessors[idx]
local.RegisterProcessor(eventConfig.Topic, func(event *susigo.Event) {
if eventConfig.StripSuffix != "" {
event.Topic = strings.TrimSuffix(event.Topic, eventConfig.StripSuffix)
}
log.Printf("forward processor event %v as %v to %v", eventConfig.Topic, event.Topic, node.ID)
nodeSusi.Publish(*event, func(remoteEvent *susigo.Event) {
remoteEvent.Topic = eventConfig.Topic
local.Ack(remoteEvent)
})
})
}
}
func setupRegisterConsumers(node *NodeConfig) {
nodeSusi := nodes[node.ID]
for idx := range node.RegisterConsumers {
eventConfig := node.RegisterConsumers[idx]
nodeSusi.RegisterConsumer(eventConfig.Topic, func(remoteEvent *susigo.Event) {
if eventConfig.StripSuffix != "" {
remoteEvent.Topic = strings.TrimSuffix(remoteEvent.Topic, eventConfig.StripSuffix)
}
log.Printf("got consumer event %v from %v, publish as %v", eventConfig.Topic, node.ID, remoteEvent.Topic)
local.Publish(*remoteEvent, nil)
})
}
}
func setupRegisterProcessors(node *NodeConfig) {
nodeSusi := nodes[node.ID]
for idx := range node.RegisterProcessors {
eventConfig := node.RegisterProcessors[idx]
nodeSusi.RegisterProcessor(eventConfig.Topic, func(remoteEvent *susigo.Event) {
if eventConfig.StripSuffix != "" {
remoteEvent.Topic = strings.TrimSuffix(remoteEvent.Topic, eventConfig.StripSuffix)
}
log.Printf("got processor event %v from %v, publish as %v", eventConfig.Topic, node.ID, remoteEvent.Topic)
local.Publish(*remoteEvent, func(localEvent *susigo.Event) {
localEvent.Topic = eventConfig.Topic
nodeSusi.Ack(localEvent)
})
})
}
}
func main() {
flag.Parse()
err := loadConfig()
if err != nil {
log.Fatal(err)
}
err = setupLocal()
if err != nil {
log.Fatal(err)
}
err = setupNodes()
if err != nil {
log.Fatal(err)
}
select {}
}