-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathidentify.go
128 lines (102 loc) · 2.89 KB
/
identify.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package nsq
import (
"bufio"
"encoding/binary"
"encoding/json"
"io"
"os"
"time"
"github.com/pkg/errors"
)
// Identify represents the IDENTIFY command.
type Identify struct {
// ClientID should be set to a unique identifier representing the client.
ClientID string
// Hostname represents the hostname of the client, by default it is set to
// the value returned by os.Hostname is used.
Hostname string
// UserAgent represents the type of the client, by default it is set to
// nsq.DefaultUserAgent.
UserAgent string
// MessageTimeout can bet set to configure the server-side message timeout
// for messages delivered to this consumer. By default it is not sent to
// the server.
MessageTimeout time.Duration
}
type identifyBody struct {
ClientID string `json:"client_id,omitempty"`
Hostname string `json:"hostname,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
MessageTimeout int `json:"msg_timeout,omitempty"`
}
// Name returns the name of the command in order to satisfy the Command
// interface.
func (c Identify) Name() string {
return "IDENTIFY"
}
// Write serializes the command to the given buffered output, satisfies the
// Command interface.
func (c Identify) Write(w *bufio.Writer) (err error) {
var data []byte
if data, err = json.Marshal(identifyBody{
ClientID: c.ClientID,
Hostname: c.Hostname,
UserAgent: c.UserAgent,
MessageTimeout: int(c.MessageTimeout / time.Millisecond),
}); err != nil {
return
}
if _, err = w.WriteString("IDENTIFY\n"); err != nil {
err = errors.Wrap(err, "writing IDENTIFY command")
return
}
if err = binary.Write(w, binary.BigEndian, uint32(len(data))); err != nil {
err = errors.Wrap(err, "writing IDENTIFY body size")
return
}
if _, err = w.Write(data); err != nil {
err = errors.Wrap(err, "writing IDENTIFY body data")
return
}
return
}
func readIdentify(r *bufio.Reader) (cmd Identify, err error) {
var body identifyBody
if body, err = readIdentifyBody(r); err != nil {
return
}
cmd = Identify{
ClientID: body.ClientID,
Hostname: body.Hostname,
UserAgent: body.UserAgent,
MessageTimeout: time.Millisecond * time.Duration(body.MessageTimeout),
}
return
}
func readIdentifyBody(r *bufio.Reader) (body identifyBody, err error) {
var size uint32
var data []byte
if err = binary.Read(r, binary.BigEndian, &size); err != nil {
err = errors.Wrap(err, "reading IDENTIFY body size")
return
}
data = make([]byte, int(size))
if _, err = io.ReadFull(r, data); err != nil {
err = errors.Wrap(err, "reading IDENTIFY body data")
return
}
if err = json.Unmarshal(data, &body); err != nil {
err = errors.Wrap(err, "decoding IDENTIFY body")
return
}
return
}
func setIdentifyDefaults(c Identify) Identify {
if len(c.UserAgent) == 0 {
c.UserAgent = DefaultUserAgent
}
if len(c.Hostname) == 0 {
c.Hostname, _ = os.Hostname()
}
return c
}