forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
produce.go
113 lines (93 loc) · 2.79 KB
/
produce.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
package kafka
import "bufio"
type produceRequestV2 struct {
RequiredAcks int16
Timeout int32
Topics []produceRequestTopicV2
}
func (r produceRequestV2) size() int32 {
return 2 + 4 + sizeofArray(len(r.Topics), func(i int) int32 { return r.Topics[i].size() })
}
func (r produceRequestV2) writeTo(w *bufio.Writer) {
writeInt16(w, r.RequiredAcks)
writeInt32(w, r.Timeout)
writeArray(w, len(r.Topics), func(i int) { r.Topics[i].writeTo(w) })
}
type produceRequestTopicV2 struct {
TopicName string
Partitions []produceRequestPartitionV2
}
func (t produceRequestTopicV2) size() int32 {
return sizeofString(t.TopicName) +
sizeofArray(len(t.Partitions), func(i int) int32 { return t.Partitions[i].size() })
}
func (t produceRequestTopicV2) writeTo(w *bufio.Writer) {
writeString(w, t.TopicName)
writeArray(w, len(t.Partitions), func(i int) { t.Partitions[i].writeTo(w) })
}
type produceRequestPartitionV2 struct {
Partition int32
MessageSetSize int32
MessageSet messageSet
}
func (p produceRequestPartitionV2) size() int32 {
return 4 + 4 + p.MessageSet.size()
}
func (p produceRequestPartitionV2) writeTo(w *bufio.Writer) {
writeInt32(w, p.Partition)
writeInt32(w, p.MessageSetSize)
p.MessageSet.writeTo(w)
}
type produceResponseV2 struct {
ThrottleTime int32
Topics []produceResponseTopicV2
}
func (r produceResponseV2) size() int32 {
return 4 + sizeofArray(len(r.Topics), func(i int) int32 { return r.Topics[i].size() })
}
func (r produceResponseV2) writeTo(w *bufio.Writer) {
writeInt32(w, r.ThrottleTime)
writeArray(w, len(r.Topics), func(i int) { r.Topics[i].writeTo(w) })
}
type produceResponseTopicV2 struct {
TopicName string
Partitions []produceResponsePartitionV2
}
func (t produceResponseTopicV2) size() int32 {
return sizeofString(t.TopicName) +
sizeofArray(len(t.Partitions), func(i int) int32 { return t.Partitions[i].size() })
}
func (t produceResponseTopicV2) writeTo(w *bufio.Writer) {
writeString(w, t.TopicName)
writeArray(w, len(t.Partitions), func(i int) { t.Partitions[i].writeTo(w) })
}
type produceResponsePartitionV2 struct {
Partition int32
ErrorCode int16
Offset int64
Timestamp int64
}
func (p produceResponsePartitionV2) size() int32 {
return 4 + 2 + 8 + 8
}
func (p produceResponsePartitionV2) writeTo(w *bufio.Writer) {
writeInt32(w, p.Partition)
writeInt16(w, p.ErrorCode)
writeInt64(w, p.Offset)
writeInt64(w, p.Timestamp)
}
func (p *produceResponsePartitionV2) readFrom(r *bufio.Reader, sz int) (remain int, err error) {
if remain, err = readInt32(r, sz, &p.Partition); err != nil {
return
}
if remain, err = readInt16(r, remain, &p.ErrorCode); err != nil {
return
}
if remain, err = readInt64(r, remain, &p.Offset); err != nil {
return
}
if remain, err = readInt64(r, remain, &p.Timestamp); err != nil {
return
}
return
}