-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_header.go
59 lines (54 loc) · 1.11 KB
/
request_header.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
package protocol
import "fmt"
type RequestHeader struct {
// Size of the request
Size int32
// ID of the API (e.g. produce, fetch, metadata)
APIKey int16
// Version of the API to use
APIVersion int16
// User defined ID to correlate requests between server and client
CorrelationID int32
// Size of the Client ID
ClientID string
}
func (r *RequestHeader) Encode(e PacketEncoder) {
e.PutInt32(r.Size)
e.PutInt16(r.APIKey)
e.PutInt16(r.APIVersion)
e.PutInt32(r.CorrelationID)
if err := e.PutString(r.ClientID); err != nil {
// TODO: better err handling
panic(err)
}
}
func (r *RequestHeader) Decode(d PacketDecoder) error {
var err error
r.Size, err = d.Int32()
if err != nil {
return err
}
r.APIKey, err = d.Int16()
if err != nil {
return err
}
r.APIVersion, err = d.Int16()
if err != nil {
return err
}
r.CorrelationID, err = d.Int32()
if err != nil {
return err
}
r.ClientID, err = d.String()
return err
}
func (r *RequestHeader) String() string {
return fmt.Sprintf(
"correlation id: %d, api key: %d, client: %s, size: %d",
r.CorrelationID,
r.APIKey,
r.ClientID,
r.Size,
)
}