forked from travisjeffery/jocko
/
fetch_request.go
107 lines (100 loc) · 1.92 KB
/
fetch_request.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
package protocol
type FetchPartition struct {
Partition int32
FetchOffset int64
MaxBytes int32
}
type FetchTopic struct {
Topic string
Partitions []*FetchPartition
}
type FetchRequest struct {
ReplicaID int32
MaxWaitTime int32
MinBytes int32
// MaxBytes int32
Topics []*FetchTopic
}
func (r *FetchRequest) Encode(e PacketEncoder) error {
if r.ReplicaID == 0 {
e.PutInt32(-1) // replica ID is -1 for clients
} else {
e.PutInt32(r.ReplicaID)
}
e.PutInt32(r.MaxWaitTime)
e.PutInt32(r.MinBytes)
// e.PutInt32(r.MaxBytes)
e.PutArrayLength(len(r.Topics))
for _, t := range r.Topics {
e.PutString(t.Topic)
e.PutArrayLength(len(t.Partitions))
for _, p := range t.Partitions {
e.PutInt32(p.Partition)
e.PutInt64(p.FetchOffset)
e.PutInt32(p.MaxBytes)
}
}
return nil
}
func (r *FetchRequest) Decode(d PacketDecoder) error {
var err error
r.ReplicaID, err = d.Int32()
if err != nil {
return err
}
r.MaxWaitTime, err = d.Int32()
if err != nil {
return err
}
r.MinBytes, err = d.Int32()
if err != nil {
return err
}
// r.MaxBytes, err = d.Int32()
// if err != nil {
// return err
// }
topicCount, err := d.ArrayLength()
if err != nil {
return err
}
topics := make([]*FetchTopic, topicCount)
for i := range topics {
t := &FetchTopic{}
t.Topic, err = d.String()
if err != nil {
return err
}
partitionCount, err := d.ArrayLength()
if err != nil {
return err
}
ps := make([]*FetchPartition, partitionCount)
for j := range ps {
p := &FetchPartition{}
p.Partition, err = d.Int32()
if err != nil {
return err
}
p.FetchOffset, err = d.Int64()
if err != nil {
return err
}
p.MaxBytes, err = d.Int32()
if err != nil {
return err
}
ps[j] = p
}
t.Partitions = ps
topics[i] = t
}
r.Topics = topics
return nil
}
func (r *FetchRequest) Key() int16 {
return FetchKey
}
func (r *FetchRequest) Version() int16 {
return 1
}