-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnsq.go
165 lines (139 loc) · 3.64 KB
/
nsq.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package nsq
import (
"bytes"
"io"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type Client struct {
http.Client
Address string
Scheme string
UserAgent string
}
func (c *Client) Ping() error {
return c.call("GET", "/ping", nil)
}
func (c *Client) Publish(topic string, message []byte) (err error) {
_, err = c.do("POST", "/pub", url.Values{
"topic": []string{topic},
}, message)
return
}
func (c *Client) MutliPublish(topic string, messages ...[]byte) (err error) {
_, err = c.do("POST", "/mpub", url.Values{
"topic": []string{topic},
}, bytes.Join(messages, []byte("\n")))
return
}
func (c *Client) CreateTopic(topic string) error {
return c.call("POST", "/topic/create", url.Values{
"topic": []string{topic},
})
}
func (c *Client) DeleteTopic(topic string) error {
return c.call("POST", "/topic/delete", url.Values{
"topic": []string{topic},
})
}
func (c *Client) EmptyTopic(topic string) error {
return c.call("POST", "/topic/empty", url.Values{
"topic": []string{topic},
})
}
func (c *Client) PauseTopic(topic string) error {
return c.call("POST", "/topic/pause", url.Values{
"topic": []string{topic},
})
}
func (c *Client) UnpauseTopic(topic string) error {
return c.call("POST", "/topic/unpause", url.Values{
"topic": []string{topic},
})
}
func (c *Client) CreateChannel(topic string, channel string) error {
return c.call("POST", "/channel/create", url.Values{
"topic": []string{topic},
"channel": []string{channel},
})
}
func (c *Client) DeleteChannel(topic string, channel string) error {
return c.call("POST", "/channel/delete", url.Values{
"topic": []string{topic},
"channel": []string{channel},
})
}
func (c *Client) EmptyChannel(topic string, channel string) error {
return c.call("POST", "/channel/empty", url.Values{
"topic": []string{topic},
"channel": []string{channel},
})
}
func (c *Client) PauseChannel(topic string, channel string) error {
return c.call("POST", "/channel/pause", url.Values{
"topic": []string{topic},
"channel": []string{channel},
})
}
func (c *Client) UnpauseChannel(topic string, channel string) error {
return c.call("POST", "/channel/unpause", url.Values{
"topic": []string{topic},
"channel": []string{channel},
})
}
func (c *Client) call(method string, path string, query url.Values) (err error) {
_, err = c.do(method, path, query, nil)
return
}
func (c *Client) do(method string, path string, query url.Values, data []byte) (ret []byte, err error) {
var res *http.Response
var host = c.Address
var scheme = c.Scheme
var userAgent = c.UserAgent
var body io.ReadCloser
if len(host) == 0 {
host = "localhost:4151"
}
if len(scheme) == 0 {
scheme = "http"
}
if len(userAgent) == 0 {
userAgent = DefaultUserAgent
}
if len(data) != 0 {
body = io.NopCloser(bytes.NewReader(data))
}
if res, err = c.Do(&http.Request{
Method: method,
URL: &url.URL{
Scheme: scheme,
Host: host,
Path: path,
RawQuery: query.Encode(),
},
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Content-Type": []string{"application/octet-stream"},
"User-Agent": []string{userAgent},
},
Body: body,
Host: host,
ContentLength: int64(len(data)),
}); err != nil {
err = errors.Wrapf(err, "%s %s://%s?%s", method, scheme, host, query.Encode())
return
}
if res.StatusCode != http.StatusOK {
res.Body.Close()
err = errors.Errorf("%s %s://%s?%s: %d %s", method, scheme, host, query.Encode(), res.StatusCode, res.Status)
return
}
if ret, err = io.ReadAll(res.Body); err != nil {
err = errors.Wrapf(err, "%s %s://%s?%s", method, scheme, host, query.Encode())
return
}
return
}