mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-08 02:03:44 +08:00
发布v2.3.5版本,本次为优化版本。更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
73
server/internal/library/queue/consumer.go
Normal file
73
server/internal/library/queue/consumer.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// consumerStrategy 消费者策略,实现该接口即可加入到消费队列中
|
||||
type consumerStrategy interface {
|
||||
GetTopic() string // 获取消费主题
|
||||
Handle(ctx context.Context, mqMsg MqMsg) (err error) // 处理消息
|
||||
}
|
||||
|
||||
// consumerManager 消费者管理
|
||||
type consumerManager struct {
|
||||
sync.Mutex
|
||||
list map[string]consumerStrategy // 维护的消费者列表
|
||||
}
|
||||
|
||||
var consumers = &consumerManager{
|
||||
list: make(map[string]consumerStrategy),
|
||||
}
|
||||
|
||||
// RegisterConsumer 注册任务到消费者队列
|
||||
func RegisterConsumer(cs consumerStrategy) {
|
||||
consumers.Lock()
|
||||
defer consumers.Unlock()
|
||||
topic := cs.GetTopic()
|
||||
if _, ok := consumers.list[topic]; ok {
|
||||
g.Log().Debugf(ctx, "queue.RegisterConsumer topic:%v duplicate registration.", topic)
|
||||
return
|
||||
}
|
||||
consumers.list[topic] = cs
|
||||
}
|
||||
|
||||
// StartConsumersListener 启动所有已注册的消费者监听
|
||||
func StartConsumersListener(ctx context.Context) {
|
||||
for _, consumer := range consumers.list {
|
||||
go func(consumer consumerStrategy) {
|
||||
consumerListen(ctx, consumer)
|
||||
}(consumer)
|
||||
}
|
||||
}
|
||||
|
||||
// consumerListen 消费者监听
|
||||
func consumerListen(ctx context.Context, job consumerStrategy) {
|
||||
var (
|
||||
topic = job.GetTopic()
|
||||
consumer, err = InstanceConsumer()
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
g.Log().Fatalf(ctx, "InstanceConsumer %s err:%+v", topic, err)
|
||||
return
|
||||
}
|
||||
|
||||
if listenErr := consumer.ListenReceiveMsgDo(topic, func(mqMsg MqMsg) {
|
||||
err = job.Handle(ctx, mqMsg)
|
||||
|
||||
if err != nil {
|
||||
// 遇到错误,重新加入到队列
|
||||
//queue.Push(topic, mqMsg.Body)
|
||||
}
|
||||
|
||||
// 记录消费队列日志
|
||||
ConsumerLog(ctx, topic, mqMsg, err)
|
||||
|
||||
}); listenErr != nil {
|
||||
g.Log().Fatalf(ctx, "消费队列:%s 监听失败, err:%+v", topic, listenErr)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Disk 磁盘队列
|
||||
|
||||
type DiskProducerMq struct {
|
||||
config *disk.Config
|
||||
producers map[string]*disk.Queue
|
||||
|
||||
Reference in New Issue
Block a user