-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroker.go
More file actions
126 lines (104 loc) · 2.76 KB
/
broker.go
File metadata and controls
126 lines (104 loc) · 2.76 KB
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
package rabbitmq
import (
"errors"
amqp "github.com/rabbitmq/amqp091-go"
"os"
"os/signal"
"reflect"
"runtime"
"syscall"
)
var (
ErrorTopicNameEmpty = errors.New("topic name can't be empty")
ErrorConsumerFnExists = errors.New("consumer func already exists")
ErrorConsumerFirstArgNotMatch = errors.New("consumer functions first arguments must have matched types")
ErrorFirsArgMustBePointer = errors.New("first argument of handler func must be pointer")
)
type BrokerInterface interface {
AddConsumerHandler(msg interface{}, handlerFn ConsumerFn) error
StartConsume(quit chan struct{}) error
Publish(msg interface{}, headers amqp.Table) error
QueuePurge() error
}
type Broker struct {
rabbitMQ *rabbitMq
consumer *consumer
encoder MessageEncoder
opts *Options
}
func NewBroker(opts ...Option) (BrokerInterface, error) {
options := &Options{}
for _, opt := range opts {
opt(options)
}
if options.topic == "" {
return nil, ErrorTopicNameEmpty
}
rmq, err := newRabbitMq(options)
if err != nil {
return nil, err
}
encoder := options.GetMessageEncoder()
broker := &Broker{
rabbitMQ: rmq,
consumer: newConsumer(rmq, encoder, options),
encoder: encoder,
opts: options,
}
return broker, nil
}
func (m *Broker) AddConsumerHandler(msg interface{}, handlerFn ConsumerFn) error {
refFn := reflect.ValueOf(handlerFn)
msgType := reflect.TypeOf(msg)
if msgType.Kind() != reflect.Ptr {
return ErrorFirsArgMustBePointer
}
msgTypeEl := reflect.TypeOf(msg).Elem()
fnName := runtime.FuncForPC(refFn.Pointer()).Name()
if _, ok := m.consumer.existsHandlers[fnName]; ok {
return ErrorConsumerFnExists
}
if len(m.consumer.handlers) > 0 {
if m.consumer.handlers[0].msgType != msgTypeEl {
return ErrorConsumerFirstArgNotMatch
}
}
h := &handler{
fn: handlerFn,
msgType: msgTypeEl,
}
m.consumer.handlers = append(m.consumer.handlers, h)
m.consumer.existsHandlers[fnName] = struct{}{}
return nil
}
func (m *Broker) StartConsume(quit chan struct{}) error {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
m.consumer.Consume()
select {
case <-ch:
case <-quit:
m.rabbitMQ.close <- true
}
return nil
}
func (m *Broker) Publish(msg interface{}, headers amqp.Table) error {
var err error
if headers == nil {
headers = make(amqp.Table)
}
publication := amqp.Publishing{
ContentType: m.encoder.GetContentType(),
Headers: headers,
}
publication.Body, err = m.encoder.Marshal(msg)
if err != nil {
return err
}
pubOpts := m.opts.GetPublisherOpts()
return m.rabbitMQ.channel.Publish(m.opts.GetExchangeName(), m.opts.topic, pubOpts.Mandatory, pubOpts.Immediate,
publication)
}
func (m *Broker) QueuePurge() error {
return m.rabbitMQ.queuePurge()
}