-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstomp.go
More file actions
294 lines (249 loc) · 7.82 KB
/
stomp.go
File metadata and controls
294 lines (249 loc) · 7.82 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package stomp
import (
"bufio"
"fmt"
"log"
"math/rand"
"net"
"sync"
"time"
)
const (
// When the ack mode is auto, then the client does not need to send the server
// ACK frames for the messages it receives. The server will assume the client
// has received the message as soon as it sends it to the client. This
// acknowledgment mode can cause messages being transmitted to the client to
// get dropped.
AckAuto AckMode = "auto"
// When the ack mode is client, then the client MUST send the server ACK frames
// for the messages it processes. If the connection fails before a client sends
// an ACK frame for the message the server will assume the message has not been
// processed and MAY redeliver the message to another client. The ACK frames
// sent by the client will be treated as a cumulative acknowledgment. This means
// the acknowledgment operates on the message specified in the ACK frame and all
// messages sent to the subscription before the ACK'ed message.
//
// In case the client did not process some messages, it SHOULD send NACK frames
// to tell the server it did not consume these messages.
AckClient AckMode = "client"
// When the ack mode is client-individual, the acknowledgment operates just like
// the client acknowledgment mode except that the ACK or NACK frames sent by the
// client are not cumulative. This means that an ACK or NACK frame for a
// subsequent message MUST NOT cause a previous message to get acknowledged.
AckIndividual AckMode = "client-individual"
)
// Valid values for the "ack" subscription header entry. If the header is not
// set, it defaults to auto.
type AckMode string
// A Conn represents a STOMP connection.
type Conn struct {
// Err contains the last received error of an read operation.
Err error
Reconnect func(n int, d time.Duration, err error) (bool, time.Duration)
ReconnectSuccess func(n int)
reader *bufio.Reader
network string
addr string
options []Option
conn net.Conn
subsMu sync.Mutex
subs map[string]*Subscription
rhb time.Duration
whb time.Duration
closeC chan struct{}
writeC chan frame
readC chan int
}
// A Subscription represents a subscription on a STOMP server to
// a specified destination.
type Subscription struct {
// Destination is the destination on the STOMP server this subscription
// listens on.
Destination string
// C is the channel where messages received for this subscription
// are sent to.
C chan *Message
id string
destination string
options []Option
}
// Dial connects to the given network address using net.Dial an then initializes
// a STOMP connection. Additional header and options can be given via the
// options parameter.
func Dial(network, addr string, options ...Option) (*Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
c := &Conn{
Err: nil,
Reconnect: ExponentialBackoffReconnect,
ReconnectSuccess: nil,
conn: conn,
network: network,
addr: addr,
options: options,
reader: bufio.NewReader(conn),
subs: make(map[string]*Subscription),
rhb: 5 * time.Second,
whb: 5 * time.Second,
closeC: make(chan struct{}),
writeC: make(chan frame),
readC: make(chan int, 1),
}
err = c.connect(options)
if err != nil {
return nil, err
}
go c.readLoop()
go c.writeLoop()
return c, nil
}
func (c *Conn) connect(options []Option) error {
err := c.unsafeWrite(&Frame{
Command: "CONNECT",
Header: Header{
"host": "localhost",
"accept-version": "1.2",
"heart-beat": fmt.Sprintf("%d,%d", c.whb/time.Millisecond, c.rhb/time.Millisecond),
},
}, options...)
if err != nil {
return err
}
f, err := c.unsafeRead()
if err != nil {
return err
}
if f.Command == "ERROR" {
return NewError(f)
}
// parse connected frame and store heart beat
connected := &Connected{*f}
c.rhb = time.Duration(connected.ReadHeartBeat()) * time.Millisecond
c.whb = time.Duration(connected.WriteHeartBeat()) * time.Millisecond
return nil
}
// Close closes the connection and all associated subscription channels.
func (c *Conn) Close() error {
log.Printf("DEBUG: closing ...")
close(c.closeC)
c.closeSubscriptions()
return c.conn.Close()
}
func (c *Conn) closeSubscriptions() {
c.subsMu.Lock()
for _, sub := range c.subs {
close(sub.C)
}
c.subs = make(map[string]*Subscription)
c.subsMu.Unlock()
}
// Subscribe is used to register to listen to the given destination. Messages
// received for this subscription can be received via the C channel on the
// returned Subscription.
func (c *Conn) Subscribe(destination string, options ...Option) (*Subscription, error) {
id := randID()
frame := &Frame{
Command: "SUBSCRIBE",
Header: Header{
"id": id,
"destination": destination,
"ack": "auto",
},
}
sub := &Subscription{
C: make(chan *Message, 10),
id: id,
destination: destination,
options: options,
}
if err := c.safeWrite(frame, options...); err != nil {
return nil, err
}
c.subsMu.Lock()
c.subs[id] = sub
c.subsMu.Unlock()
return sub, nil
}
// Unsubscribe removes an existing subscription and closes the receiver channel.
// Once the subscription is removed the STOMP connections will no longer receive
// messages from that subscription.
func (c *Conn) Unsubscribe(s *Subscription, options ...Option) error {
frame := &Frame{
Command: "UNSUBSCRIBE",
Header: Header{"id": s.id},
}
if err := c.safeWrite(frame, options...); err != nil {
return err
}
c.subsMu.Lock()
delete(c.subs, s.id)
c.subsMu.Unlock()
return nil
}
// Send sends a message to a destination in the messaging system.
//
// Note that STOMP treats this destination as an opaque string and no delivery
// semantics are assumed by the name of a destination. You should consult your
// STOMP server's documentation to find out how to construct a destination name
// which gives you the delivery semantics that your application needs.
//
// The reliability semantics of the message are also server specific and will
// depend on the destination value being used and the other message headers
// such as the "transaction" or "persist" header or other server specific
// message headers.
func (c *Conn) Send(destination, contentType string, body []byte, options ...Option) error {
frame := &Frame{
Command: "SEND",
Header: Header{
"destination": destination,
"content-type": contentType,
},
Body: body,
}
if len(body) > 0 {
frame.Header["content-length"] = fmt.Sprintf("%d", len(body))
}
return c.safeWrite(frame, options...)
}
// Ack acknowledges the consumption of a message from a subscription using the
// client or client-individual acknowledgment modes. Any messages received from
// such a subscription will not be considered to have been consumed until the
// message has been acknowledged via Ack.
func (c *Conn) Ack(m *Message, options ...Option) error {
id := m.Ack()
if id != "" {
return c.safeWrite(&Frame{
Command: "ACK",
Header: Header{"id": id},
}, options...)
}
return nil
}
// Nack is the opposite of Ack. It tells the server that the client did not
// consume the message. The server can then either send the message to a
// different client, discard it, or put it in a dead letter queue. The exact
// behavior is server specific.
//
// Nack applies either to one single message (if the subscription's ack mode is
// client-individual) or to all messages sent before and not yet Ack'ed or
// Nack'ed (if the subscription's ack mode is client).
func (c *Conn) Nack(m *Message, options ...Option) error {
id := m.Ack()
if id != "" {
return c.safeWrite(&Frame{
Command: "NACK",
Header: Header{"id": id},
}, options...)
}
return nil
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randID() string {
b := make([]rune, 8)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}