Navigation Menu

Skip to content

Commit

Permalink
Do timeout correctly for subscriptions with Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 14, 2015
1 parent 9698eaa commit d7b9c4f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/droonga/client/connection/droonga-protocol/thread.rb
Expand Up @@ -81,20 +81,30 @@ def subscribe(message, options={}, &block)
receive_options = {
:timeout => options[:timeout_seconds],
}
start = Time.new
sync = block.nil?
if sync
Enumerator.new do |yielder|
loop do
receiver.receive(receive_options) do |object|
yielder << object
end
if receive_options[:timeout] and
Time.new - start > receive_options[:timeout]
break
end
end
receiver.close
end
else
thread = ::Thread.new do
begin
loop do
receiver.receive(receive_options, &block)
if receive_options[:timeout] and
Time.new - start > receive_options[:timeout]
break
end
end
ensure
receiver.close
Expand Down

0 comments on commit d7b9c4f

Please sign in to comment.