Navigation Menu

Skip to content

Commit

Permalink
Accept "max_messages" option for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 14, 2015
1 parent 5641dd1 commit d07a8e8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/droonga/client/connection/droonga-protocol/thread.rb
Expand Up @@ -81,16 +81,23 @@ def subscribe(message, options={}, &block)
send(message, options)

subscription_timeout = options[:subscription_timeout]
max_messages = options[:max_messages]
start = Time.now
receive_options = {
:timeout => options[:timeout] || DEFAULT_TIMEOUT_SECONDS,
}
n_messages = 0
sync = block.nil?
if sync
Enumerator.new do |yielder|
loop do
receiver.receive(receive_options) do |object|
yielder << object
n_messages += 1
end
if max_messages and
n_messages >= max_messages
break
end
if subscription_timeout
elapsed_seconds = Time.now - start
Expand All @@ -103,7 +110,14 @@ def subscribe(message, options={}, &block)
thread = ::Thread.new do
begin
loop do
receiver.receive(receive_options, &block)
receiver.receive(receive_options) do |message|
block.call(message)
n_messages += 1
end
if max_messages and
n_messages >= max_messages
break
end
if subscription_timeout
elapsed_seconds = Time.now - start
break if elapsed_seconds >= subscription_timeout
Expand Down

0 comments on commit d07a8e8

Please sign in to comment.