Skip to content

Commit 7816eb0

Browse files
committed
support custom prefix
1 parent f3d0307 commit 7816eb0

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ But if consumption exceeded deadline, the message will be redelivered immediatel
235235

236236
WithScriptPreload(true) makes DelayQueue preload scripts and call them using EvalSha to reduce communication costs. WithScriptPreload(false) makes DelayQueue run scripts by Eval commnand. Using preload and EvalSha by Default
237237

238+
### Customize Prefix
239+
240+
```go
241+
queue := delayqueue.NewQueue("example", redisCli, callback, UseCustomPrefix("MyPrefix"))
242+
```
243+
244+
All keys of delayqueue has a smae prefix, `dp` by default. If you want to modify the prefix, you could use `UseCustomPrefix`.
245+
246+
238247
## Monitoring
239248

240249
We provides Monitor to monitor the running status.

README_CN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ WithScriptPreload(true) 会让 delayqueue 预上传脚本并使用 EvalSha 命
226226

227227
ScriptPreload 默认值为 true.
228228

229+
### 自定义前缀
230+
231+
```go
232+
queue := delayqueue.NewQueue("example", redisCli, callback, UseCustomPrefix("MyPrefix"))
233+
```
234+
235+
delayqueue 中所有的 key 都有相同的前缀,默认情况下前缀为 `dp`。如果你需要自定义前缀可以使用 UseCustomPrefix 函数。
236+
229237
## 监控
230238

231239
我们提供了 `Monitor` 来监控运行数据:

delayqueue.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type Logger interface {
9191
}
9292

9393
type hashTagKeyOpt int
94+
type prefixOpt string
9495

9596
// CallbackFunc receives and consumes messages
9697
// returns true to confirm successfully consumed, false to re-deliver this message
@@ -104,6 +105,11 @@ func UseHashTagKey() interface{} {
104105
return hashTagKeyOpt(1)
105106
}
106107

108+
// UseCustomPrefix customize prefix to instead of default prefix "dp"
109+
func UseCustomPrefix(prefix string) interface{} {
110+
return prefixOpt(prefix)
111+
}
112+
107113
// NewQueue0 creates a new queue, use DelayQueue.StartConsume to consume or DelayQueue.SendScheduleMsg to publish message
108114
// callback returns true to confirm successful consumption. If callback returns false or not return within maxConsumeDuration, DelayQueue will re-deliver this message
109115
func NewQueue0(name string, cli RedisCli, opts ...interface{}) *DelayQueue {
@@ -113,22 +119,23 @@ func NewQueue0(name string, cli RedisCli, opts ...interface{}) *DelayQueue {
113119
if cli == nil {
114120
panic("cli is required")
115121
}
122+
prefix := "dp"
116123
useHashTag := false
117124
var callback CallbackFunc = nil
118125
for _, opt := range opts {
119126
switch o := opt.(type) {
120127
case hashTagKeyOpt:
121128
useHashTag = true
129+
case prefixOpt:
130+
prefix = string(o)
122131
case CallbackFunc:
123132
callback = o
124133
}
125134
}
126-
var keyPrefix string
135+
keyPrefix := prefix + ":" + name
127136
if useHashTag {
128-
keyPrefix = "{dp:" + name + "}"
129-
} else {
130-
keyPrefix = "dp:" + name
131-
}
137+
keyPrefix = "{" + keyPrefix + "}"
138+
}
132139
return &DelayQueue{
133140
name: name,
134141
redisCli: cli,

delayqueue_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"os"
77
"strconv"
8+
"strings"
89
"sync"
910
"testing"
1011
"time"
@@ -478,3 +479,32 @@ func TestDelayQueue_TryIntercept(t *testing.T) {
478479
t.Error("expect empty messages")
479480
}
480481
}
482+
483+
func TestUseCustomPrefix(t *testing.T) {
484+
redisCli := redis.NewClient(&redis.Options{
485+
Addr: "127.0.0.1:6379",
486+
})
487+
cb := func(s string) bool {
488+
return false
489+
}
490+
prefix := "MYQUEUE"
491+
dp := NewQueue("test", redisCli, cb, UseCustomPrefix(prefix))
492+
if !strings.HasPrefix(dp.pendingKey, prefix) {
493+
t.Error("wrong prefix")
494+
}
495+
if !strings.HasPrefix(dp.readyKey, prefix) {
496+
t.Error("wrong prefix")
497+
}
498+
if !strings.HasPrefix(dp.unAckKey, prefix) {
499+
t.Error("wrong prefix")
500+
}
501+
if !strings.HasPrefix(dp.retryKey, prefix) {
502+
t.Error("wrong prefix")
503+
}
504+
if !strings.HasPrefix(dp.retryCountKey, prefix) {
505+
t.Error("wrong prefix")
506+
}
507+
if !strings.HasPrefix(dp.garbageKey, prefix) {
508+
t.Error("wrong prefix")
509+
}
510+
}

0 commit comments

Comments
 (0)