-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsq_pro.go
316 lines (293 loc) · 7.63 KB
/
nsq_pro.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package mq
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/nsqio/go-nsq"
"github.com/zhimaAi/go_tools/logs"
)
const nsqDefaultHost = `127.0.0.1`
const producerDefaultPort = 4150
const producerDefaultWorkNum = 1
const producerDefaultJobsLen = 1000
const consumerDefaultPort = 4161
type job struct {
topic string
message string
delay time.Duration
}
type ProducerHandle struct {
addr string
host string
port uint
workNum uint
workWg *sync.WaitGroup
opLock *sync.Mutex
running bool
jobs chan *job
isStop bool
}
func NewProducerHandle() *ProducerHandle {
return &ProducerHandle{
addr: ``,
host: nsqDefaultHost,
port: producerDefaultPort,
workNum: producerDefaultWorkNum,
workWg: &sync.WaitGroup{},
opLock: &sync.Mutex{},
running: false,
jobs: make(chan *job, producerDefaultJobsLen),
isStop: false,
}
}
func (handle *ProducerHandle) SetAddr(addr string) *ProducerHandle {
if !handle.running && len(strings.TrimSpace(addr)) > 0 {
handle.addr = strings.TrimSpace(addr)
}
return handle
}
func (handle *ProducerHandle) SetHostAndPort(host string, port uint) *ProducerHandle {
if !handle.running && len(strings.TrimSpace(host)) > 0 {
handle.host = strings.TrimSpace(host)
}
if !handle.running && port > 0 && port < 65536 {
handle.port = port
}
return handle
}
func (handle *ProducerHandle) SetWorkNum(workNum uint) *ProducerHandle {
if !handle.running && workNum > 0 {
handle.workNum = workNum
}
return handle
}
func (handle *ProducerHandle) getConn() (*nsq.Producer, error) {
if len(handle.addr) == 0 {
handle.addr = fmt.Sprintf(`%s:%d`, handle.host, handle.port)
}
conn, err := nsq.NewProducer(handle.addr, nsq.NewConfig())
if err != nil {
return nil, err
}
conn.SetLoggerLevel(nsq.LogLevelError)
if err = conn.Ping(); err != nil {
return nil, err
}
return conn, nil
}
func (handle *ProducerHandle) publish(conn *nsq.Producer, data *job) error {
if data.delay > 0 {
delay := min(data.delay, time.Hour) //nsq延时时间0-3600,000ms
return conn.DeferredPublish(data.topic, delay, []byte(data.message))
}
return conn.Publish(data.topic, []byte(data.message))
}
func (handle *ProducerHandle) autoRun() {
handle.opLock.Lock()
defer handle.opLock.Unlock()
if !handle.running {
handle.running = true
for i := 0; i < int(handle.workNum); i++ {
handle.workWg.Add(1)
go doWork(handle)
}
}
}
func doWork(handle *ProducerHandle) {
defer handle.workWg.Done()
var conn *nsq.Producer
var err error
defer func(conn *nsq.Producer) {
if conn != nil {
conn.Stop()
}
}(conn)
for data := range handle.jobs {
//获取conn
for conn == nil {
if handle.isStop {
_ = handle.AddJobs(data.topic, data.message, data.delay) //重入jobs
return //直接退出
}
conn, err = handle.getConn()
if err != nil {
logs.Error(`producer conn error:%s`, err.Error())
time.Sleep(time.Second)
}
}
//生产data
if err = handle.publish(conn, data); err != nil {
logs.Error(`producer publish error:%s`, err.Error())
//重试一次
if err = handle.publish(conn, data); err != nil {
logs.Error(`producer retry publish error:%s`, err.Error())
_ = handle.AddJobs(data.topic, data.message, data.delay) //重入jobs
conn.Stop() //关闭连接
conn = nil //清理连接
}
}
}
}
func (handle *ProducerHandle) AddJobs(topic, message string, delay ...time.Duration) error {
if handle.isStop {
return errors.New(`已退出,禁止生产数据`)
}
if !handle.running {
handle.autoRun() //自动启动work
}
topic = strings.TrimSpace(topic)
if len(topic) == 0 || len(message) == 0 {
return errors.New(`topic与message不能为空`)
}
data := &job{topic: topic, message: message, delay: 0}
if len(delay) > 0 && delay[0] > 0 {
data.delay = delay[0]
}
defer func() {
if r := recover(); r != nil {
logs.Other(`recover`, `topic:%s,message:%s,delay:%v,recover:%#v`,
data.topic, data.message, data.delay, r)
}
}()
handle.jobs <- data
return nil
}
func (handle *ProducerHandle) Stop() {
handle.opLock.Lock()
defer handle.opLock.Unlock()
if handle.isStop {
return
}
logs.Info(`正在退出...`)
handle.isStop = true
time.Sleep(time.Second)
close(handle.jobs)
logs.Info(`jobs已关闭...`)
go func(handle *ProducerHandle) {
for len(handle.jobs) > 0 {
logs.Info(`(%d)休眠中...`, len(handle.jobs))
time.Sleep(time.Second * 1)
}
}(handle)
handle.workWg.Wait()
logs.Info(`jobs任务量:%d`, len(handle.jobs))
for data := range handle.jobs {
logs.Warning(`未生产的数据:topic:%s,message:%s,delay:%v`, data.topic, data.message, data.delay)
}
logs.Info(`退出已完成`)
}
type ConsumerHandle struct {
addr string
host string
port uint
workWg *sync.WaitGroup
opLock *sync.Mutex
stopLock *sync.Mutex
run int
conns []*nsq.Consumer
isStop bool
}
func NewConsumerHandle() *ConsumerHandle {
return &ConsumerHandle{
addr: ``,
host: nsqDefaultHost,
port: consumerDefaultPort,
workWg: &sync.WaitGroup{},
opLock: &sync.Mutex{},
stopLock: &sync.Mutex{},
run: 0,
conns: make([]*nsq.Consumer, 0),
isStop: false,
}
}
func (handle *ConsumerHandle) SetAddr(addr string) *ConsumerHandle {
if len(strings.TrimSpace(addr)) > 0 {
handle.addr = strings.TrimSpace(addr)
}
return handle
}
func (handle *ConsumerHandle) SetHostAndPort(host string, port uint) *ConsumerHandle {
if len(strings.TrimSpace(host)) > 0 {
handle.host = strings.TrimSpace(host)
}
if port > 0 && port < 65536 {
handle.port = port
}
return handle
}
func (handle *ConsumerHandle) setRun(diff int) {
handle.opLock.Lock()
defer handle.opLock.Unlock()
handle.run += diff
}
func (handle *ConsumerHandle) PushZero(addr, topic string) error {
conn, err := NewProducer(PConfig{Server: addr, Topic: topic})
if err != nil {
return err
}
defer conn.Stop()
return conn.Publish(`0`)
}
func (handle *ConsumerHandle) Run(topic string, channel string, workNum uint, callback func(msg string, args ...string) error) error {
handle.opLock.Lock()
defer handle.opLock.Unlock()
if handle.isStop {
return errors.New(`已退出,禁止创建消费者`)
}
topic = strings.TrimSpace(topic)
channel = strings.TrimSpace(channel)
workNum = max(1, workNum)
if len(topic) == 0 || len(channel) == 0 || callback == nil {
return errors.New(`topic,channel,callback不能为空`)
}
for i := 0; i < int(workNum); i++ {
conn, err := nsq.NewConsumer(topic, channel, nsq.NewConfig())
if err != nil {
return err
}
conn.SetLoggerLevel(nsq.LogLevelError)
conn.AddHandler(&Handle{handle: func(msg string, args ...string) error {
if msg == `0` {
return nil
}
handle.workWg.Add(1)
defer handle.workWg.Done()
handle.setRun(1)
defer handle.setRun(-1)
return callback(msg, args...)
}, topic: topic, channel: channel})
if len(handle.addr) == 0 {
handle.addr = fmt.Sprintf(`%s:%d`, handle.host, handle.port)
}
err = conn.ConnectToNSQLookupd(handle.addr)
if err != nil {
return err
}
handle.conns = append(handle.conns, conn)
}
return nil
}
func (handle *ConsumerHandle) Stop() {
handle.stopLock.Lock()
defer handle.stopLock.Unlock()
if handle.isStop {
return
}
logs.Info(`正在退出...`)
handle.isStop = true
for i := range handle.conns {
handle.conns[i].Stop()
}
logs.Info(`conns已关闭...`)
go func(handle *ConsumerHandle) {
for handle.run > 0 {
logs.Info(`(%d)休眠中...`, handle.run)
time.Sleep(time.Second * 1)
}
}(handle)
handle.workWg.Wait()
logs.Info(`run任务数:%d`, handle.run)
logs.Info(`退出已完成`)
}