Skip to content

Commit fa14a01

Browse files
committed
support custom logger
1 parent 705b972 commit fa14a01

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func producer() {
9797

9898
## Options
9999

100+
### Consume Function
101+
100102
```go
101103
func (q *DelayQueue)WithCallback(callback CallbackFunc) *DelayQueue
102104
```
@@ -113,25 +115,38 @@ queue.WithCallback(func(payload string) bool {
113115
})
114116
```
115117

118+
### Logger
119+
116120
```go
117-
func (q *DelayQueue)WithLogger(logger *log.Logger) *DelayQueue
121+
func (q *DelayQueue)WithLogger(logger Logger) *DelayQueue
118122
```
119123

120-
WithLogger customizes logger for queue
124+
WithLogger customizes logger for queue. Logger should implemented the following interface:
125+
126+
```go
127+
type Logger interface {
128+
Printf(format string, v ...interface{})
129+
}
130+
```
121131

132+
### Concurrent
122133

123134
```go
124135
func (q *DelayQueue)WithConcurrent(c uint) *DelayQueue
125136
```
126137

127138
WithConcurrent sets the number of concurrent consumers
128139

140+
### Polling Interval
141+
129142
```go
130143
func (q *DelayQueue)WithFetchInterval(d time.Duration) *DelayQueue
131144
```
132145

133146
WithFetchInterval customizes the interval at which consumer fetch message from redis
134147

148+
### Timeout
149+
135150
```go
136151
func (q *DelayQueue)WithMaxConsumeDuration(d time.Duration) *DelayQueue
137152
```
@@ -141,12 +156,16 @@ WithMaxConsumeDuration customizes max consume duration
141156
If no acknowledge received within WithMaxConsumeDuration after message delivery, DelayQueue will try to deliver this
142157
message again
143158

159+
### Max Processing Limit
160+
144161
```go
145162
func (q *DelayQueue)WithFetchLimit(limit uint) *DelayQueue
146163
```
147164

148165
WithFetchLimit limits the max number of unack (processing) messages
149166

167+
### Hash Tag
168+
150169
```go
151170
UseHashTagKey()
152171
```
@@ -159,6 +178,8 @@ WARNING! CHANGING(add or remove) this option will cause DelayQueue failing to re
159178

160179
> see more: https://redis.io/docs/reference/cluster-spec/#hash-tags
161180

181+
### Default Retry Count
182+
162183
```go
163184
WithDefaultRetryCount(count uint) *DelayQueue
164185
```
@@ -167,6 +188,12 @@ WithDefaultRetryCount customizes the max number of retry, it effects of messages
167188

168189
use WithRetryCount during DelayQueue.SendScheduleMsg or DelayQueue.SendDelayMsg to specific retry count of particular message
169190

191+
```go
192+
queue.SendDelayMsg(msg, time.Hour, delayqueue.WithRetryCount(3))
193+
```
194+
195+
### Script Preload
196+
170197
```go
171198
(q *DelayQueue) WithScriptPreload(flag bool) *DelayQueue
172199
```

README_CN.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func producer() {
9292

9393
## 选项
9494

95+
### 回调函数
96+
9597
```go
9698
func (q *DelayQueue)WithCallback(callback CallbackFunc) *DelayQueue
9799
```
@@ -107,36 +109,53 @@ queue.WithCallback(func(payload string) bool {
107109
})
108110
```
109111

112+
### 日志
113+
110114
```go
111115
func (q *DelayQueue)WithLogger(logger *log.Logger) *DelayQueue
112116
```
113117

114-
为 DelayQueue 设置 logger
118+
为 DelayQueue 设置 logger, logger 需要实现下面的接口:
119+
120+
```go
121+
type Logger interface {
122+
Printf(format string, v ...interface{})
123+
}
124+
```
115125

126+
### 并发数
116127

117128
```go
118129
func (q *DelayQueue)WithConcurrent(c uint) *DelayQueue
119130
```
120131

121132
设置消费者并发数
122133

134+
### 轮询间隔
135+
123136
```go
124137
func (q *DelayQueue)WithFetchInterval(d time.Duration) *DelayQueue
125138
```
126139

127140
设置消费者从 Redis 拉取消息的时间间隔
128141

142+
### 消费超时
143+
129144
```go
130145
func (q *DelayQueue)WithMaxConsumeDuration(d time.Duration) *DelayQueue
131146
```
132147

133148
设置最长消费时间。若拉取消息后超出 MaxConsumeDuration 时限仍未返回 ACK 则认为消费失败,DelayQueue 会重新投递此消息。
134149

150+
### 最大处理中消息数
151+
135152
```go
136153
func (q *DelayQueue)WithFetchLimit(limit uint) *DelayQueue
137154
```
138155

139-
FetchLimit 限制消费者从 Redis 中拉取的消息数目,即单个消费者正在处理中的消息数不会超过 FetchLimit
156+
单个消费者正在处理中的消息数不会超过 FetchLimit
157+
158+
### 启用 HashTag
140159

141160
```go
142161
UseHashTagKey()
@@ -150,6 +169,8 @@ UseHashTagKey() 会在 Redis Key 上添加 hash tag 确保同一个队列的所
150169

151170
see more: https://redis.io/docs/reference/cluster-spec/#hash-tags
152171

172+
### 设置默认重试次数
173+
153174
```go
154175
WithDefaultRetryCount(count uint)
155176
```
@@ -158,6 +179,12 @@ WithDefaultRetryCount(count uint)
158179

159180
在调用 DelayQueue.SendScheduleMsg or DelayQueue.SendDelayMsg 发送消息时,可以调用 WithRetryCount 为这条消息单独指定重试次数。
160181

182+
```go
183+
queue.SendDelayMsg(msg, time.Hour, delayqueue.WithRetryCount(3))
184+
```
185+
186+
### 预加载脚本
187+
161188
```go
162189
(q *DelayQueue) WithScriptPreload(flag bool) *DelayQueue
163190
```

delayqueue.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type DelayQueue struct {
2727
garbageKey string // set: message id
2828
useHashTag bool
2929
ticker *time.Ticker
30-
logger *log.Logger
30+
logger Logger
3131
close chan struct{}
3232
running int32
3333
maxConsumeDuration time.Duration // default 5 seconds
@@ -82,6 +82,11 @@ type RedisCli interface {
8282
EvalSha(sha1 string, keys []string, args []interface{}) (interface{}, error)
8383
}
8484

85+
// Logger is an abstraction of logging system
86+
type Logger interface {
87+
Printf(format string, v ...interface{})
88+
}
89+
8590
type hashTagKeyOpt int
8691

8792
// CallbackFunc receives and consumes messages
@@ -153,7 +158,7 @@ func (q *DelayQueue) WithCallback(callback CallbackFunc) *DelayQueue {
153158
}
154159

155160
// WithLogger customizes logger for queue
156-
func (q *DelayQueue) WithLogger(logger *log.Logger) *DelayQueue {
161+
func (q *DelayQueue) WithLogger(logger Logger) *DelayQueue {
157162
q.logger = logger
158163
return q
159164
}

0 commit comments

Comments
 (0)