@@ -22,11 +22,12 @@ type Worker struct {
22
22
// redis config
23
23
rdb * redis.Client
24
24
pubsub * redis.PubSub
25
+ channel <- chan * redis.Message
25
26
addr string
26
27
db int
27
28
connectionString string
28
29
password string
29
- channel string
30
+ channelName string
30
31
channelSize int
31
32
32
33
stopOnce sync.Once
@@ -75,7 +76,7 @@ func WithConnectionString(connectionString string) Option {
75
76
// WithChannel setup the channel of redis
76
77
func WithChannel (channel string ) Option {
77
78
return func (w * Worker ) {
78
- w .channel = channel
79
+ w .channelName = channel
79
80
}
80
81
}
81
82
@@ -98,7 +99,7 @@ func NewWorker(opts ...Option) *Worker {
98
99
var err error
99
100
w := & Worker {
100
101
addr : "127.0.0.1:6379" ,
101
- channel : "queue" ,
102
+ channelName : "queue" ,
102
103
channelSize : 1024 ,
103
104
stop : make (chan struct {}),
104
105
logger : queue .NewLogger (),
@@ -138,7 +139,19 @@ func NewWorker(opts ...Option) *Worker {
138
139
w .rdb = rdb
139
140
140
141
ctx := context .Background ()
141
- w .pubsub = w .rdb .Subscribe (ctx , w .channel )
142
+ w .pubsub = w .rdb .Subscribe (ctx , w .channelName )
143
+
144
+ var ropts []redis.ChannelOption
145
+
146
+ if w .channelSize > 1 {
147
+ ropts = append (ropts , redis .WithChannelSize (w .channelSize ))
148
+ }
149
+
150
+ w .channel = w .pubsub .Channel (ropts ... )
151
+ // make sure the connection is successful
152
+ if err := w .pubsub .Ping (ctx ); err != nil {
153
+ w .logger .Fatal (err )
154
+ }
142
155
143
156
return w
144
157
}
@@ -247,7 +260,7 @@ func (w *Worker) Queue(job queue.QueuedMessage) error {
247
260
ctx := context .Background ()
248
261
249
262
// Publish a message.
250
- err := w .rdb .Publish (ctx , w .channel , job .Bytes ()).Err ()
263
+ err := w .rdb .Publish (ctx , w .channelName , job .Bytes ()).Err ()
251
264
if err != nil {
252
265
return err
253
266
}
@@ -257,27 +270,6 @@ func (w *Worker) Queue(job queue.QueuedMessage) error {
257
270
258
271
// Run start the worker
259
272
func (w * Worker ) Run () error {
260
- // check queue status
261
- select {
262
- case <- w .stop :
263
- return nil
264
- default :
265
- }
266
-
267
- var options []redis.ChannelOption
268
- ctx := context .Background ()
269
-
270
- if w .channelSize > 1 {
271
- options = append (options , redis .WithChannelSize (w .channelSize ))
272
- }
273
-
274
- ch := w .pubsub .Channel (options ... )
275
- // make sure the connection is successful
276
- err := w .pubsub .Ping (ctx )
277
- if err != nil {
278
- return err
279
- }
280
-
281
273
for {
282
274
// check queue status
283
275
select {
@@ -287,15 +279,15 @@ func (w *Worker) Run() error {
287
279
}
288
280
289
281
select {
290
- case m , ok := <- ch :
282
+ case m , ok := <- w . channel :
291
283
select {
292
284
case <- w .stop :
293
285
return nil
294
286
default :
295
287
}
296
288
297
289
if ! ok {
298
- return fmt .Errorf ("redis pubsub: channel=%s closed" , w .channel )
290
+ return fmt .Errorf ("redis pubsub: channel=%s closed" , w .channelName )
299
291
}
300
292
301
293
var data queue.Job
0 commit comments