This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
offsets_request.go
110 lines (100 loc) · 2.02 KB
/
offsets_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
108
109
110
package protocol
type OffsetsPartition struct {
Partition int32
Timestamp int64 // -1 to receive latest offset, -2 to receive earliest offset
MaxNumOffsets int32
}
type OffsetsTopic struct {
Topic string
Partitions []*OffsetsPartition
}
type OffsetsRequest struct {
APIVersion int16
ReplicaID int32
IsolationLevel int8
Topics []*OffsetsTopic
}
func (r *OffsetsRequest) Encode(e PacketEncoder) (err error) {
e.PutInt32(-1)
if r.APIVersion >= 2 {
e.PutInt8(r.IsolationLevel)
}
err = e.PutArrayLength(len(r.Topics))
if err != nil {
return err
}
for _, t := range r.Topics {
err = e.PutString(t.Topic)
if err != nil {
return err
}
err = e.PutArrayLength(len(t.Partitions))
if err != nil {
return err
}
for _, p := range t.Partitions {
e.PutInt32(p.Partition)
e.PutInt64(p.Timestamp)
if r.APIVersion == 0 {
e.PutInt32(p.MaxNumOffsets)
}
}
}
return nil
}
func (r *OffsetsRequest) Decode(d PacketDecoder, version int16) (err error) {
r.APIVersion = version
r.ReplicaID, err = d.Int32()
if err != nil {
return err
}
if version >= 2 {
r.IsolationLevel, err = d.Int8()
if err != nil {
return err
}
}
topicCount, err := d.ArrayLength()
if err != nil {
return err
}
r.Topics = make([]*OffsetsTopic, topicCount)
for i := range r.Topics {
ot := new(OffsetsTopic)
ot.Topic, err = d.String()
if err != nil {
return err
}
partitionCount, err := d.ArrayLength()
if err != nil {
return err
}
ot.Partitions = make([]*OffsetsPartition, partitionCount)
for j := range ot.Partitions {
p := new(OffsetsPartition)
p.Partition, err = d.Int32()
if err != nil {
return err
}
p.Timestamp, err = d.Int64()
if err != nil {
return err
}
if version == 0 {
p.MaxNumOffsets, err = d.Int32()
if err != nil {
return err
}
}
ot.Partitions[j] = p
}
r.Topics[i] = ot
}
return err
}
func (r *OffsetsRequest) Key() int16 {
return OffsetsKey
}
func (r *OffsetsRequest) Version() int16 {
return r.APIVersion
}