-
Notifications
You must be signed in to change notification settings - Fork 2
/
mq.go
181 lines (151 loc) · 3.74 KB
/
mq.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
174
175
176
177
178
179
180
181
package util
import (
"fmt"
"sync"
"github.com/Dataman-Cloud/omega-metrics/config"
log "github.com/Sirupsen/logrus"
"github.com/streadway/amqp"
)
const (
Metrics_exchange string = "cluster_info"
Master_metrics_routing string = "master_metrics"
Master_state_routing string = "master_state"
Slave_metrics_routing string = "slave_metrics"
Slave_state_routing string = "slave_state"
)
func failOnError(err error, msg string) error {
if err != nil {
log.Errorf("%s: %s", msg, err)
}
return err
}
var mq *amqp.Connection
func MQ() *amqp.Connection {
if mq != nil {
return mq
}
mutex := sync.Mutex{}
mutex.Lock()
InitMQ()
defer mutex.Unlock()
return mq
}
func Publish(name string, message []byte) error {
mq := MQ()
channel, err := mq.Channel()
if err != nil {
log.Error("can't get channel", err)
return err
}
defer channel.Close()
err = channel.ExchangeDeclare(name, "fanout", true, false, false, false, nil)
if err != nil {
log.Error("can't declare exchange", err)
return err
}
err = channel.Publish(
name,
"",
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: message,
})
if err != nil {
log.Error("can't publish message", err)
return err
}
return nil
}
// func Subscribe(name string, handler func([]byte)) error {
// mq := MQ()
// channel, err := mq.Channel()
// if err != nil {
// log.Error("can't get channel", err)
// return err
// }
// err = channel.ExchangeDeclare(name, "fanout", true, false, false, false, nil)
// if err != nil {
// log.Error("can't declare exchange", err)
// return err
// }
// queue, err := channel.QueueDeclare("", false, false, true, false, nil)
// if err != nil {
// log.Error("can't declare queue", err)
// return err
// }
// err = channel.QueueBind(queue.Name, "", name, false, nil)
// if err != nil {
// log.Error("can't bind queue ", err)
// return err
// }
// messages, err := channel.Consume(queue.Name, "", true, false, false, false, nil)
// if err != nil {
// log.Error("can't consume ", err)
// return err
// }
// go func() {
// defer channel.Close()
// for message := range messages {
// handler(message.Body)
// }
// }()
// return nil
// }
func MetricsPublish(exchange string, message []byte) error {
mq := MQ()
channel, err := mq.Channel()
failOnError(err, "can't get channel")
defer channel.Close()
err = channel.ExchangeDeclare(exchange, "direct", true, false, false, false, nil)
failOnError(err, "can't declare exchange")
err = channel.Publish(
exchange,
Master_metrics_routing,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: message,
})
failOnError(err, "can't pulish message")
return nil
}
func MetricsSubscribe(exchange string, routingkey string, handler func(string, []byte)) error {
mq := MQ()
channel, err := mq.Channel()
failOnError(err, "can't get channel")
err = channel.ExchangeDeclare(exchange, "direct", true, false, false, false, nil)
failOnError(err, "can't declare exchange")
err = channel.QueueBind(routingkey, routingkey, exchange, false, nil)
failOnError(err, "can't bind queue")
messages, err := channel.Consume(routingkey, "", true, false, false, false, nil)
failOnError(err, "can't consume")
go func() {
defer channel.Close()
for message := range messages {
handler(message.RoutingKey, message.Body)
}
}()
return nil
}
func InitMQ() {
conf := config.Pairs()
opts := fmt.Sprintf("amqp://%s:%s@%s:%d/",
conf.Mq.User, conf.Mq.Password, conf.Mq.Host, conf.Mq.Port)
var err error
mq, err = amqp.Dial(opts)
if err != nil {
log.Error("got err", err)
log.Fatal("can't dial mq server: ", opts)
panic(-1)
}
log.Debug("initialized MQ")
}
func DestroyMQ() {
log.Info("destroying MQ")
if mq != nil {
mq.Close()
}
}