Newer
Older
Andre Sterba
committed
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
package event
import (
"fmt"
"os"
"code.fbi.h-da.de/danet/gosdn/controller/customerrs"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/sirupsen/logrus"
)
type (
eventCallbackFunc func(*Event)
)
// TypeToCallbackTuple provides a mapping between an event type and a provided callback.
type TypeToCallbackTuple struct {
Type Type
Callback eventCallbackFunc
}
// ServiceInterface defines an event service.
type ServiceInterface interface {
SubscribeToEventType([]TypeToCallbackTuple)
SetupEventReciever(stopChan chan os.Signal)
CloseConnection()
}
// Service is used to setup a connection to a broker and publish events to topics.
type Service struct {
connection *amqp.Connection
channel *amqp.Channel
subscriber map[Type]eventCallbackFunc
topics []Topic
}
// NewEventService creates a new connection to the broker and opens a channel for later usage.
func NewEventService(brokerConnectionString string, topics []Topic) (ServiceInterface, error) {
conn, err := amqp.Dial(brokerConnectionString)
if err != nil {
return nil, &customerrs.AMQPInitFailError{Action: "failed to connect to RabbitMQ", Err: err}
}
ch, err := conn.Channel()
if err != nil {
return nil, &customerrs.AMQPInitFailError{Action: "failed to open a channel", Err: err}
}
return &Service{
connection: conn,
channel: ch,
subscriber: map[Type]eventCallbackFunc{},
topics: topics,
}, nil
}
// SubscribeToEventType subscribes a provided callback to an event.
func (e *Service) SubscribeToEventType(eventTypeToCallbackMapping []TypeToCallbackTuple) {
for _, eventTypeToCallback := range eventTypeToCallbackMapping {
e.subscriber[eventTypeToCallback.Type] = eventTypeToCallback.Callback
}
}
// SetupEventReciever will be removed in the future.
func (e *Service) SetupEventReciever(stopChan chan os.Signal) {
for _, topic := range e.topics {
go func(topic string) {
setupErr := e.setupQueueConsume(topic, stopChan)
if setupErr != nil {
stopChan <- os.Interrupt
}
}(topic.String())
}
}
// setupQueueConsume sets up a connection to the broker, listens to messages on a specific topic
// and writes them into a channel.
func (e *Service) setupQueueConsume(topic string, stopChan chan os.Signal) error {
err := e.channel.ExchangeDeclare(
topic, // name
"fanout", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
if err != nil {
return &customerrs.AMQPInitFailError{Action: "failed declaring exchange", Err: err}
}
Andre Sterba
committed
q, err := e.channel.QueueDeclare(
Andre Sterba
committed
false, // durable
false, // delete when unused
Andre Sterba
committed
false, // no-wait
nil, // arguments
)
if err != nil {
return &customerrs.AMQPInitFailError{Action: "failed declaring queue", Err: err}
}
Andre Sterba
committed
err = e.channel.QueueBind(
q.Name, // queue name
"", // routing key
topic, // exchange
false,
nil,
)
Andre Sterba
committed
if err != nil {
return &customerrs.AMQPInitFailError{Action: "failed binding queue to exchange", Err: err}
Andre Sterba
committed
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
}
msgs, err := e.channel.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return &customerrs.AMQPInitFailError{Action: "failed to register consumer", Err: err}
}
var forever chan struct{}
go func() {
for d := range msgs {
event, err := getEventFromPayload(d.Body)
if err != nil {
fmt.Printf("Error: +%v\n", err)
}
val, ok := e.subscriber[parseTypeString(event.Type)]
if ok {
val(event)
}
}
}()
<-forever
return nil
}
// CloseConnection closes an exisiting connection.
func (e *Service) CloseConnection() {
err := e.connection.Close()
if err != nil {
logrus.Errorf("could not close connection %s", err)
}
}