-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (125 loc) · 3.48 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
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
// Copyright (c) OpenFaaS Project 2018. All rights reserved.
// Copyright (c) Keiran Smith 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"encoding/json"
"log"
"os"
"strings"
"time"
"github.com/jrallison/go-workers"
"github.com/openfaas-incubator/connector-sdk/types"
)
type connectorConfig struct {
upstreamTimeout time.Duration
queues []string
printResponse bool
rebuildInterval time.Duration
redis_host string
}
func main() {
config := buildConnectorConfig()
topicMap := types.NewTopicMap()
lookupBuilder := types.FunctionLookupBuilder{
Client: types.MakeClient(config.upstreamTimeout),
}
creds := types.GetCredentials()
controllerconfig := &types.ControllerConfig{
RebuildInterval: time.Millisecond * 1000,
GatewayURL: "http://127.0.0.1:8080",
PrintResponse: true,
PrintResponseBody: true,
}
controller := types.NewController(creds, controllerconfig)
receiver := ResponseReceiver{}
controller.Subscribe(&receiver)
controller.BeginMapBuilder()
ticker := time.NewTicker(config.rebuildInterval)
go synchronizeLookups(ticker, &lookupBuilder, &topicMap)
workers.Configure(map[string]string{
"server": config.redis_host,
"database": "0",
"pool": "30",
"process": "1",
})
for _, queue := range config.queues {
handler := makeMessageHandler(controller, queue)
workers.Process(queue, handler, 10)
}
workers.Run()
}
func synchronizeLookups(ticker *time.Ticker,
lookupBuilder *types.FunctionLookupBuilder,
topicMap *types.TopicMap) {
for {
<-ticker.C
lookups, err := lookupBuilder.Build()
if err != nil {
log.Fatalln(err)
}
log.Println("Syncing topic map")
topicMap.Sync(&lookups)
}
}
func makeMessageHandler(controller *types.Controller, queue string) func(msg *workers.Msg) {
mcb := func(msg *workers.Msg) {
msgJson, err := json.Marshal(msg.Args)
if err != nil {
log.Fatal(err.Error())
}
controller.Invoke(queue, &msgJson)
}
return mcb
}
func buildConnectorConfig() connectorConfig {
redis := "redis_host"
if val, exists := os.LookupEnv("redis_host"); exists {
redis = val
}
queues := []string{}
if val, exists := os.LookupEnv("queues"); exists {
for _, topic := range strings.Split(val, ",") {
if len(topic) > 0 {
queues = append(queues, topic)
}
}
}
if len(queues) == 0 {
log.Fatal(`Provide a list of queues i.e. queues="payment_published,slack_joined"`)
}
upstreamTimeout := time.Second * 30
rebuildInterval := time.Second * 3
if val, exists := os.LookupEnv("upstream_timeout"); exists {
parsedVal, err := time.ParseDuration(val)
if err == nil {
upstreamTimeout = parsedVal
}
}
if val, exists := os.LookupEnv("rebuild_interval"); exists {
parsedVal, err := time.ParseDuration(val)
if err == nil {
rebuildInterval = parsedVal
}
}
printResponse := false
if val, exists := os.LookupEnv("print_response"); exists {
printResponse = (val == "1" || val == "true")
}
return connectorConfig{
upstreamTimeout: upstreamTimeout,
queues: queues,
rebuildInterval: rebuildInterval,
redis_host: redis,
printResponse: printResponse,
}
}
type ResponseReceiver struct {
}
func (ResponseReceiver) Response(res types.InvokerResponse) {
if res.Error != nil {
log.Printf("tester got error: %s", res.Error.Error())
} else {
log.Printf("tester got result: [%d] %s => %s (%d) bytes", res.Status, res.Topic, res.Function, len(*res.Body))
}
}