-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_v2.go
424 lines (347 loc) · 10 KB
/
client_v2.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"bufio"
"compress/flate"
"crypto/tls"
"errors"
"fmt"
"github.com/bitly/go-nsq"
"github.com/mreiferson/go-snappystream"
"log"
"net"
"sync"
"sync/atomic"
"time"
)
const DefaultBufferSize = 16 * 1024
type IdentifyDataV2 struct {
ShortId string `json:"short_id"`
LongId string `json:"long_id"`
HeartbeatInterval int `json:"heartbeat_interval"`
OutputBufferSize int `json:"output_buffer_size"`
OutputBufferTimeout int `json:"output_buffer_timeout"`
FeatureNegotiation bool `json:"feature_negotiation"`
TLSv1 bool `json:"tls_v1"`
Deflate bool `json:"deflate"`
DeflateLevel int `json:"deflate_level"`
Snappy bool `json:"snappy"`
SampleRate int32 `json:"sample_rate"`
UserAgent string `json:"user_agent"`
}
type ClientV2 struct {
// 64bit atomic vars need to be first for proper alignment on 32bit platforms
ReadyCount int64
LastReadyCount int64
InFlightCount int64
MessageCount uint64
FinishCount uint64
RequeueCount uint64
sync.Mutex
ID int64
context *Context
UserAgent string
// original connection
net.Conn
// connections based on negotiated features
tlsConn *tls.Conn
flateWriter *flate.Writer
// reading/writing interfaces
Reader *bufio.Reader
Writer *bufio.Writer
// output buffering
OutputBufferSize int
OutputBufferTimeout *time.Ticker
OutputBufferTimeoutUpdateChan chan time.Duration
State int32
ConnectTime time.Time
Channel *Channel
ReadyStateChan chan int
ExitChan chan int
ShortIdentifier string
LongIdentifier string
SubEventChan chan *Channel
SampleRate int32
SampleRateUpdateChan chan int32
// states for exposing to nsqadmin
TLS int32
Snappy int32
Deflate int32
// re-usable buffer for reading the 4-byte lengths off the wire
lenBuf [4]byte
lenSlice []byte
// heartbeats are client configurable via IDENTIFY
Heartbeat *time.Ticker
HeartbeatInterval time.Duration
HeartbeatUpdateChan chan time.Duration
}
func NewClientV2(id int64, conn net.Conn, context *Context) *ClientV2 {
var identifier string
if conn != nil {
identifier, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
}
c := &ClientV2{
ID: id,
context: context,
Conn: conn,
Reader: bufio.NewReaderSize(conn, DefaultBufferSize),
Writer: bufio.NewWriterSize(conn, DefaultBufferSize),
OutputBufferSize: DefaultBufferSize,
OutputBufferTimeout: time.NewTicker(250 * time.Millisecond),
OutputBufferTimeoutUpdateChan: make(chan time.Duration, 1),
// ReadyStateChan has a buffer of 1 to guarantee that in the event
// there is a race the state update is not lost
ReadyStateChan: make(chan int, 1),
ExitChan: make(chan int),
ConnectTime: time.Now(),
ShortIdentifier: identifier,
LongIdentifier: identifier,
State: nsq.StateInit,
SubEventChan: make(chan *Channel, 1),
SampleRateUpdateChan: make(chan int32, 1),
// heartbeats are client configurable but default to 30s
Heartbeat: time.NewTicker(context.nsqd.options.ClientTimeout / 2),
HeartbeatInterval: context.nsqd.options.ClientTimeout / 2,
HeartbeatUpdateChan: make(chan time.Duration, 1),
}
c.lenSlice = c.lenBuf[:]
return c
}
func (c *ClientV2) String() string {
return c.RemoteAddr().String()
}
func (c *ClientV2) Identify(data IdentifyDataV2) error {
c.ShortIdentifier = data.ShortId
c.LongIdentifier = data.LongId
c.UserAgent = data.UserAgent
err := c.SetHeartbeatInterval(data.HeartbeatInterval)
if err != nil {
return err
}
err = c.SetOutputBufferSize(data.OutputBufferSize)
if err != nil {
return err
}
err = c.SetOutputBufferTimeout(data.OutputBufferTimeout)
if err != nil {
return err
}
return c.SetSampleRate(data.SampleRate)
}
func (c *ClientV2) Stats() ClientStats {
return ClientStats{
Version: "V2",
RemoteAddress: c.RemoteAddr().String(),
Name: c.ShortIdentifier,
UserAgent: c.UserAgent,
State: atomic.LoadInt32(&c.State),
ReadyCount: atomic.LoadInt64(&c.ReadyCount),
InFlightCount: atomic.LoadInt64(&c.InFlightCount),
MessageCount: atomic.LoadUint64(&c.MessageCount),
FinishCount: atomic.LoadUint64(&c.FinishCount),
RequeueCount: atomic.LoadUint64(&c.RequeueCount),
ConnectTime: c.ConnectTime.Unix(),
SampleRate: atomic.LoadInt32(&c.SampleRate),
TLS: atomic.LoadInt32(&c.TLS) == 1,
Deflate: atomic.LoadInt32(&c.Deflate) == 1,
Snappy: atomic.LoadInt32(&c.Snappy) == 1,
}
}
func (c *ClientV2) IsReadyForMessages() bool {
if c.Channel.IsPaused() {
return false
}
readyCount := atomic.LoadInt64(&c.ReadyCount)
lastReadyCount := atomic.LoadInt64(&c.LastReadyCount)
inFlightCount := atomic.LoadInt64(&c.InFlightCount)
if *verbose {
log.Printf("[%s] state rdy: %4d lastrdy: %4d inflt: %4d", c,
readyCount, lastReadyCount, inFlightCount)
}
if inFlightCount >= lastReadyCount || readyCount <= 0 {
return false
}
return true
}
func (c *ClientV2) SetReadyCount(count int64) {
atomic.StoreInt64(&c.ReadyCount, count)
atomic.StoreInt64(&c.LastReadyCount, count)
c.tryUpdateReadyState()
}
func (c *ClientV2) tryUpdateReadyState() {
// you can always *try* to write to ReadyStateChan because in the cases
// where you cannot the message pump loop would have iterated anyway.
// the atomic integer operations guarantee correctness of the value.
select {
case c.ReadyStateChan <- 1:
default:
}
}
func (c *ClientV2) FinishedMessage() {
atomic.AddUint64(&c.FinishCount, 1)
atomic.AddInt64(&c.InFlightCount, -1)
c.tryUpdateReadyState()
}
func (c *ClientV2) Empty() {
atomic.StoreInt64(&c.InFlightCount, 0)
c.tryUpdateReadyState()
}
func (c *ClientV2) SendingMessage() {
atomic.AddInt64(&c.ReadyCount, -1)
atomic.AddInt64(&c.InFlightCount, 1)
atomic.AddUint64(&c.MessageCount, 1)
}
func (c *ClientV2) TimedOutMessage() {
atomic.AddInt64(&c.InFlightCount, -1)
c.tryUpdateReadyState()
}
func (c *ClientV2) RequeuedMessage() {
atomic.AddUint64(&c.RequeueCount, 1)
atomic.AddInt64(&c.InFlightCount, -1)
c.tryUpdateReadyState()
}
func (c *ClientV2) StartClose() {
// Force the client into ready 0
c.SetReadyCount(0)
// mark this client as closing
atomic.StoreInt32(&c.State, nsq.StateClosing)
}
func (c *ClientV2) Pause() {
c.tryUpdateReadyState()
}
func (c *ClientV2) UnPause() {
c.tryUpdateReadyState()
}
func (c *ClientV2) SetHeartbeatInterval(desiredInterval int) error {
// clients can modify the rate of heartbeats (or disable)
var interval time.Duration
switch {
case desiredInterval == -1:
interval = -1
case desiredInterval == 0:
// do nothing (use default)
case desiredInterval >= 1000 &&
desiredInterval <= int(c.context.nsqd.options.MaxHeartbeatInterval/time.Millisecond):
interval = (time.Duration(desiredInterval) * time.Millisecond)
default:
return errors.New(fmt.Sprintf("heartbeat interval (%d) is invalid", desiredInterval))
}
// leave the default heartbeat in place
if desiredInterval != 0 {
select {
case c.HeartbeatUpdateChan <- interval:
default:
}
c.HeartbeatInterval = interval
}
return nil
}
func (c *ClientV2) SetOutputBufferSize(desiredSize int) error {
c.Lock()
defer c.Unlock()
var size int
switch {
case desiredSize == -1:
// effectively no buffer (every write will go directly to the wrapped net.Conn)
size = 1
case desiredSize == 0:
// do nothing (use default)
case desiredSize >= 64 && desiredSize <= int(c.context.nsqd.options.MaxOutputBufferSize):
size = desiredSize
default:
return errors.New(fmt.Sprintf("output buffer size (%d) is invalid", desiredSize))
}
if size > 0 {
err := c.Writer.Flush()
if err != nil {
return err
}
c.OutputBufferSize = size
c.Writer = bufio.NewWriterSize(c.Conn, size)
}
return nil
}
func (c *ClientV2) SetOutputBufferTimeout(desiredTimeout int) error {
var timeout time.Duration
switch {
case desiredTimeout == -1:
timeout = -1
case desiredTimeout == 0:
// do nothing (use default)
case desiredTimeout >= 1 &&
desiredTimeout <= int(c.context.nsqd.options.MaxOutputBufferTimeout/time.Millisecond):
timeout = (time.Duration(desiredTimeout) * time.Millisecond)
default:
return errors.New(fmt.Sprintf("output buffer timeout (%d) is invalid", desiredTimeout))
}
if desiredTimeout != 0 {
select {
case c.OutputBufferTimeoutUpdateChan <- timeout:
default:
}
}
return nil
}
func (c *ClientV2) SetSampleRate(sampleRate int32) error {
if sampleRate < 0 || sampleRate > 99 {
return errors.New(fmt.Sprintf("sample rate (%d) is invalid", sampleRate))
}
if sampleRate != 0 {
atomic.StoreInt32(&c.SampleRate, sampleRate)
select {
case c.SampleRateUpdateChan <- sampleRate:
default:
}
}
return nil
}
func (c *ClientV2) UpgradeTLS() error {
c.Lock()
defer c.Unlock()
tlsConn := tls.Server(c.Conn, c.context.nsqd.tlsConfig)
err := tlsConn.Handshake()
if err != nil {
return err
}
c.tlsConn = tlsConn
c.Reader = bufio.NewReaderSize(c.tlsConn, DefaultBufferSize)
c.Writer = bufio.NewWriterSize(c.tlsConn, c.OutputBufferSize)
atomic.StoreInt32(&c.TLS, 1)
return nil
}
func (c *ClientV2) UpgradeDeflate(level int) error {
c.Lock()
defer c.Unlock()
conn := c.Conn
if c.tlsConn != nil {
conn = c.tlsConn
}
c.Reader = bufio.NewReaderSize(flate.NewReader(conn), DefaultBufferSize)
fw, _ := flate.NewWriter(conn, level)
c.flateWriter = fw
c.Writer = bufio.NewWriterSize(fw, c.OutputBufferSize)
atomic.StoreInt32(&c.Deflate, 1)
return nil
}
func (c *ClientV2) UpgradeSnappy() error {
c.Lock()
defer c.Unlock()
conn := c.Conn
if c.tlsConn != nil {
conn = c.tlsConn
}
c.Reader = bufio.NewReaderSize(snappystream.NewReader(conn, snappystream.SkipVerifyChecksum), DefaultBufferSize)
c.Writer = bufio.NewWriterSize(snappystream.NewWriter(conn), c.OutputBufferSize)
atomic.StoreInt32(&c.Snappy, 1)
return nil
}
func (c *ClientV2) Flush() error {
c.SetWriteDeadline(time.Now().Add(time.Second))
err := c.Writer.Flush()
if err != nil {
return err
}
if c.flateWriter != nil {
return c.flateWriter.Flush()
}
return nil
}