mirror of
https://github.com/HDT3213/delayqueue.git
synced 2025-10-05 23:26:49 +08:00
delay queue
This commit is contained in:
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
.idea
|
||||||
|
unack2retry.lua
|
80
README.md
80
README.md
@@ -1,2 +1,78 @@
|
|||||||
# delayqueue
|
# DelayQueue
|
||||||
golang delay message queue based on redis
|
|
||||||
|
DelayQueue is a message queue supporting delayed/scheduled delivery based on redis.
|
||||||
|
|
||||||
|
DelayQueue guarantees to deliver at least once.
|
||||||
|
|
||||||
|
DelayQueue support ACK/Retry mechanism, it will re-deliver message after a while as long as no confirmation is received.
|
||||||
|
As long as Redis doesn't crash, consumer crashes won't cause message loss.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"github.com/hdt3213/delayqueue"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
redisCli := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1:6379",
|
||||||
|
})
|
||||||
|
queue := delayqueue.NewQueue("example", redisCli, func(payload string) bool {
|
||||||
|
// callback returns true to confirm successful consumption.
|
||||||
|
// If callback returns false or not return within maxConsumeDuration, DelayQueue will re-deliver this message
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
// send delay message
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
err := queue.SendDelayMsg(strconv.Itoa(i), time.Hour, delayqueue.WithRetryCount(3))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// send schedule message
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
err := queue.SendScheduleMsg(strconv.Itoa(i), time.Now().Add(time.Hour))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// start consume
|
||||||
|
done := queue.StartConsume()
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## options
|
||||||
|
|
||||||
|
```
|
||||||
|
WithLogger(logger *log.Logger)
|
||||||
|
```
|
||||||
|
|
||||||
|
WithLogger customizes logger for queue
|
||||||
|
|
||||||
|
```
|
||||||
|
WithFetchInterval(d time.Duration)
|
||||||
|
```
|
||||||
|
|
||||||
|
WithFetchInterval customizes the interval at which consumer fetch message from redis
|
||||||
|
|
||||||
|
```
|
||||||
|
WithMaxConsumeDuration(d time.Duration)
|
||||||
|
```
|
||||||
|
|
||||||
|
WithMaxConsumeDuration customizes max consume duration
|
||||||
|
|
||||||
|
If no acknowledge received within WithMaxConsumeDuration after message delivery, DelayQueue will try to deliver this
|
||||||
|
message again
|
||||||
|
|
||||||
|
```
|
||||||
|
WithFetchLimit(limit uint)
|
||||||
|
```
|
||||||
|
|
||||||
|
WithFetchLimit limits the max number of messages at one time
|
||||||
|
416
delayqueue.go
Normal file
416
delayqueue.go
Normal file
@@ -0,0 +1,416 @@
|
|||||||
|
package delayqueue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DelayQueue is a message queue supporting delayed/scheduled delivery based on redis
|
||||||
|
type DelayQueue struct {
|
||||||
|
// name for this Queue. Make sure the name is unique in redis database
|
||||||
|
name string
|
||||||
|
redisCli *redis.Client
|
||||||
|
cb func(string) bool
|
||||||
|
pendingKey string // sorted set: message id -> delivery time
|
||||||
|
readyKey string // list
|
||||||
|
unAckKey string // sorted set: message id -> retry time
|
||||||
|
retryKey string // list
|
||||||
|
retryCountKey string // hash: message id -> remain retry count
|
||||||
|
garbageKey string // set: message id
|
||||||
|
idGenKey string // id generator
|
||||||
|
ticker *time.Ticker
|
||||||
|
logger *log.Logger
|
||||||
|
close chan struct{}
|
||||||
|
|
||||||
|
maxConsumeDuration time.Duration
|
||||||
|
msgTTL time.Duration
|
||||||
|
defaultRetryCount int
|
||||||
|
fetchInterval time.Duration
|
||||||
|
fetchLimit uint
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewQueue creates a new queue, use DelayQueue.StartConsume to consume or DelayQueue.SendScheduleMsg to publish message
|
||||||
|
// callback returns true to confirm successful consumption. If callback returns false or not return within maxConsumeDuration, DelayQueue will re-deliver this message
|
||||||
|
func NewQueue(name string, cli *redis.Client, callback func(string) bool) *DelayQueue {
|
||||||
|
if name == "" {
|
||||||
|
panic("name is required")
|
||||||
|
}
|
||||||
|
if cli == nil {
|
||||||
|
panic("cli is required")
|
||||||
|
}
|
||||||
|
if callback == nil {
|
||||||
|
panic("callback is required")
|
||||||
|
}
|
||||||
|
return &DelayQueue{
|
||||||
|
name: name,
|
||||||
|
redisCli: cli,
|
||||||
|
cb: callback,
|
||||||
|
pendingKey: "dp:" + name + ":pending",
|
||||||
|
readyKey: "dp:" + name + ":ready",
|
||||||
|
unAckKey: "dp:" + name + ":unack",
|
||||||
|
retryKey: "dp:" + name + ":retry",
|
||||||
|
retryCountKey: "dp:" + name + ":retry:cnt",
|
||||||
|
garbageKey: "dp:" + name + ":garbage",
|
||||||
|
idGenKey: "dp:" + name + ":id_gen",
|
||||||
|
close: make(chan struct{}, 1),
|
||||||
|
maxConsumeDuration: 5 * time.Second,
|
||||||
|
msgTTL: time.Hour,
|
||||||
|
logger: log.Default(),
|
||||||
|
defaultRetryCount: 3,
|
||||||
|
fetchInterval: time.Second,
|
||||||
|
fetchLimit: math.MaxInt32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithLogger customizes logger for queue
|
||||||
|
func (q *DelayQueue) WithLogger(logger *log.Logger) *DelayQueue {
|
||||||
|
q.logger = logger
|
||||||
|
return q
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFetchInterval customizes the interval at which consumer fetch message from redis
|
||||||
|
func (q *DelayQueue) WithFetchInterval(d time.Duration) *DelayQueue {
|
||||||
|
q.fetchInterval = d
|
||||||
|
return q
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMaxConsumeDuration customizes max consume duration
|
||||||
|
// If no acknowledge received within WithMaxConsumeDuration after message delivery, DelayQueue will try to deliver this message again
|
||||||
|
func (q *DelayQueue) WithMaxConsumeDuration(d time.Duration) *DelayQueue {
|
||||||
|
q.maxConsumeDuration = d
|
||||||
|
return q
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFetchLimit limits the max number of messages at one time
|
||||||
|
func (q *DelayQueue) WithFetchLimit(limit uint) *DelayQueue {
|
||||||
|
q.fetchLimit = limit
|
||||||
|
return q
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) genMsgKey(idStr string) string {
|
||||||
|
return "dp:" + q.name + ":msg:" + idStr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) genId() (uint32, error) {
|
||||||
|
ctx := context.Background()
|
||||||
|
id, err := q.redisCli.Incr(ctx, q.idGenKey).Result()
|
||||||
|
if err != nil && err.Error() == "ERR increment or decrement would overflow" {
|
||||||
|
err = q.redisCli.Set(ctx, q.idGenKey, 1, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("reset id gen failed: %v", err)
|
||||||
|
}
|
||||||
|
return 1, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("incr id gen failed: %v", err)
|
||||||
|
}
|
||||||
|
return uint32(id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type retryCountOpt int
|
||||||
|
|
||||||
|
// WithRetryCount set retry count for a msg
|
||||||
|
// example: queue.SendDelayMsg(payload, duration, delayqueue.WithRetryCount(3))
|
||||||
|
func WithRetryCount(count int) interface{} {
|
||||||
|
return retryCountOpt(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendScheduleMsg submits a message delivered at given time
|
||||||
|
func (q *DelayQueue) SendScheduleMsg(payload string, t time.Time, opts ...interface{}) error {
|
||||||
|
// parse options
|
||||||
|
retryCount := q.defaultRetryCount
|
||||||
|
for _, opt := range opts {
|
||||||
|
switch o := opt.(type) {
|
||||||
|
case retryCountOpt:
|
||||||
|
retryCount = int(o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// generate id
|
||||||
|
id, err := q.genId()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
idStr := strconv.FormatUint(uint64(id), 10)
|
||||||
|
ctx := context.Background()
|
||||||
|
now := time.Now()
|
||||||
|
// store msg
|
||||||
|
msgTTL := t.Sub(now) + q.msgTTL // delivery + q.msgTTL
|
||||||
|
err = q.redisCli.Set(ctx, q.genMsgKey(idStr), payload, msgTTL).Err()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("store msg failed: %v", err)
|
||||||
|
}
|
||||||
|
// store retry count
|
||||||
|
err = q.redisCli.HSet(ctx, q.retryCountKey, idStr, retryCount).Err()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("store retry count failed: %v", err)
|
||||||
|
}
|
||||||
|
// put to pending
|
||||||
|
err = q.redisCli.ZAdd(ctx, q.pendingKey, &redis.Z{Score: float64(t.Unix()), Member: id}).Err()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("push to pending failed: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendDelayMsg submits a message delivered after given duration
|
||||||
|
func (q *DelayQueue) SendDelayMsg(payload string, duration time.Duration, opts ...interface{}) error {
|
||||||
|
t := time.Now().Add(duration)
|
||||||
|
return q.SendScheduleMsg(payload, t, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pending2ReadyScript atomically moves messages from pending to ready
|
||||||
|
// argv: currentTime, pendingKey, readyKey
|
||||||
|
const pending2ReadyScript = `
|
||||||
|
local msgs = redis.call('ZRangeByScore', ARGV[2], '0', ARGV[1]) -- get ready msg
|
||||||
|
if (#msgs == 0) then return end
|
||||||
|
local args2 = {'LPush', ARGV[3]} -- push into ready
|
||||||
|
for _,v in ipairs(msgs) do
|
||||||
|
table.insert(args2, v)
|
||||||
|
end
|
||||||
|
redis.call(unpack(args2))
|
||||||
|
redis.call('ZRemRangeByScore', ARGV[2], '0', ARGV[1]) -- remove msgs from pending
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *DelayQueue) pending2Ready() error {
|
||||||
|
now := time.Now().Unix()
|
||||||
|
ctx := context.Background()
|
||||||
|
keys := []string{q.pendingKey, q.readyKey}
|
||||||
|
err := q.redisCli.Eval(ctx, pending2ReadyScript, keys, now, q.pendingKey, q.readyKey).Err()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return fmt.Errorf("pending2ReadyScript failed: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// pending2ReadyScript atomically moves messages from ready to unack
|
||||||
|
// argv: retryTime, readyKey/retryKey, unackKey
|
||||||
|
const ready2UnackScript = `
|
||||||
|
local msg = redis.call('RPop', ARGV[2])
|
||||||
|
if (not msg) then return end
|
||||||
|
redis.call('ZAdd', ARGV[3], ARGV[1], msg)
|
||||||
|
return msg
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *DelayQueue) ready2Unack() (string, error) {
|
||||||
|
retryTime := time.Now().Add(q.maxConsumeDuration).Unix()
|
||||||
|
ctx := context.Background()
|
||||||
|
keys := []string{q.readyKey, q.unAckKey}
|
||||||
|
ret, err := q.redisCli.Eval(ctx, ready2UnackScript, keys, retryTime, q.readyKey, q.unAckKey).Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("ready2UnackScript failed: %v", err)
|
||||||
|
}
|
||||||
|
str, ok := ret.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("illegal result: %#v", ret)
|
||||||
|
}
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) retry2Unack() (string, error) {
|
||||||
|
retryTime := time.Now().Add(q.maxConsumeDuration).Unix()
|
||||||
|
ctx := context.Background()
|
||||||
|
keys := []string{q.retryKey, q.unAckKey}
|
||||||
|
ret, err := q.redisCli.Eval(ctx, ready2UnackScript, keys, retryTime, q.retryKey, q.unAckKey).Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
return "", redis.Nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("ready2UnackScript failed: %v", err)
|
||||||
|
}
|
||||||
|
str, ok := ret.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("illegal result: %#v", ret)
|
||||||
|
}
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) callback(idStr string) (bool, error) {
|
||||||
|
ctx := context.Background()
|
||||||
|
payload, err := q.redisCli.Get(ctx, q.genMsgKey(idStr)).Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// Is an IO error?
|
||||||
|
return false, fmt.Errorf("get message payload failed: %v", err)
|
||||||
|
}
|
||||||
|
return q.cb(payload), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) ack(idStr string) error {
|
||||||
|
ctx := context.Background()
|
||||||
|
err := q.redisCli.ZRem(ctx, q.unAckKey, idStr).Err()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("remove from unack failed: %v", err)
|
||||||
|
}
|
||||||
|
// msg key has ttl, ignore result of delete
|
||||||
|
_ = q.redisCli.Del(ctx, q.genMsgKey(idStr)).Err()
|
||||||
|
q.redisCli.HDel(ctx, q.retryCountKey, idStr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// unack2RetryScript atomically moves messages from unack to retry which remaining retry count greater than 0,
|
||||||
|
// and moves messages from unack to garbage which retry count is 0
|
||||||
|
// Because DelayQueue cannot determine garbage message before eval unack2RetryScript, so it cannot pass keys parameter to redisCli.Eval
|
||||||
|
// Therefore unack2RetryScript moves garbage message to garbageKey instead of deleting directly
|
||||||
|
// argv: currentTime, unackKey, retryCountKey, retryKey, garbageKey
|
||||||
|
const unack2RetryScript = `
|
||||||
|
local msgs = redis.call('ZRangeByScore', ARGV[2], '0', ARGV[1]) -- get retry msg
|
||||||
|
if (#msgs == 0) then return end
|
||||||
|
local retryCounts = redis.call('HMGet', ARGV[3], unpack(msgs)) -- get retry count
|
||||||
|
for i,v in ipairs(retryCounts) do
|
||||||
|
local k = msgs[i]
|
||||||
|
if tonumber(v) > 0 then
|
||||||
|
redis.call("HIncrBy", ARGV[3], k, -1) -- reduce retry count
|
||||||
|
redis.call("LPush", ARGV[4], k) -- add to retry
|
||||||
|
else
|
||||||
|
redis.call("HDel", ARGV[3], k) -- del retry count
|
||||||
|
redis.call("SAdd", ARGV[5], k) -- add to garbage
|
||||||
|
end
|
||||||
|
end
|
||||||
|
redis.call('ZRemRangeByScore', ARGV[2], '0', ARGV[1]) -- remove msgs from unack
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *DelayQueue) unack2Retry() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
keys := []string{q.unAckKey, q.retryKey, q.retryCountKey, q.garbageKey}
|
||||||
|
now := time.Now()
|
||||||
|
err := q.redisCli.Eval(ctx, unack2RetryScript, keys,
|
||||||
|
now.Unix(), q.unAckKey, q.retryCountKey, q.retryKey, q.garbageKey).Err()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return fmt.Errorf("unack to retry script failed: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) garbageCollect() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
msgIds, err := q.redisCli.SMembers(ctx, q.garbageKey).Result()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("smembers failed: %v", err)
|
||||||
|
}
|
||||||
|
if len(msgIds) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// allow concurrent clean
|
||||||
|
msgKeys := make([]string, 0, len(msgIds))
|
||||||
|
for _, idStr := range msgIds {
|
||||||
|
msgKeys = append(msgKeys, q.genMsgKey(idStr))
|
||||||
|
}
|
||||||
|
err = q.redisCli.Del(ctx, msgKeys...).Err()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return fmt.Errorf("del msgs failed: %v", err)
|
||||||
|
}
|
||||||
|
err = q.redisCli.SRem(ctx, q.garbageKey, msgIds).Err()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return fmt.Errorf("remove from garbage key failed: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *DelayQueue) consume() error {
|
||||||
|
// pending2ready
|
||||||
|
err := q.pending2Ready()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// consume
|
||||||
|
var fetchCount uint
|
||||||
|
for {
|
||||||
|
idStr, err := q.ready2Unack()
|
||||||
|
if err == redis.Nil { // consumed all
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fetchCount++
|
||||||
|
ack, err := q.callback(idStr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if ack {
|
||||||
|
err = q.ack(idStr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fetchCount >= q.fetchLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// unack to retry
|
||||||
|
err = q.unack2Retry()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = q.garbageCollect()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// retry
|
||||||
|
fetchCount = 0
|
||||||
|
for {
|
||||||
|
idStr, err := q.retry2Unack()
|
||||||
|
if err == redis.Nil { // consumed all
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fetchCount++
|
||||||
|
ack, err := q.callback(idStr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if ack {
|
||||||
|
err = q.ack(idStr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fetchCount >= q.fetchLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartConsume creates a goroutine to consume message from DelayQueue
|
||||||
|
// use `<-done` to wait consumer stopping
|
||||||
|
func (q *DelayQueue) StartConsume() (done <-chan struct{}) {
|
||||||
|
q.ticker = time.NewTicker(q.fetchInterval)
|
||||||
|
done0 := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
tickerLoop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-q.ticker.C:
|
||||||
|
err := q.consume()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("consume error: %v", err)
|
||||||
|
}
|
||||||
|
case <-q.close:
|
||||||
|
break tickerLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done0 <- struct{}{}
|
||||||
|
}()
|
||||||
|
return done0
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopConsume stops consumer goroutine
|
||||||
|
func (q *DelayQueue) StopConsume() {
|
||||||
|
q.close <- struct{}{}
|
||||||
|
if q.ticker != nil {
|
||||||
|
q.ticker.Stop()
|
||||||
|
}
|
||||||
|
}
|
108
delayqueue_test.go
Normal file
108
delayqueue_test.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package delayqueue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDelayQueue_consume(t *testing.T) {
|
||||||
|
redisCli := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1:6379",
|
||||||
|
})
|
||||||
|
redisCli.FlushDB(context.Background())
|
||||||
|
size := 10
|
||||||
|
retryCount := 3
|
||||||
|
deliveryCount := make(map[string]int)
|
||||||
|
cb := func(s string) bool {
|
||||||
|
deliveryCount[s]++
|
||||||
|
i, _ := strconv.ParseInt(s, 10, 64)
|
||||||
|
return i%2 == 0
|
||||||
|
}
|
||||||
|
queue := NewQueue("test", redisCli, cb).
|
||||||
|
WithFetchInterval(time.Millisecond * 50).
|
||||||
|
WithMaxConsumeDuration(0).
|
||||||
|
WithLogger(log.New(os.Stderr, "[DelayQueue]", log.LstdFlags)).
|
||||||
|
WithFetchLimit(1)
|
||||||
|
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
err := queue.SendDelayMsg(strconv.Itoa(i), 0, WithRetryCount(retryCount))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < 10*size; i++ {
|
||||||
|
err := queue.consume()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("consume error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, v := range deliveryCount {
|
||||||
|
i, _ := strconv.ParseInt(k, 10, 64)
|
||||||
|
if i%2 == 0 {
|
||||||
|
if v != 1 {
|
||||||
|
t.Errorf("expect 1 delivery, actual %d", v)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if v != retryCount+1 {
|
||||||
|
t.Errorf("expect %d delivery, actual %d", retryCount+1, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDelayQueue_StopConsume(t *testing.T) {
|
||||||
|
size := 10
|
||||||
|
redisCli := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1:6379",
|
||||||
|
})
|
||||||
|
redisCli.FlushDB(context.Background())
|
||||||
|
var queue *DelayQueue
|
||||||
|
var received int
|
||||||
|
queue = NewQueue("test", redisCli, func(s string) bool {
|
||||||
|
received++
|
||||||
|
if received == size {
|
||||||
|
queue.StopConsume()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
err := queue.SendDelayMsg(strconv.Itoa(i), 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("send message failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done := queue.StartConsume()
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIDOverflow(t *testing.T) {
|
||||||
|
redisCli := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1:6379",
|
||||||
|
})
|
||||||
|
redisCli.FlushDB(context.Background())
|
||||||
|
queue := NewQueue("test", redisCli, func(s string) bool {
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
err := redisCli.Set(context.Background(), queue.idGenKey, math.MaxInt64, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
_, err := queue.genId()
|
||||||
|
if err != nil {
|
||||||
|
t.Error("id gen error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDelayQUeu(t *testing.T) {
|
||||||
|
|
||||||
|
}
|
7
go.mod
Normal file
7
go.mod
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module github.com/hdt3213/delayqueue
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-redis/redis/v8 v8.11.4 // indirect
|
||||||
|
)
|
90
go.sum
Normal file
90
go.sum
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||||
|
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
|
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
|
||||||
|
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
||||||
|
github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg=
|
||||||
|
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
|
||||||
|
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
|
||||||
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||||
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
|
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||||
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
|
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||||
|
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||||
|
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
|
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
|
||||||
|
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
|
||||||
|
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
||||||
|
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
|
||||||
|
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
||||||
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||||
|
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||||
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
Reference in New Issue
Block a user