forked from looplab/eventhorizon
/
commandbus_rabbitmq.go
247 lines (214 loc) · 6.54 KB
/
commandbus_rabbitmq.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package eventhorizon
import (
"encoding/json"
"fmt"
"sync"
"github.com/doubledutch/lager"
"github.com/streadway/amqp"
)
const (
commandExchange = "commands.exchange"
commandExchangeType = "topic"
commandQueueName = "commands.queue"
commandKey = "#"
)
// RabbitMQCommandBus implements CommandBus using RabbitMQ.
type RabbitMQCommandBus struct {
conn *amqp.Connection
channel *amqp.Channel
handlersLock sync.Mutex
handlers map[string]CommandHandler
factoriesLock sync.Mutex
factories map[string]func() Command
done chan error
exchange string
queue string
tag string
lgr lager.ContextLager
}
// NewRabbitMQCommandBus creates a new RabbitMQ command bus. amqpURI is the RabbitMQ
// URI for rabbitmq. app is provides a namespace for this application, allowing
// for multiple command buses to run on one RabbitMQ and not conflict with eachother.
// tag is used as the RabbitMQ consumer tag for this bus.
func NewRabbitMQCommandBus(amqpURI, app, tag string) (*RabbitMQCommandBus, error) {
lgr := lager.Child()
connection, err := amqp.Dial(amqpURI)
if err != nil {
lgr.WithError(err).Errorf("Error dialing URI: %s", amqpURI)
return nil, fmt.Errorf("Dial err: %s", err)
}
channel, err := connection.Channel()
if err != nil {
connection.Close()
lgr.WithError(err).Errorf("Error opening channel")
return nil, fmt.Errorf("Channel: %s", err)
}
exchange := appExchange(app, commandExchange)
queueName := appTagQueueName(app, tag, commandQueueName)
if err := channel.ExchangeDeclare(
exchange, // name
commandExchangeType, // type
true, // durable
false, // auto-deleted
false, // internal
false, // noWait
nil, // arguments
); err != nil {
channel.Close()
connection.Close()
lgr.WithError(err).Errorf("Error declaring exchange :%s", exchange)
return nil, fmt.Errorf("Exchange Declare: %s", err)
}
queue, err := channel.QueueDeclare(
queueName, // name of the queue
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
if err != nil {
channel.Close()
connection.Close()
lgr.WithError(err).Errorf("Error declaring queue %s", queueName)
return nil, fmt.Errorf("Queue Declare: %s", err)
}
if err = channel.QueueBind(
queue.Name, // name of the queue
commandKey, // bindingKey
exchange, // sourceExchange
false, // noWait
nil, // arguments
); err != nil {
channel.Close()
connection.Close()
lgr.WithError(err).Errorf("Error binding queue %s", queueName)
return nil, fmt.Errorf("Queue Bind: %s", err)
}
deliveries, err := channel.Consume(
queue.Name, // name
tag, // consumerTag,
false, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
if err != nil {
channel.Close()
connection.Close()
lgr.WithError(err).Errorf("Error consuming queue %s", queueName)
return nil, fmt.Errorf("Queue Consume: %s", err)
}
bus := &RabbitMQCommandBus{
conn: connection,
channel: channel,
exchange: exchange,
queue: queueName,
tag: tag,
handlers: make(map[string]CommandHandler),
factories: make(map[string]func() Command),
done: make(chan error),
lgr: lgr,
}
go bus.handleCommands(deliveries, bus.done)
return bus, nil
}
// PublishCommand publishes a command to the commands exchange.
func (b *RabbitMQCommandBus) PublishCommand(command Command) error {
d, err := json.Marshal(command)
if err != nil {
b.lgr.WithError(err).Errorf("Unable to marshal command")
return err
}
err = b.channel.Publish(
b.exchange, // publish to an exchange
command.CommandType(), // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "text/plain",
ContentEncoding: "",
Body: d,
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
Priority: 0, // 0-9
// a bunch of application/implementation-specific fields
})
if err != nil {
b.lgr.WithError(err).Errorf("Unable to publish command")
}
return err
}
// Close closes the command bus, closing the rabbitmq connection.
func (b *RabbitMQCommandBus) Close() error {
// will close() the deliveries channel
if err := b.channel.Cancel(b.tag, true); err != nil {
return fmt.Errorf("Consumer cancel failed: %s", err)
}
if err := b.conn.Close(); err != nil {
return fmt.Errorf("AMQP connection close error: %s", err)
}
return <-b.done
}
func (b *RabbitMQCommandBus) handleCommands(deliveries <-chan amqp.Delivery, done chan error) {
for d := range deliveries {
b.factoriesLock.Lock()
f, ok := b.factories[d.RoutingKey]
b.factoriesLock.Unlock()
if !ok {
b.lgr.With(map[string]string{
"commandType": d.RoutingKey,
}).Debugf("No factory for command type")
d.Reject(false)
continue
}
command := f()
if err := json.Unmarshal(d.Body, command); err != nil {
b.lgr.WithError(err).With(map[string]string{
"commandType": d.RoutingKey,
}).Errorf("Unable to unmarshal received command")
d.Reject(false)
continue
}
if err := b.HandleCommand(command); err != nil {
b.lgr.WithError(err).With(map[string]string{
"commandType": d.RoutingKey,
}).Errorf("Error handling command")
d.Reject(false)
continue
}
b.lgr.With(map[string]string{"commandType": d.RoutingKey}).Debugf("Handled command")
d.Ack(false)
}
done <- nil
}
// HandleCommand handles a command, dispatching it to the proper handlers.
func (b *RabbitMQCommandBus) HandleCommand(command Command) error {
b.handlersLock.Lock()
defer b.handlersLock.Unlock()
if handler, ok := b.handlers[command.CommandType()]; ok {
return handler.HandleCommand(command)
}
return ErrHandlerNotFound
}
// SetHandler sets a handler for a specific command.
func (b *RabbitMQCommandBus) SetHandler(handler CommandHandler, command Command) error {
b.handlersLock.Lock()
defer b.handlersLock.Unlock()
if _, ok := b.handlers[command.CommandType()]; ok {
return ErrHandlerAlreadySet
}
b.handlers[command.CommandType()] = handler
return nil
}
// RegisterCommandType registers a command factory for a specific command.
func (b *RabbitMQCommandBus) RegisterCommandType(command Command, factory func() Command) error {
b.factoriesLock.Lock()
defer b.factoriesLock.Unlock()
if _, ok := b.factories[command.CommandType()]; ok {
return ErrHandlerAlreadySet
}
b.factories[command.CommandType()] = factory
return nil
}