-
Notifications
You must be signed in to change notification settings - Fork 2
/
broker.go
101 lines (85 loc) · 2.09 KB
/
broker.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
package main
// just listens on a REP socket and forwards
// messages to a PUB socket
import (
"encoding/json"
"expvar"
"flag"
"io/ioutil"
"net/http"
"time"
log "github.com/Sirupsen/logrus"
"github.com/kelseyhightower/envconfig"
zmq "github.com/pebbe/zmq2"
)
type envelope struct {
Address string `json:"address"`
Content string `json:"content"`
}
type ConfigData struct {
RepSocket string
PubSocket string
}
type config struct {
LogLevel string `envconfig:"LOG_LEVEL"`
ExpVarPort string `envconfig:"EXPVAR_PORT" default:"8081"`
}
var startTime = time.Now().UTC()
var numMessages = expvar.NewInt("NumMessages")
func uptime() interface{} {
uptime := time.Since(startTime)
return int64(uptime)
}
func main() {
log.SetLevel(log.InfoLevel)
var configfile string
flag.StringVar(&configfile, "config", "./broker.json", "Broker JSON config file")
flag.Parse()
file, err := ioutil.ReadFile(configfile)
if err != nil {
log.Fatal("could not read config file: " + err.Error())
}
f := ConfigData{}
err = json.Unmarshal(file, &f)
if err != nil {
log.Fatal("could not parse config file: " + err.Error())
}
var c config
err = envconfig.Process("broker", &c)
if err != nil {
log.Fatal(err.Error())
}
// defaults to INFO
if c.LogLevel == "DEBUG" {
log.SetLevel(log.DebugLevel)
}
if c.LogLevel == "WARN" {
log.SetLevel(log.WarnLevel)
}
if c.LogLevel == "ERROR" {
log.SetLevel(log.ErrorLevel)
}
if c.LogLevel == "FATAL" {
log.SetLevel(log.FatalLevel)
}
pubsocket, _ := zmq.NewSocket(zmq.PUB)
repsocket, _ := zmq.NewSocket(zmq.REP)
defer pubsocket.Close()
defer repsocket.Close()
pubsocket.Bind(f.PubSocket)
repsocket.Bind(f.RepSocket)
log.Info("listening on ZMQ sockets")
expvar.Publish("Uptime", expvar.Func(uptime))
go http.ListenAndServe(":"+c.ExpVarPort, nil)
log.Info("expvar available on :" + c.ExpVarPort + "/debug/vars")
var e envelope
for {
msg, _ := repsocket.Recv(0)
json.Unmarshal([]byte(msg), &e)
pubsocket.Send(e.Address, zmq.SNDMORE)
pubsocket.Send(e.Content, 0)
repsocket.Send("published", 0)
log.Debug("published message")
numMessages.Add(1)
}
}