forked from krakend/krakend-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
162 lines (137 loc) · 3.59 KB
/
consumer.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
package amqp
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"github.com/streadway/amqp"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
)
const consumerNamespace = "github.com/devopsfaith/krakend-amqp/consume"
var errNoConsumerCfgDefined = errors.New("no amqp consumer defined")
var errNoBackendHostDefined = errors.New("no host backend defined")
type consumerCfg struct {
queueCfg
AutoACK bool `json:"auto_ack"`
NoLocal bool `json:"no_local"`
}
func (f backendFactory) initConsumer(ctx context.Context, remote *config.Backend) (proxy.Proxy, error) {
if len(remote.Host) < 1 {
return proxy.NoopProxy, errNoBackendHostDefined
}
dns := remote.Host[0]
cfg, err := getConsumerConfig(remote)
if err != nil {
f.logger.Debug(fmt.Sprintf("AMQP: %s: %s", dns, err.Error()))
return proxy.NoopProxy, err
}
f.mu.Lock()
defer f.mu.Unlock()
if msgs, ok := f.consumers[dns+cfg.Name]; ok {
return consumerBackend(remote, msgs), nil
}
ch, close, err := f.newChannel(dns)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: getting the channel for %s/%s: %s", dns, cfg.Name, err.Error()))
return proxy.NoopProxy, err
}
err = ch.ExchangeDeclare(
cfg.Exchange, // name
"topic", // type
cfg.Durable,
cfg.Delete,
cfg.Exclusive,
cfg.NoWait,
nil,
)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: declaring the exchange for %s/%s: %s", dns, cfg.Name, err.Error()))
close()
return proxy.NoopProxy, err
}
q, err := ch.QueueDeclare(
cfg.Name,
cfg.Durable,
cfg.Delete,
cfg.Exclusive,
cfg.NoWait,
nil,
)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: declaring the queue for %s/%s: %s", dns, cfg.Name, err.Error()))
close()
return proxy.NoopProxy, err
}
for _, k := range cfg.RoutingKey {
err := ch.QueueBind(
q.Name, // queue name
k, // routing key
cfg.Exchange, // exchange
false,
nil,
)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: bindind the queue for %s/%s: %s", dns, cfg.Name, err.Error()))
}
}
if cfg.PrefetchCount != 0 || cfg.PrefetchSize != 0 {
if err := ch.Qos(cfg.PrefetchCount, cfg.PrefetchSize, false); err != nil {
f.logger.Error(fmt.Sprintf("AMQP: setting the QoS for the consumer %s/%s: %s", dns, cfg.Name, err.Error()))
close()
return proxy.NoopProxy, err
}
}
msgs, err := ch.Consume(
cfg.Name,
"", // cfg.Exchange,
cfg.AutoACK,
cfg.Exclusive,
cfg.NoLocal,
cfg.NoWait,
nil,
)
if err != nil {
f.logger.Error(fmt.Sprintf("AMQP: setting up the consumer for %s/%s: %s", dns, cfg.Name, err.Error()))
close()
return proxy.NoopProxy, err
}
f.consumers[dns+cfg.Name] = msgs
go func() {
<-ctx.Done()
close()
}()
return consumerBackend(remote, msgs), nil
}
func getConsumerConfig(remote *config.Backend) (*consumerCfg, error) {
v, ok := remote.ExtraConfig[consumerNamespace]
if !ok {
return nil, errNoConsumerCfgDefined
}
b, _ := json.Marshal(v)
cfg := &consumerCfg{}
err := json.Unmarshal(b, cfg)
return cfg, err
}
func consumerBackend(remote *config.Backend, msgs <-chan amqp.Delivery) proxy.Proxy {
ef := proxy.NewEntityFormatter(remote)
return func(ctx context.Context, _ *proxy.Request) (*proxy.Response, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case msg := <-msgs:
var data map[string]interface{}
err := remote.Decoder(bytes.NewBuffer(msg.Body), &data)
if err != nil && err != io.EOF {
msg.Ack(false)
return nil, err
}
msg.Ack(true)
newResponse := proxy.Response{Data: data, IsComplete: true}
newResponse = ef.Format(newResponse)
return &newResponse, nil
}
}
}