-
Notifications
You must be signed in to change notification settings - Fork 0
/
redismq.go
242 lines (201 loc) · 4.92 KB
/
redismq.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
package queue
import (
"context"
"encoding/json"
"errors"
"fmt"
"relation-service/model/dao/cache/redis"
"time"
"github.com/longpi1/gopkg/libary/log"
redis2 "github.com/go-redis/redis"
)
type RedisMq struct {
poolName string
groupName string
timeout int64
}
type RedisOption struct {
Timeout int64
}
// SendMsg 按字符串类型生产数据
func (r *RedisMq) SendMsg(topic string, body string) (mqMsg Msg, err error) {
return r.SendByteMsg(topic, []byte(body))
}
// SendByteMsg 生产数据
func (r *RedisMq) SendByteMsg(topic string, body []byte) (mqMsg Msg, err error) {
if r.poolName == "" {
return mqMsg, fmt.Errorf("RedisMq producer not register")
}
if topic == "" {
return mqMsg, fmt.Errorf("RedisMq topic is empty")
}
mqMsg = Msg{
RunType: SendMsg,
Topic: topic,
MsgId: getRandMsgId(),
Body: body,
Timestamp: time.Now(),
}
data, err := json.Marshal(mqMsg)
if err != nil {
return
}
key := r.genKey(r.groupName, topic)
if err = redis.LPush(key, data); err != nil {
return
}
if r.timeout > 0 {
if err = redis.Expire(key, time.Duration(r.timeout)); err != nil {
return
}
}
return
}
func (r *RedisMq) SendDelayMsg(topic string, body string, delaySecond int64) (msg Msg, err error) {
if delaySecond < 1 {
return r.SendMsg(topic, body)
}
if r.poolName == "" {
err = fmt.Errorf("SendDelayMsg RedisMq not register")
return
}
if topic == "" {
err = fmt.Errorf("SendDelayMsg RedisMq topic is empty")
return
}
msg = Msg{
RunType: SendMsg,
Topic: topic,
MsgId: getRandMsgId(),
Body: []byte(body),
Timestamp: time.Now(),
}
data, err := json.Marshal(msg)
if err != nil {
return
}
var (
key = r.genKey(r.groupName, "delay:"+topic)
expireSecond = time.Now().Unix() + delaySecond
timePiece = fmt.Sprintf("%s:%d", key, expireSecond)
z = redis2.Z{Score: float64(expireSecond), Member: timePiece}
)
if err = redis.ZAdd(key, z); err != nil {
return
}
if err = redis.RPush(timePiece, data); err != nil {
return
}
// consumer will also delete the item
if r.timeout > 0 {
_ = redis.Expire(timePiece, time.Duration(r.timeout+delaySecond))
_ = redis.Expire(key, time.Duration(r.timeout))
}
return
}
// ListenReceiveMsgDo 消费数据
func (r *RedisMq) ListenReceiveMsgDo(topic string, receiveDo func(mqMsg Msg)) (err error) {
if r.poolName == "" {
return fmt.Errorf("RedisMq producer not register")
}
if topic == "" {
return fmt.Errorf("RedisMq topic is empty")
}
var (
key = r.genKey(r.groupName, topic)
key2 = r.genKey(r.groupName, "delay:"+topic)
)
go func() {
for range time.Tick(300 * time.Millisecond) {
mqMsgList := r.loopReadQueue(key)
for _, mqMsg := range mqMsgList {
receiveDo(mqMsg)
}
}
}()
go func() {
mqMsgCh, errCh := r.loopReadDelayQueue(key2)
for mqMsg := range mqMsgCh {
receiveDo(mqMsg)
}
for err = range errCh {
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
log.Error("ListenReceiveMsgDo Delay topic:%v, err:%+v", topic, err)
}
}
}()
select {}
}
// 生成队列key
func (r *RedisMq) genKey(groupName string, topic string) string {
return fmt.Sprintf("comment:%s_%s", groupName, topic)
}
func (r *RedisMq) loopReadQueue(key string) (msgList []Msg) {
for {
data, err := redis.Prop(key)
if err != nil {
log.Error("loopReadQueue redis RPOP err:%+v", err)
break
}
var msg Msg
if err = json.Unmarshal([]byte(data), &msg); err != nil {
log.Error("loopReadQueue Scan err:%+v", err)
break
}
if msg.MsgId != "" {
msgList = append(msgList, msg)
}
}
return msgList
}
func RegisterRedisMqProducer(connOpt RedisOption, groupName string) (client Producer) {
return RegisterRedisMq(connOpt, groupName)
}
// RegisterRedisMqConsumer 注册消费者
func RegisterRedisMqConsumer(connOpt RedisOption, groupName string) (client Consumer) {
return RegisterRedisMq(connOpt, groupName)
}
// RegisterRedisMq 注册redis实例
func RegisterRedisMq(connOpt RedisOption, groupName string) *RedisMq {
return &RedisMq{
poolName: fmt.Sprintf("%s-%d", groupName, time.Now().UnixNano()),
groupName: groupName,
timeout: connOpt.Timeout,
}
}
func (r *RedisMq) loopReadDelayQueue(key string) (resCh chan Msg, errCh chan error) {
resCh = make(chan Msg)
errCh = make(chan error, 1)
go func() {
defer close(resCh)
defer close(errCh)
for {
val, err := redis.ZRangeByScore(key, redis2.ZRangeBy{Offset: 0})
if err != nil {
return
}
for _, listK := range val {
for {
pop, err := redis.LPop(listK)
if err != nil {
errCh <- err
return
} else {
var msg Msg
if err = json.Unmarshal([]byte(pop), &msg); err != nil {
log.Error("loopReadDelayQueue Scan err:%+v", err)
break
}
if msg.MsgId == "" {
continue
}
select {
case resCh <- msg:
}
}
}
}
}
}()
return resCh, errCh
}