forked from nsqio/go-nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
372 lines (328 loc) · 9.55 KB
/
writer.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package nsq
import (
"bufio"
"bytes"
"errors"
"log"
"net"
"os"
"strings"
"sync"
"sync/atomic"
"time"
)
// Writer is a high-level type to publish to NSQ.
//
// A Writer instance is 1:1 with a destination `nsqd`
// and will lazily connect to that instance (and re-connect)
// when Publish commands are executed.
type Writer struct {
net.Conn
WriteTimeout time.Duration
Addr string
HeartbeatInterval time.Duration
ShortIdentifier string
LongIdentifier string
concurrentWriters int32
transactionChan chan *WriterTransaction
dataChan chan []byte
transactions []*WriterTransaction
state int32
stopFlag int32
exitChan chan int
closeChan chan int
wg sync.WaitGroup
}
// WriterTransaction is returned by the async publish methods
// to retrieve metadata about the command after the
// response is received.
type WriterTransaction struct {
cmd *Command
doneChan chan *WriterTransaction
FrameType int32 // the frame type received in response to the publish command
Data []byte // the response data of the publish command
Error error // the error (or nil) of the publish command
Args []interface{} // the slice of variadic arguments passed to PublishAsync or MultiPublishAsync
}
func (t *WriterTransaction) finish() {
if t.doneChan != nil {
t.doneChan <- t
}
}
// returned when a publish command is made against a Writer that is not connected
var ErrNotConnected = errors.New("not connected")
// returned when a publish command is made against a Writer that has been stopped
var ErrStopped = errors.New("stopped")
// NewWriter returns an instance of Writer for the specified address
func NewWriter(addr string) *Writer {
hostname, err := os.Hostname()
if err != nil {
log.Fatalf("ERROR: unable to get hostname %s", err.Error())
}
return &Writer{
transactionChan: make(chan *WriterTransaction),
exitChan: make(chan int),
closeChan: make(chan int),
dataChan: make(chan []byte),
// can be overriden before connecting
Addr: addr,
WriteTimeout: time.Second,
HeartbeatInterval: DefaultClientTimeout / 2,
ShortIdentifier: strings.Split(hostname, ".")[0],
LongIdentifier: hostname,
}
}
// String returns the address of the Writer
func (w *Writer) String() string {
return w.Addr
}
// Stop disconnects and permanently stops the Writer
func (w *Writer) Stop() {
if !atomic.CompareAndSwapInt32(&w.stopFlag, 0, 1) {
return
}
w.close()
w.wg.Wait()
}
// PublishAsync publishes a message body to the specified topic
// but does not wait for the response from `nsqd`.
//
// When the Writer eventually receives the response from `nsqd`,
// the supplied `doneChan` (if specified)
// will receive a `WriterTransaction` instance with the supplied variadic arguments
// (and the response `FrameType`, `Data`, and `Error`)
func (w *Writer) PublishAsync(topic string, body []byte, doneChan chan *WriterTransaction, args ...interface{}) error {
return w.sendCommandAsync(Publish(topic, body), doneChan, args)
}
// MultiPublishAsync publishes a slice of message bodies to the specified topic
// but does not wait for the response from `nsqd`.
//
// When the Writer eventually receives the response from `nsqd`,
// the supplied `doneChan` (if specified)
// will receive a `WriterTransaction` instance with the supplied variadic arguments
// (and the response `FrameType`, `Data`, and `Error`)
func (w *Writer) MultiPublishAsync(topic string, body [][]byte, doneChan chan *WriterTransaction, args ...interface{}) error {
cmd, err := MultiPublish(topic, body)
if err != nil {
return err
}
return w.sendCommandAsync(cmd, doneChan, args)
}
// Publish synchronously publishes a message body to the specified topic, returning
// the response frameType, data, and error
func (w *Writer) Publish(topic string, body []byte) (int32, []byte, error) {
return w.sendCommand(Publish(topic, body))
}
// MultiPublish synchronously publishes a slice of message bodies to the specified topic, returning
// the response frameType, data, and error
func (w *Writer) MultiPublish(topic string, body [][]byte) (int32, []byte, error) {
cmd, err := MultiPublish(topic, body)
if err != nil {
return -1, nil, err
}
return w.sendCommand(cmd)
}
func (w *Writer) sendCommand(cmd *Command) (int32, []byte, error) {
doneChan := make(chan *WriterTransaction)
err := w.sendCommandAsync(cmd, doneChan, nil)
if err != nil {
close(doneChan)
return -1, nil, err
}
t := <-doneChan
return t.FrameType, t.Data, t.Error
}
func (w *Writer) sendCommandAsync(cmd *Command, doneChan chan *WriterTransaction, args []interface{}) error {
// keep track of how many outstanding writers we're dealing with
// in order to later ensure that we clean them all up...
atomic.AddInt32(&w.concurrentWriters, 1)
defer atomic.AddInt32(&w.concurrentWriters, -1)
if atomic.LoadInt32(&w.state) != StateConnected {
err := w.connect()
if err != nil {
return err
}
}
t := &WriterTransaction{
cmd: cmd,
doneChan: doneChan,
FrameType: -1,
Args: args,
}
select {
case w.transactionChan <- t:
case <-w.exitChan:
return ErrStopped
}
return nil
}
func (w *Writer) connect() error {
if atomic.LoadInt32(&w.stopFlag) == 1 {
return ErrStopped
}
if !atomic.CompareAndSwapInt32(&w.state, StateInit, StateConnected) {
return ErrNotConnected
}
log.Printf("[%s] connecting...", w)
conn, err := net.DialTimeout("tcp", w.Addr, time.Second*5)
if err != nil {
log.Printf("ERROR: [%s] failed to dial %s - %s", w, w.Addr, err)
atomic.StoreInt32(&w.state, StateInit)
return err
}
w.closeChan = make(chan int)
w.Conn = conn
w.SetWriteDeadline(time.Now().Add(w.WriteTimeout))
_, err = w.Write(MagicV2)
if err != nil {
log.Printf("ERROR: [%s] failed to write magic - %s", w, err)
w.close()
return err
}
ci := make(map[string]interface{})
ci["short_id"] = w.ShortIdentifier
ci["long_id"] = w.LongIdentifier
ci["heartbeat_interval"] = int64(w.HeartbeatInterval / time.Millisecond)
ci["feature_negotiation"] = true
cmd, err := Identify(ci)
if err != nil {
log.Printf("ERROR: [%s] failed to create IDENTIFY command - %s", w, err)
w.close()
return err
}
w.SetWriteDeadline(time.Now().Add(w.WriteTimeout))
err = cmd.Write(w)
if err != nil {
log.Printf("ERROR: [%s] failed to write IDENTIFY - %s", w, err)
w.close()
return err
}
w.SetReadDeadline(time.Now().Add(w.HeartbeatInterval * 2))
resp, err := ReadResponse(w)
if err != nil {
log.Printf("ERROR: [%s] failed to read IDENTIFY response - %s", w, err)
w.close()
return err
}
frameType, data, err := UnpackResponse(resp)
if err != nil {
log.Printf("ERROR: [%s] failed to unpack IDENTIFY response - %s", w, resp)
w.close()
return err
}
if frameType == FrameTypeError {
log.Printf("ERROR: [%s] IDENTIFY returned error response - %s", w, data)
w.close()
return errors.New(string(data))
}
w.wg.Add(2)
go w.readLoop()
go w.messageRouter()
return nil
}
func (w *Writer) close() {
if !atomic.CompareAndSwapInt32(&w.state, StateConnected, StateDisconnected) {
return
}
close(w.closeChan)
w.Conn.Close()
go func() {
// we need to handle this in a goroutine so we don't
// block the caller from making progress
w.wg.Wait()
atomic.StoreInt32(&w.state, StateInit)
}()
}
func (w *Writer) messageRouter() {
for {
select {
case t := <-w.transactionChan:
w.transactions = append(w.transactions, t)
w.SetWriteDeadline(time.Now().Add(w.WriteTimeout))
err := t.cmd.Write(w.Conn)
if err != nil {
log.Printf("ERROR: [%s] failed writing %s", w, err)
w.close()
goto exit
}
case buf := <-w.dataChan:
frameType, data, err := UnpackResponse(buf)
if err != nil {
log.Printf("ERROR: [%s] failed (%s) unpacking response %d %s", w, err, frameType, data)
w.close()
goto exit
}
if frameType == FrameTypeResponse && bytes.Equal(data, []byte("_heartbeat_")) {
log.Printf("[%s] heartbeat received", w)
w.SetWriteDeadline(time.Now().Add(w.WriteTimeout))
err := Nop().Write(w.Conn)
if err != nil {
log.Printf("ERROR: [%s] failed sending heartbeat - %s", w, err)
w.close()
goto exit
}
continue
}
t := w.transactions[0]
w.transactions = w.transactions[1:]
t.FrameType = frameType
t.Data = data
t.Error = nil
t.finish()
case <-w.closeChan:
goto exit
}
}
exit:
w.transactionCleanup()
w.wg.Done()
log.Printf("[%s] exiting messageRouter()", w)
}
func (w *Writer) transactionCleanup() {
// clean up transactions we can easily account for
for _, t := range w.transactions {
t.Error = ErrNotConnected
t.finish()
}
w.transactions = w.transactions[:0]
// spin and free up any writes that might have raced
// with the cleanup process (blocked on writing
// to transactionChan)
for {
select {
case t := <-w.transactionChan:
t.Error = ErrNotConnected
t.finish()
default:
// keep spinning until there are 0 concurrent writers
if atomic.LoadInt32(&w.concurrentWriters) == 0 {
return
}
// give the runtime a chance to schedule other racing goroutines
time.Sleep(5 * time.Millisecond)
continue
}
}
}
func (w *Writer) readLoop() {
rbuf := bufio.NewReader(w.Conn)
for {
w.SetReadDeadline(time.Now().Add(w.HeartbeatInterval * 2))
resp, err := ReadResponse(rbuf)
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
log.Printf("ERROR: [%s] reading response %s", w, err)
}
w.close()
goto exit
}
select {
case w.dataChan <- resp:
case <-w.closeChan:
goto exit
}
}
exit:
w.wg.Done()
log.Printf("[%s] exiting readLoop()", w)
}