forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
produce.go
152 lines (128 loc) · 3.7 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
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
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(wb *writeBuffer) {
wb.writeInt16(r.RequiredAcks)
wb.writeInt32(r.Timeout)
wb.writeArray(len(r.Topics), func(i int) { r.Topics[i].writeTo(wb) })
}
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(wb *writeBuffer) {
wb.writeString(t.TopicName)
wb.writeArray(len(t.Partitions), func(i int) { t.Partitions[i].writeTo(wb) })
}
type produceRequestPartitionV2 struct {
Partition int32
MessageSetSize int32
MessageSet messageSet
}
func (p produceRequestPartitionV2) size() int32 {
return 4 + 4 + p.MessageSet.size()
}
func (p produceRequestPartitionV2) writeTo(wb *writeBuffer) {
wb.writeInt32(p.Partition)
wb.writeInt32(p.MessageSetSize)
p.MessageSet.writeTo(wb)
}
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(wb *writeBuffer) {
wb.writeInt32(r.ThrottleTime)
wb.writeArray(len(r.Topics), func(i int) { r.Topics[i].writeTo(wb) })
}
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(wb *writeBuffer) {
wb.writeString(t.TopicName)
wb.writeArray(len(t.Partitions), func(i int) { t.Partitions[i].writeTo(wb) })
}
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(wb *writeBuffer) {
wb.writeInt32(p.Partition)
wb.writeInt16(p.ErrorCode)
wb.writeInt64(p.Offset)
wb.writeInt64(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
}
type produceResponsePartitionV7 struct {
Partition int32
ErrorCode int16
Offset int64
Timestamp int64
StartOffset int64
}
func (p produceResponsePartitionV7) size() int32 {
return 4 + 2 + 8 + 8 + 8
}
func (p produceResponsePartitionV7) writeTo(wb *writeBuffer) {
wb.writeInt32(p.Partition)
wb.writeInt16(p.ErrorCode)
wb.writeInt64(p.Offset)
wb.writeInt64(p.Timestamp)
wb.writeInt64(p.StartOffset)
}
func (p *produceResponsePartitionV7) 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
}
if remain, err = readInt64(r, remain, &p.StartOffset); err != nil {
return
}
return
}