-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjoingroup_request.go
105 lines (85 loc) · 2.85 KB
/
joingroup_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
package healer
import (
"encoding/binary"
)
/*
https://kafka.apache.org/protocol.html#The_Messages_JoinGroup
*/
// JoinGroupRequest struct holds params in JoinGroupRequest
type JoinGroupRequest struct {
*RequestHeader
GroupID string
SessionTimeout int32 // ms
RebalanceTimeout int32 // ms. this is NOT included in verions 0
MemberID string
ProtocolType string
GroupProtocols []*GroupProtocol
}
// GroupProtocol is sub struct in JoinGroupRequest
type GroupProtocol struct {
ProtocolName string
ProtocolMetadata []byte
}
// NewJoinGroupRequest create a JoinGroupRequest
func NewJoinGroupRequest(apiVersion uint16, clientID string) *JoinGroupRequest {
requestHeader := &RequestHeader{
APIKey: API_JoinGroup,
APIVersion: apiVersion,
ClientID: &clientID,
}
return &JoinGroupRequest{
RequestHeader: requestHeader,
GroupProtocols: []*GroupProtocol{},
}
}
// AddGroupProtocal add new GroupProtocol to JoinGroupReuqest
func (r *JoinGroupRequest) AddGroupProtocal(gp *GroupProtocol) {
r.GroupProtocols = append(r.GroupProtocols, gp)
}
func (r *JoinGroupRequest) length() int {
l := r.RequestHeader.length() + 2 + len(r.GroupID) + 4 + 2 + len(r.MemberID) + 2 + len(r.ProtocolType)
if r.RequestHeader.APIVersion == 1 || r.RequestHeader.APIVersion == 2 {
l += 4 // RebalanceTimeout
}
l += 4
for _, gp := range r.GroupProtocols {
l += 2 + len(gp.ProtocolName)
l += 4 + len(gp.ProtocolMetadata)
}
return l
}
// Encode encodes the JoinGroupRequest object to []byte. it implement Request Interface
func (r *JoinGroupRequest) Encode(version uint16) []byte {
requestLength := r.length()
payload := make([]byte, requestLength+4)
offset := 0
binary.BigEndian.PutUint32(payload[offset:], uint32(requestLength))
offset += 4
offset += r.RequestHeader.EncodeTo(payload[offset:])
binary.BigEndian.PutUint16(payload[offset:], uint16(len(r.GroupID)))
offset += 2
offset += copy(payload[offset:], r.GroupID)
binary.BigEndian.PutUint32(payload[offset:], uint32(r.SessionTimeout))
offset += 4
if r.Version() == 1 || r.Version() == 2 {
binary.BigEndian.PutUint32(payload[offset:], uint32(r.RebalanceTimeout))
offset += 4
}
binary.BigEndian.PutUint16(payload[offset:], uint16(len(r.MemberID)))
offset += 2
offset += copy(payload[offset:], r.MemberID)
binary.BigEndian.PutUint16(payload[offset:], uint16(len(r.ProtocolType)))
offset += 2
offset += copy(payload[offset:], r.ProtocolType)
binary.BigEndian.PutUint32(payload[offset:], uint32(len(r.GroupProtocols)))
offset += 4
for _, gp := range r.GroupProtocols {
binary.BigEndian.PutUint16(payload[offset:], uint16(len(gp.ProtocolName)))
offset += 2
offset += copy(payload[offset:], gp.ProtocolName)
binary.BigEndian.PutUint32(payload[offset:], uint32(len(gp.ProtocolMetadata)))
offset += 4
offset += copy(payload[offset:], gp.ProtocolMetadata)
}
return payload
}