-
Notifications
You must be signed in to change notification settings - Fork 5
/
ack_list.go
48 lines (43 loc) · 1007 Bytes
/
ack_list.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
package rmq
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
"time"
)
type ACKListMQ struct {
client *redis.Client // Redis客户端
}
func NewACKListMQ(client *redis.Client) *ACKListMQ {
return &ACKListMQ{client: client}
}
func (q *ACKListMQ) SendMsg(ctx context.Context, msg *Msg) error {
return q.client.LPush(ctx, msg.Topic, msg.Body).Err()
}
// Consume 返回值代表消费过程中遇到的无法处理的错误
func (q *ACKListMQ) Consume(ctx context.Context, topic string, h Handler) error {
for {
// 获取消息
body, err := q.client.LIndex(ctx, topic, -1).Bytes()
if err != nil && !errors.Is(err, redis.Nil) {
return err
}
// 没有消息了,休眠一会
if errors.Is(err, redis.Nil) {
time.Sleep(time.Second)
continue
}
// 处理消息
err = h(&Msg{
Topic: topic,
Body: body,
})
if err != nil {
continue
}
// 如果处理成功,删除消息
if err := q.client.RPop(ctx, topic).Err(); err != nil {
return err
}
}
}