/
produce_response.go
118 lines (107 loc) · 2.48 KB
/
produce_response.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
package protocol
import "time"
type ProducePartitionResponse struct {
Partition int32
ErrorCode int16
BaseOffset int64
LogAppendTime time.Time
LogStartOffset int64
}
type ProduceTopicResponse struct {
Topic string
PartitionResponses []*ProducePartitionResponse
}
type ProduceResponse struct {
APIVersion int16
Responses []*ProduceTopicResponse
ThrottleTime time.Duration
}
func (r *ProduceResponse) Encode(e PacketEncoder) (err error) {
if err = e.PutArrayLength(len(r.Responses)); err != nil {
return err
}
for _, resp := range r.Responses {
if err = e.PutString(resp.Topic); err != nil {
return err
}
if err = e.PutArrayLength(len(resp.PartitionResponses)); err != nil {
return err
}
for _, p := range resp.PartitionResponses {
e.PutInt32(p.Partition)
e.PutInt16(p.ErrorCode)
if r.APIVersion >= 2 {
e.PutInt64(p.BaseOffset)
e.PutInt64(int64(p.LogAppendTime.UnixNano() / int64(time.Millisecond)))
}
if r.APIVersion >= 5 {
e.PutInt64(p.LogStartOffset)
}
}
}
if r.APIVersion >= 1 {
e.PutInt32(int32(r.ThrottleTime / time.Millisecond))
}
return nil
}
type ProduceTopicResponses []*ProduceTopicResponse
type ProducePartitionResponses []*ProducePartitionResponse
func (r *ProduceResponse) Decode(d PacketDecoder, version int16) (err error) {
r.APIVersion = version
l, err := d.ArrayLength()
if err != nil {
return err
}
r.Responses = make([]*ProduceTopicResponse, l)
for i := range r.Responses {
resp := new(ProduceTopicResponse)
r.Responses[i] = resp
resp.Topic, err = d.String()
if err != nil {
return err
}
pl, err := d.ArrayLength()
if err != nil {
return err
}
ps := make([]*ProducePartitionResponse, pl)
for j := range ps {
p := new(ProducePartitionResponse)
ps[j] = p
p.Partition, err = d.Int32()
if err != nil {
return err
}
p.ErrorCode, err = d.Int16()
if err != nil {
return err
}
p.BaseOffset, err = d.Int64()
if err != nil {
return err
}
if r.APIVersion >= 2 {
millis, err := d.Int64()
if err != nil {
return err
}
p.LogAppendTime = time.Unix(millis/1000, (millis%1000)*int64(time.Millisecond))
}
if r.APIVersion >= 5 {
p.LogStartOffset, err = d.Int64()
if err != nil {
return err
}
}
}
resp.PartitionResponses = ps
}
if r.APIVersion >= 1 {
throttle, err := d.Int32()
if err != nil {
return err
}
r.ThrottleTime = time.Duration(throttle) * time.Millisecond
}
return nil
}