forked from travisjeffery/jocko
/
fetch_response.go
85 lines (79 loc) · 1.75 KB
/
fetch_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
package protocol
type FetchPartitionResponse struct {
Partition int32
ErrorCode int16
HighWatermark int64
RecordSet []byte
}
type FetchResponse struct {
Topic string
PartitionResponses []*FetchPartitionResponse
}
type FetchResponses struct {
ThrottleTimeMs int32
Responses []*FetchResponse
}
func (r *FetchResponses) Encode(e PacketEncoder) (err error) {
e.PutInt32(r.ThrottleTimeMs)
if err = e.PutArrayLength(len(r.Responses)); err != nil {
return err
}
for _, r := range r.Responses {
if err = e.PutString(r.Topic); err != nil {
return err
}
if err = e.PutArrayLength(len(r.PartitionResponses)); err != nil {
return err
}
for _, p := range r.PartitionResponses {
e.PutInt32(p.Partition)
e.PutInt16(p.ErrorCode)
e.PutInt64(p.HighWatermark)
if err = e.PutBytes(p.RecordSet); err != nil {
return err
}
}
}
return nil
}
func (r *FetchResponses) Decode(d PacketDecoder) error {
var err error
r.ThrottleTimeMs, err = d.Int32()
if err != nil {
return err
}
responseCount, err := d.ArrayLength()
r.Responses = make([]*FetchResponse, responseCount)
for i := range r.Responses {
resp := &FetchResponse{}
resp.Topic, err = d.String()
if err != nil {
return err
}
partitionCount, err := d.ArrayLength()
ps := make([]*FetchPartitionResponse, partitionCount)
for j := range ps {
p := &FetchPartitionResponse{}
p.Partition, err = d.Int32()
if err != nil {
return err
}
p.ErrorCode, err = d.Int16()
if err != nil {
return err
}
p.HighWatermark, err = d.Int64()
if err != nil {
return err
}
p.RecordSet, err = d.Bytes()
if err != nil {
return err
}
ps[j] = p
}
resp.PartitionResponses = ps
r.Responses[i] = resp
}
return nil
}