-
Notifications
You must be signed in to change notification settings - Fork 13
/
message.go
341 lines (303 loc) · 8.68 KB
/
message.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
package nsq
import (
"encoding/binary"
"encoding/json"
"errors"
"io"
"strconv"
"sync/atomic"
"time"
)
// The number of bytes for a Message.ID
const MsgIDLength = 16
const COMPRESS_RATIO_WILD_GUESS = 10
type FullMessageID [MsgIDLength]byte
// MessageID is the binary bytes message ID
type MessageID [MsgIDLength]byte
type NewMessageID uint64
func GetCompatibleMsgIDFromNew(id NewMessageID, traceID uint64) MessageID {
var buf MessageID
binary.BigEndian.PutUint64(buf[:8], uint64(id))
binary.BigEndian.PutUint64(buf[8:16], uint64(traceID))
return buf
}
func GetNewMessageID(old []byte) NewMessageID {
return NewMessageID(binary.BigEndian.Uint64(old[:8]))
}
//ext versions
// version for message has no ext
var NoExtVer = uint8(0)
// version for message has json header ext
var JSONHeaderExtVer = uint8(4)
// Message is the fundamental data type containing
// the id, body, and metadata
type Message struct {
ID MessageID
Body []byte
Timestamp int64
Attempts uint16
NSQDAddress string
Partition string
Delegate MessageDelegate
autoResponseDisabled int32
responded int32
Offset uint64
RawSize uint32
ExtVer uint8
ExtBytes []byte
}
// NewMessage creates a Message, initializes some metadata,
// and returns a pointer
func NewMessage(id MessageID, body []byte) *Message {
return &Message{
ID: id,
Body: body,
Timestamp: time.Now().UnixNano(),
}
}
func (m *Message) GetTraceID() uint64 {
if len(m.ID) < 16 {
return 0
}
return binary.BigEndian.Uint64(m.ID[8:16])
}
func (m *Message) GetFullMsgID() FullMessageID {
return FullMessageID(m.ID)
}
func (m *Message) GetJsonExt() (*MsgExt, error) {
if m.ExtVer != JSONHeaderExtVer {
return nil, errors.New("the header is not json extention")
}
var jext MsgExt
if len(m.ExtBytes) > 0 {
err := json.Unmarshal(m.ExtBytes, &jext.Custom)
if err != nil {
return nil, err
}
}
//fetch tag
if tagV, exist := jext.Custom[dispatchTagExtK]; exist {
tagStr, _ := tagV.(string)
jext.DispatchTag = tagStr
}
//fetch traceID
if traceIDV, exist := jext.Custom[traceIDExtK]; exist {
var err error
traceIDStr, ok := traceIDV.(string)
if !ok {
return nil, errors.New("traceID not string")
}
jext.TraceID, err = strconv.ParseUint(traceIDStr, 10, 64)
if err != nil {
return nil, err
}
}
return &jext, nil
}
// DisableAutoResponse disables the automatic response that
// would normally be sent when a handler.HandleMessage
// returns (FIN/REQ based on the error value returned).
//
// This is useful if you want to batch, buffer, or asynchronously
// respond to messages.
func (m *Message) DisableAutoResponse() {
atomic.StoreInt32(&m.autoResponseDisabled, 1)
}
// IsAutoResponseDisabled indicates whether or not this message
// will be responded to automatically
func (m *Message) IsAutoResponseDisabled() bool {
return atomic.LoadInt32(&m.autoResponseDisabled) == 1
}
// HasResponded indicates whether or not this message has been responded to
func (m *Message) HasResponded() bool {
return atomic.LoadInt32(&m.responded) == 1
}
// Finish sends a FIN command to the nsqd which
// sent this message
func (m *Message) Finish() {
if !atomic.CompareAndSwapInt32(&m.responded, 0, 1) {
return
}
m.Delegate.OnFinish(m)
}
// Touch sends a TOUCH command to the nsqd which
// sent this message
func (m *Message) Touch() {
if m.HasResponded() {
return
}
m.Delegate.OnTouch(m)
}
// Requeue sends a REQ command to the nsqd which
// sent this message, using the supplied delay.
//
// A delay of -1 will automatically calculate
// based on the number of attempts and the
// configured default_requeue_delay
func (m *Message) Requeue(delay time.Duration) {
m.doRequeue(delay, true, false)
}
// RequeueWithoutBackoff sends a REQ command to the nsqd which
// sent this message, using the supplied delay.
//
// Notably, using this method to respond does not trigger a backoff
// event on the configured Delegate.
func (m *Message) RequeueWithoutBackoff(delay time.Duration) {
m.doRequeue(delay, false, false)
}
// RequeueBackoffSingleConn send a REQ to the nsqd which sent this message and
// only do the backoff on the connection which send this message. This is useful while
// only a single connection need to be paused but others need continue consume.
func (m *Message) RequeueBackoffSingleConn(delay time.Duration) {
m.doRequeue(delay, true, true)
}
func (m *Message) doRequeue(delay time.Duration, backoff bool, connOnly bool) {
if !atomic.CompareAndSwapInt32(&m.responded, 0, 1) {
return
}
m.Delegate.OnRequeue(m, delay, backoff, connOnly)
}
// WriteTo implements the WriterTo interface and serializes
// the message into the supplied producer.
//
// It is suggested that the target Writer is buffered to
// avoid performing many system calls.
func (m *Message) WriteTo(w io.Writer) (int64, error) {
var buf [10]byte
var total int64
binary.BigEndian.PutUint64(buf[:8], uint64(m.Timestamp))
binary.BigEndian.PutUint16(buf[8:10], uint16(m.Attempts))
n, err := w.Write(buf[:])
total += int64(n)
if err != nil {
return total, err
}
n, err = w.Write(m.ID[:])
total += int64(n)
if err != nil {
return total, err
}
n, err = w.Write(m.Body)
total += int64(n)
if err != nil {
return total, err
}
return total, nil
}
// DecodeMessage deseralizes data (as []byte) and creates a new Message
func DecodeMessage(b []byte) (*Message, error) {
if len(b) < 10+MsgIDLength {
return nil, errors.New("not enough data to decode valid message")
}
var msg Message
msg.Timestamp = int64(binary.BigEndian.Uint64(b[:8]))
msg.Attempts = binary.BigEndian.Uint16(b[8:10])
copy(msg.ID[:], b[10:10+MsgIDLength])
msg.Body = b[10+MsgIDLength:]
return &msg, nil
}
// DecodeMessage deseralizes data (as []byte) and creates a new Message
// b row bytes to decode
// ext indicate whether decode message in ext format
// disableDecompress indicate whether depress message bytes, when message is compressed
func DecodeMessageWithExt(b []byte, ext bool, disableDecompress bool) (*Message, error) {
if len(b) < 10+MsgIDLength {
return nil, errors.New("not enough data to decode valid message")
}
var msg Message
pos := 0
msg.Timestamp = int64(binary.BigEndian.Uint64(b[:8]))
pos += 8
msg.Attempts = binary.BigEndian.Uint16(b[pos : pos+2])
pos += 2
copy(msg.ID[:], b[pos:pos+MsgIDLength])
pos += MsgIDLength
if ext {
if len(b) < pos+1 {
return nil, errors.New("not enough data to decode valid message")
}
msg.ExtVer = uint8(b[pos])
pos++
switch msg.ExtVer {
case 0x0:
default:
if len(b) < pos+2 {
return nil, errors.New("not enough data to decode valid message")
}
extLen := binary.BigEndian.Uint16(b[pos : pos+2])
pos += 2
if len(b) < pos+int(extLen) {
return nil, errors.New("not enough data to decode valid message")
}
msg.ExtBytes = b[pos : pos+int(extLen)]
pos += int(extLen)
}
}
if ext && msg.ExtVer == JSONHeaderExtVer {
json, err := msg.GetJsonExt()
if err != nil {
return nil, err
}
if disableDecompress {
msg.Body = b[pos:]
} else {
var decompressed bool
msg.Body, decompressed, err = tryDecompress(b[pos:], json)
if decompressed {
removeCompressHeader(&msg, json)
}
}
if err != nil {
return nil, err
}
} else {
msg.Body = b[pos:]
}
return &msg, nil
}
func tryDecompress(bodyMayCompressed []byte, ext *MsgExt) ([]byte, bool, error) {
compressH := ext.Custom[NSQ_CLIENT_COMPRESS_HEADER_KEY]
compressHStr, _ := compressH.(string)
if compressHStr != "" {
codecNo, err := strconv.Atoi(compressHStr)
if err != nil {
return nil, false, err
}
codec, err := GetNSQClientCompressCodeByCodecNo(codecNo)
if err != nil {
return nil, false, err
}
compressHSize := ext.Custom[NSQ_CLIENT_COMPRESS_SIZE_HEADER_KEY]
origSizeStr, ok := compressHSize.(string)
var originalMsgSize int
if ok {
originalMsgSize, err = strconv.Atoi(origSizeStr)
if err != nil {
return nil, false, err
}
} else {
//try luck with wild guess
originalMsgSize = len(bodyMayCompressed) * COMPRESS_RATIO_WILD_GUESS
}
decompressed, err := codec.Decompress(bodyMayCompressed, originalMsgSize)
if err != nil {
return nil, false, err
}
return decompressed, true, err
} else {
return bodyMayCompressed, false, nil
}
}
//after a successful decompress, remove NSQ_CLIENT_COMPRESS_HEADER_KEY and NSQ_CLIENT_COMPRESS_SIZE_HEADER_KEY
//and build new json header with ext.Custom map
func removeCompressHeader(msg *Message, ext *MsgExt) error {
//remove nsq compress codec key in header, before message process
delete(ext.Custom, NSQ_CLIENT_COMPRESS_HEADER_KEY)
delete(ext.Custom, NSQ_CLIENT_COMPRESS_SIZE_HEADER_KEY)
newExtJsonBytes, err := json.Marshal(ext.Custom)
if err != nil {
return err
}
msg.ExtBytes = newExtJsonBytes
return nil
}