Skip to content

Commit 2fcf6da

Browse files
committed
blpop can take a 0 timeout, so we can avoid looping with exceptions in
the non-blocking case
1 parent a49f3f2 commit 2fcf6da

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lib/resque/multi_queue.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def initialize(queues, redis)
1616

1717
@queues = {}
1818
@redis = redis
19+
@q_list = queues
1920

2021
queues.each do |queue|
2122
key = @redis.is_a?(Redis::Namespace) ? "#{@redis.namespace}:" : ""
@@ -32,25 +33,25 @@ def initialize(queues, redis)
3233
def pop(non_block = false)
3334
if non_block
3435
synchronize do
35-
value = nil
36+
queue_name, payload = @redis.blpop(*(queue_names + [0]))
3637

37-
@queues.values.each do |queue|
38-
begin
39-
return queue.pop(true)
40-
rescue ThreadError
41-
end
42-
end
43-
44-
raise ThreadError
38+
raise ThreadError unless queue_name && payload
39+
@queues[queue_name].decode(payload)
4540
end
4641
else
47-
queue_names = @queues.values.map {|queue| queue.redis_name }
4842
synchronize do
4943
value = @redis.blpop(*(queue_names + [1])) until value
5044
queue_name, payload = value
5145
@queues[queue_name].decode(payload)
5246
end
5347
end
5448
end
49+
50+
private
51+
def queue_names
52+
# possibly refactor this to set an ivar of the list in the constructor.
53+
# We don't need to calculate the list on every call to `pop`.
54+
@q_list.map {|queue| queue.redis_name }
55+
end
5556
end
5657
end

0 commit comments

Comments
 (0)