-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsq.go
154 lines (122 loc) · 3.97 KB
/
nsq.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
package nsq
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/lerenn/asyncapi-codegen/pkg/extensions"
"github.com/lerenn/asyncapi-codegen/pkg/extensions/brokers"
"github.com/nsqio/go-nsq"
)
const defaultChannelName = "default"
type Controller struct {
addr string
p *nsq.Producer
logger extensions.Logger
connect func(c *nsq.Consumer, addr string) error
}
var _ extensions.BrokerController = (*Controller)(nil)
// ControllerOption is a function that can be used to configure a NSQ controller
// Examples: WithQueueGroup(), WithLogger().
type ControllerOption func(controller *Controller)
// NewController creates a new NSQ controller.
func NewController(url string, options ...ControllerOption) (*Controller, error) {
p, err := nsq.NewProducer(url, nsq.NewConfig())
if err != nil {
return nil, err
}
c := &Controller{
addr: url,
p: p,
logger: extensions.DummyLogger{},
connect: nsqdConnect,
}
// Execute options
for _, option := range options {
option(c)
}
return c, nil
}
// WithLogger set a custom logger that will log operations on broker controller.
func WithLogger(logger extensions.Logger) ControllerOption {
return func(controller *Controller) { controller.logger = logger }
}
func WithLookupdConnect() ControllerOption {
return func(controller *Controller) { controller.connect = nsqlookupdConnect }
}
// Publish a message to the broker.
func (c *Controller) Publish(_ context.Context, topic string, bm extensions.BrokerMessage) error {
if i := strings.IndexRune(topic, '#'); i >= 0 {
topic = topic[:i]
}
return c.p.Publish(topic, bm.Payload)
}
// Subscribe to messages from the broker.
func (c *Controller) Subscribe(ctx context.Context, topic string) (extensions.BrokerChannelSubscription, error) {
channel := defaultChannelName
if i := strings.IndexRune(topic, '#'); i >= 0 && i < len(topic)-1 {
channel = topic[i+1:]
topic = topic[:i]
}
consumer, err := nsq.NewConsumer(topic, channel, nsq.NewConfig())
if err != nil {
return extensions.BrokerChannelSubscription{}, err
}
msgChan := make(chan extensions.BrokerMessage, brokers.BrokerMessagesQueueSize)
consumer.AddHandler(messagesHandler(msgChan))
if err := c.connect(consumer, c.addr); err != nil {
return extensions.BrokerChannelSubscription{}, err
}
// Create a new subscription
sub := extensions.NewBrokerChannelSubscription(msgChan, make(chan any, 1))
sub.WaitForCancellationAsync(consumer.Stop)
return sub, nil
}
func (c *Controller) LookupTopics(ctx context.Context) ([]string, error) {
endpoint := (&url.URL{
Scheme: "http",
Host: c.addr,
Path: "/topics",
}).String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("trying to get list of topics from nsqlookupd: %w", err)
}
type topicsBody struct {
Topics []string `json:"topics"`
}
var body topicsBody
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return nil, fmt.Errorf("trying to parse response from nsqlookupd: %w", err)
}
return body.Topics, nil
}
func messagesHandler(c chan<- extensions.BrokerMessage) nsq.Handler {
return nsq.HandlerFunc(func(message *nsq.Message) error {
headers := map[string][]byte{
"X-MsgID": []byte(message.ID[:]),
"X-Attempts": []byte(strconv.Itoa(int(message.Attempts))),
"X-Timestamp": []byte(strconv.Itoa(int(message.Timestamp))),
}
c <- extensions.BrokerMessage{
Headers: headers,
Payload: message.Body,
}
return nil
})
}
// Close closes everything related to the broker.
func (c *Controller) Close() { c.p.Stop() }
func nsqdConnect(c *nsq.Consumer, addr string) error { return c.ConnectToNSQD(addr) }
func nsqlookupdConnect(c *nsq.Consumer, addr string) error { return c.ConnectToNSQLookupd(addr) }