mirror of
https://github.com/HDT3213/delayqueue.git
synced 2025-09-29 12:32:21 +08:00
add UseHashTagKey
This commit is contained in:
11
README.md
11
README.md
@@ -98,6 +98,17 @@ WithFetchLimit(limit uint)
|
|||||||
|
|
||||||
WithFetchLimit limits the max number of unack (processing) messages
|
WithFetchLimit limits the max number of unack (processing) messages
|
||||||
|
|
||||||
|
```go
|
||||||
|
UseHashTagKey()
|
||||||
|
```
|
||||||
|
|
||||||
|
UseHashTagKey add hashtags to redis keys to ensure all keys of this queue are allocated in the same hash slot.
|
||||||
|
|
||||||
|
If you are using Codis/AliyunRedisCluster/TencentCloudRedisCluster, you should add this option to NewQueue: `NewQueue("test", redisCli, cb, UseHashTagKey())`. This Option cannot be changed after DelayQueue has been created.
|
||||||
|
|
||||||
|
WARNING! CHANGING(add or remove) this option will cause DelayQueue failing to read existed data in redis
|
||||||
|
|
||||||
|
> see more: https://redis.io/docs/reference/cluster-spec/#hash-tags
|
||||||
|
|
||||||
```go
|
```go
|
||||||
WithDefaultRetryCount(count uint)
|
WithDefaultRetryCount(count uint)
|
||||||
|
@@ -35,9 +35,19 @@ type DelayQueue struct {
|
|||||||
concurrent uint
|
concurrent uint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type hashTagKeyOpt int
|
||||||
|
|
||||||
|
// UseHashTagKey add hashtags to redis keys to ensure all keys of this queue are allocated in the same hash slot.
|
||||||
|
// If you are using Codis/AliyunRedisCluster/TencentCloudRedisCluster, add this option to NewQueue
|
||||||
|
// WARNING! Changing (add or remove) this option will cause DelayQueue failing to read existed data in redis
|
||||||
|
// see more: https://redis.io/docs/reference/cluster-spec/#hash-tags
|
||||||
|
func UseHashTagKey() interface{} {
|
||||||
|
return hashTagKeyOpt(1)
|
||||||
|
}
|
||||||
|
|
||||||
// NewQueue creates a new queue, use DelayQueue.StartConsume to consume or DelayQueue.SendScheduleMsg to publish message
|
// 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
|
// 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 {
|
func NewQueue(name string, cli *redis.Client, callback func(string) bool, opts ...interface{}) *DelayQueue {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
panic("name is required")
|
panic("name is required")
|
||||||
}
|
}
|
||||||
@@ -47,16 +57,29 @@ func NewQueue(name string, cli *redis.Client, callback func(string) bool) *Delay
|
|||||||
if callback == nil {
|
if callback == nil {
|
||||||
panic("callback is required")
|
panic("callback is required")
|
||||||
}
|
}
|
||||||
|
useHashTag := false
|
||||||
|
for _, opt := range opts {
|
||||||
|
switch opt.(type) {
|
||||||
|
case hashTagKeyOpt:
|
||||||
|
useHashTag = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var keyPrefix string
|
||||||
|
if useHashTag {
|
||||||
|
keyPrefix = "{dp:" + name + "}"
|
||||||
|
} else {
|
||||||
|
keyPrefix = "dp:" + name
|
||||||
|
}
|
||||||
return &DelayQueue{
|
return &DelayQueue{
|
||||||
name: name,
|
name: name,
|
||||||
redisCli: cli,
|
redisCli: cli,
|
||||||
cb: callback,
|
cb: callback,
|
||||||
pendingKey: "dp:" + name + ":pending",
|
pendingKey: keyPrefix + ":pending",
|
||||||
readyKey: "dp:" + name + ":ready",
|
readyKey: keyPrefix + ":ready",
|
||||||
unAckKey: "dp:" + name + ":unack",
|
unAckKey: keyPrefix + ":unack",
|
||||||
retryKey: "dp:" + name + ":retry",
|
retryKey: keyPrefix + ":retry",
|
||||||
retryCountKey: "dp:" + name + ":retry:cnt",
|
retryCountKey: keyPrefix + ":retry:cnt",
|
||||||
garbageKey: "dp:" + name + ":garbage",
|
garbageKey: keyPrefix + ":garbage",
|
||||||
close: make(chan struct{}, 1),
|
close: make(chan struct{}, 1),
|
||||||
maxConsumeDuration: 5 * time.Second,
|
maxConsumeDuration: 5 * time.Second,
|
||||||
msgTTL: time.Hour,
|
msgTTL: time.Hour,
|
||||||
|
@@ -24,7 +24,7 @@ func TestDelayQueue_consume(t *testing.T) {
|
|||||||
i, _ := strconv.ParseInt(s, 10, 64)
|
i, _ := strconv.ParseInt(s, 10, 64)
|
||||||
return i%2 == 0
|
return i%2 == 0
|
||||||
}
|
}
|
||||||
queue := NewQueue("test", redisCli, cb).
|
queue := NewQueue("test", redisCli, cb, UseHashTagKey()).
|
||||||
WithFetchInterval(time.Millisecond * 50).
|
WithFetchInterval(time.Millisecond * 50).
|
||||||
WithMaxConsumeDuration(0).
|
WithMaxConsumeDuration(0).
|
||||||
WithLogger(log.New(os.Stderr, "[DelayQueue]", log.LstdFlags)).
|
WithLogger(log.New(os.Stderr, "[DelayQueue]", log.LstdFlags)).
|
||||||
|
Reference in New Issue
Block a user