|
|
@@ -1,6 +1,7 @@
|
|
|
package redis
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/data_protocol"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
|
|
|
"git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/common"
|
|
|
@@ -8,6 +9,7 @@ import (
|
|
|
"github.com/robinjoseph08/redisqueue/v2"
|
|
|
"strings"
|
|
|
"sync"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
@@ -17,8 +19,10 @@ const (
|
|
|
type Option func(options *Options)
|
|
|
|
|
|
type Options struct {
|
|
|
- MaxLen int64
|
|
|
- ConsumerNum int
|
|
|
+ MaxLen int64
|
|
|
+ ConsumerNum int
|
|
|
+ VisibilityAgainSec int
|
|
|
+ InFlightCount int
|
|
|
}
|
|
|
|
|
|
func WithMaxLen(maxLen int64) Option {
|
|
|
@@ -33,6 +37,18 @@ func WithConsumerNum(consumerNum int) Option {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func WithVisibilityAgainSec(visibilityAgainSec int) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.VisibilityAgainSec = visibilityAgainSec
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithInFlightCount(inFlightCount int) Option {
|
|
|
+ return func(options *Options) {
|
|
|
+ options.InFlightCount = inFlightCount
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
type MessageQueue struct {
|
|
|
redisOptions *redisqueue.RedisOptions
|
|
|
|
|
|
@@ -103,10 +119,12 @@ func (messageQueue *MessageQueue) Subscribe(group string, topic string, handler
|
|
|
}
|
|
|
|
|
|
newConsumer, err := redisqueue.NewConsumerWithOptions(&redisqueue.ConsumerOptions{
|
|
|
- GroupName: group,
|
|
|
- Concurrency: messageQueue.options.ConsumerNum,
|
|
|
- RedisClient: nil,
|
|
|
- RedisOptions: messageQueue.redisOptions,
|
|
|
+ VisibilityTimeout: time.Duration(messageQueue.options.VisibilityAgainSec) * time.Second,
|
|
|
+ BufferSize: messageQueue.options.InFlightCount,
|
|
|
+ GroupName: group,
|
|
|
+ Concurrency: messageQueue.options.ConsumerNum,
|
|
|
+ RedisClient: nil,
|
|
|
+ RedisOptions: messageQueue.redisOptions,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
@@ -135,6 +153,12 @@ func (messageQueue *MessageQueue) Subscribe(group string, topic string, handler
|
|
|
return nil
|
|
|
})
|
|
|
|
|
|
+ go func() {
|
|
|
+ for err := range newConsumer.Errors {
|
|
|
+ fmt.Printf("err: %+v\n", err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
go newConsumer.Run()
|
|
|
|
|
|
messageQueue.consumerMap[groupTopic] = newConsumer
|