-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathheartbeat_request.go
69 lines (55 loc) · 1.74 KB
/
heartbeat_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
package healer
import (
"encoding/binary"
)
/*
Heartbeat Request (Version: 0) => group_id generation_id member_id
group_id => STRING
generation_id => INT32
member_id => STRING
FIELD DESCRIPTION
group_id The unique group identifier
generation_id The generation of the group.
member_id The member id assigned by the group coordinator or null if joining for the first time.
*/
// TODO version0
type HeartbeatRequest struct {
*RequestHeader
GroupID string
GenerationID int32
MemberID string
}
func NewHeartbeatRequest(clientID, groupID string, generationID int32, memberID string) *HeartbeatRequest {
requestHeader := &RequestHeader{
APIKey: API_Heartbeat,
APIVersion: 0,
ClientID: &clientID,
}
return &HeartbeatRequest{
RequestHeader: requestHeader,
GroupID: groupID,
GenerationID: generationID,
MemberID: memberID,
}
}
func (heartbeatR *HeartbeatRequest) Length() int {
requestLength := heartbeatR.RequestHeader.length() + 2 + len(heartbeatR.GroupID) + 4 + 2 + len(heartbeatR.MemberID)
return requestLength
}
func (heartbeatR *HeartbeatRequest) Encode(version uint16) []byte {
requestLength := heartbeatR.Length()
payload := make([]byte, requestLength+4)
offset := 0
binary.BigEndian.PutUint32(payload[offset:], uint32(requestLength))
offset += 4
offset += heartbeatR.RequestHeader.EncodeTo(payload[offset:])
binary.BigEndian.PutUint16(payload[offset:], uint16(len(heartbeatR.GroupID)))
offset += 2
offset += copy(payload[offset:], heartbeatR.GroupID)
binary.BigEndian.PutUint32(payload[offset:], uint32(heartbeatR.GenerationID))
offset += 4
binary.BigEndian.PutUint16(payload[offset:], uint16(len(heartbeatR.MemberID)))
offset += 2
copy(payload[offset:], heartbeatR.MemberID)
return payload
}