-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
87 lines (73 loc) · 2.62 KB
/
message.go
File metadata and controls
87 lines (73 loc) · 2.62 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
package flow
import "encoding/binary"
// Message holds the data of a streaming message. A message is able to
// marshal into and unmarshal from a binary representation, which is used
// internally to send messages over the wire.
type Message struct {
Stream string // published to the pub/sub system
PartitionKey []byte // the key for partitioning
Data []byte // the data which should be sent
slot int
}
// Slot returns the slot in which the handler is processed. If a message
// has a partition key and partitions are processed synchronously, the
// returned slot will be between 0 and the number of configured slots
// (see WithSyncPartitions). Otherwise -1 will be returned.
func (m *Message) Slot() int {
return m.slot
}
func (m *Message) validate() error {
if m.Stream == "" {
return errorString("missing message stream")
}
return nil
}
// Codec defines an interface for encoding messages to and
// decoding messages from binary data.
type Codec interface {
EncodeMessage(msg Message) []byte
DecodeMessage(stream string, data []byte) (Message, error)
}
// DefaultCodec represents the default codec for messages. If no custom
// codec is set, the default codec will be used for message encoding and
// decoding.
type DefaultCodec struct{}
// EncodeMessage encodes the message into its binary representation.
func (c DefaultCodec) EncodeMessage(msg Message) []byte {
streamLen := len(msg.Stream)
pkeyLen := len(msg.PartitionKey)
p := alloc(8+streamLen+pkeyLen+len(msg.Data), nil)
binary.BigEndian.PutUint32(p, uint32(streamLen))
copy(p[4:], msg.Stream)
binary.BigEndian.PutUint32(p[4+streamLen:], uint32(pkeyLen))
copy(p[8+streamLen:], msg.PartitionKey)
copy(p[8+streamLen+pkeyLen:], msg.Data)
return p
}
// DecodeMessage decodes the message from its binary representation.
// The decoded message retains the given data to avoid copies. If the
// data buffer should be reused after the decoding, the caller is
// responsible for copying the data before passing it.
func (c DefaultCodec) DecodeMessage(stream string, p []byte) (Message, error) {
const minSize = 8
if len(p) < minSize {
return Message{}, errMalformedMessage
}
streamLen := binary.BigEndian.Uint32(p)
if len(p) < minSize+int(streamLen) {
return Message{}, errMalformedMessage
}
pkeyLen := binary.BigEndian.Uint32(p[4+streamLen:])
if len(p) < minSize+int(streamLen+pkeyLen) {
return Message{}, errMalformedMessage
}
msg := Message{
Stream: string(p[4 : 4+streamLen]),
PartitionKey: p[8+streamLen : 8+streamLen+pkeyLen],
Data: p[8+streamLen+pkeyLen:],
}
if msg.Stream == "" {
msg.Stream = stream
}
return msg, nil
}