forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stomp.go
244 lines (207 loc) · 5.61 KB
/
stomp.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
package stomp
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/url"
"time"
"github.com/go-stomp/stomp"
"github.com/go-stomp/stomp/frame"
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/cmd"
)
type rbroker struct {
opts broker.Options
stompConn *stomp.Conn
}
// init registers the STOMP broker
func init() {
cmd.DefaultBrokers["stomp"] = NewBroker
}
// stompHeaderToMap converts STOMP header to broker friendly header
func stompHeaderToMap(h *frame.Header) map[string]string {
m := map[string]string{}
for i := 0; i < h.Len(); i++ {
k, v := h.GetAt(i)
m[k] = v
}
return m
}
// defaults sets 'sane' STOMP default
func (r *rbroker) defaults() {
ConnectTimeout(30 * time.Second)(&r.opts)
VirtualHost("/")(&r.opts)
}
func (r *rbroker) Options() broker.Options {
if r.opts.Context == nil {
r.opts.Context = context.Background()
}
return r.opts
}
func (r *rbroker) Address() string {
if len(r.opts.Addrs) > 0 {
return r.opts.Addrs[0]
}
return ""
}
func (r *rbroker) Connect() error {
connectTimeOut := r.Options().Context.Value(connectTimeoutKey{}).(time.Duration)
// Decode address
url, err := url.Parse(r.Address())
if err != nil {
return err
}
// Make sure we are stomp
if url.Scheme != "stomp" {
return fmt.Errorf("Expected stomp:// protocol but was %s", url.Scheme)
}
// Retrieve user/pass if present
stompOpts := []func(*stomp.Conn) error{}
if url.User != nil && url.User.Username() != "" {
password, _ := url.User.Password()
stompOpts = append(stompOpts, stomp.ConnOpt.Login(url.User.Username(), password))
}
// Dial
netConn, err := net.DialTimeout("tcp", url.Host, connectTimeOut)
if err != nil {
return fmt.Errorf("Failed to dial %s: %v", url.Host, err)
}
// Set connect options
if auth, ok := r.Options().Context.Value(authKey{}).(*authRecord); ok && auth != nil {
stompOpts = append(stompOpts, stomp.ConnOpt.Login(auth.username, auth.password))
}
if headers, ok := r.Options().Context.Value(connectHeaderKey{}).(map[string]string); ok && headers != nil {
for k, v := range headers {
stompOpts = append(stompOpts, stomp.ConnOpt.Header(k, v))
}
}
if host, ok := r.Options().Context.Value(vHostKey{}).(string); ok && host != "" {
log.Printf("Adding host: %s", host)
stompOpts = append(stompOpts, stomp.ConnOpt.Host(host))
}
// STOMP Connect
r.stompConn, err = stomp.Connect(netConn, stompOpts...)
if err != nil {
netConn.Close()
return fmt.Errorf("Failed to connect to %s: %v", url.Host, err)
}
return nil
}
func (r *rbroker) Disconnect() error {
return r.stompConn.Disconnect()
}
func (r *rbroker) Init(opts ...broker.Option) error {
r.defaults()
for _, o := range opts {
o(&r.opts)
}
return nil
}
func (r *rbroker) Publish(topic string, msg *broker.Message, opts ...broker.PublishOption) error {
if r.stompConn == nil {
return errors.New("not connected")
}
// Set options
stompOpt := make([]func(*frame.Frame) error, 0, len(msg.Header))
for k, v := range msg.Header {
stompOpt = append(stompOpt, stomp.SendOpt.Header(k, v))
}
bOpt := broker.PublishOptions{}
for _, o := range opts {
o(&bOpt)
}
if withReceipt, ok := r.Options().Context.Value(receiptKey{}).(bool); ok && withReceipt {
stompOpt = append(stompOpt, stomp.SendOpt.Receipt)
}
if withoutContentLength, ok := r.Options().Context.Value(suppressContentLengthKey{}).(bool); ok && withoutContentLength {
stompOpt = append(stompOpt, stomp.SendOpt.NoContentLength)
}
// Send
if err := r.stompConn.Send(
topic,
"",
msg.Body,
stompOpt...); err != nil {
return err
}
return nil
}
func (r *rbroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
var ackSuccess bool
if r.stompConn == nil {
return nil, errors.New("not connected")
}
// Set options
stompOpt := make([]func(*frame.Frame) error, 0, len(opts))
bOpt := broker.SubscribeOptions{
AutoAck: true,
}
for _, o := range opts {
o(&bOpt)
}
// Make sure context is setup
if bOpt.Context == nil {
bOpt.Context = context.Background()
}
ctx := bOpt.Context
if subscribeContext, ok := ctx.Value(subscribeContextKey{}).(context.Context); ok && subscribeContext != nil {
ctx = subscribeContext
}
if durableQueue, ok := ctx.Value(durableQueueKey{}).(bool); ok && durableQueue {
stompOpt = append(stompOpt, stomp.SubscribeOpt.Header("persistent", "true"))
}
if headers, ok := ctx.Value(subscribeHeaderKey{}).(map[string]string); ok && len(headers) > 0 {
for k, v := range headers {
stompOpt = append(stompOpt, stomp.SubscribeOpt.Header(k, v))
}
}
if bval, ok := ctx.Value(ackSuccessKey{}).(bool); ok && bval {
bOpt.AutoAck = false
ackSuccess = true
}
var ackMode stomp.AckMode
if bOpt.AutoAck {
ackMode = stomp.AckAuto
} else {
ackMode = stomp.AckClientIndividual
}
// Subscribe now
sub, err := r.stompConn.Subscribe(topic, ackMode, stompOpt...)
if err != nil {
return nil, err
}
// Process messages
go func() {
for msg := range sub.C {
go func(msg *stomp.Message) {
// Transform message
m := &broker.Message{
Header: stompHeaderToMap(msg.Header),
Body: msg.Body,
}
// Handle the publication
err := handler(&publication{msg: msg, m: m, topic: topic, broker: r})
if err == nil && !bOpt.AutoAck && ackSuccess {
msg.Conn.Ack(msg)
}
}(msg)
}
}()
// Return subs
return &subscriber{sub: sub, topic: topic, opts: bOpt}, nil
}
func (r *rbroker) String() string {
return "stomp"
}
// NewBroker returns a STOMP broker
func NewBroker(opts ...broker.Option) broker.Broker {
r := &rbroker{
opts: broker.Options{
Context: context.Background(),
},
}
r.Init(opts...)
return r
}