Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

do some housekeeping in common.rb

Move the dispatcher thread body into its own method.
Most things should be private in here.
  • Loading branch information...
commit d859b2d20227cc841fee681cc71bb03d8fb1c2b7 1 parent ceb940b
@slyphon slyphon authored
Showing with 31 additions and 29 deletions.
  1. +31 −29 lib/zookeeper/common.rb
View
60 lib/zookeeper/common.rb
@@ -10,15 +10,7 @@ def event_dispatch_thread?
@dispatcher && (@dispatcher == Thread.current)
end
-protected
- def get_next_event(blocking=true)
- @event_queue.pop(!blocking).tap do |event|
- logger.debug { "#{self.class}##{__method__} delivering event #{event.inspect}" }
- end
- rescue ThreadError
- nil
- end
-
+private
def setup_call(meth_name, opts)
req_id = nil
@mutex.synchronize {
@@ -71,21 +63,9 @@ def setup_dispatch_thread!
return
end
- logger.debug { "starting dispatch thread" }
-
- @dispatcher = Thread.new do
- while true
- begin
- dispatch_next_callback(get_next_event(true))
- rescue QueueWithPipe::ShutdownException
- logger.info { "dispatch thread exiting, got shutdown exception" }
- break
- rescue Exception => e
- $stderr.puts ["#{e.class}: #{e.message}", e.backtrace.map { |n| "\t#{n}" }.join("\n")].join("\n")
- end
- end
- signal_dispatch_thread_exit!
- end
+ logger.debug { "starting dispatch thread" }
+
+ @dispatcher = Thread.new(&method(:dispatch_thread_body))
end
end
@@ -120,11 +100,12 @@ def stop_dispatch_thread!(timeout=2)
end
end
- def signal_dispatch_thread_exit!
- @mutex.synchronize do
- logger.debug { "dispatch thread exiting!" }
- @dispatch_shutdown_cond.broadcast
+ def get_next_event(blocking=true)
+ @event_queue.pop(!blocking).tap do |event|
+ logger.debug { "#{self.class}##{__method__} delivering event #{event.inspect}" }
end
+ rescue ThreadError
+ nil
end
def dispatch_next_callback(hash)
@@ -177,7 +158,28 @@ def assert_required_keys(args, required)
end
end
-private
+ def dispatch_thread_body
+ while true
+ begin
+ dispatch_next_callback(get_next_event(true))
+ rescue QueueWithPipe::ShutdownException
+ logger.info { "dispatch thread exiting, got shutdown exception" }
+ return
+ rescue Exception => e
+ $stderr.puts ["#{e.class}: #{e.message}", e.backtrace.map { |n| "\t#{n}" }.join("\n")].join("\n")
+ end
+ end
+ ensure
+ signal_dispatch_thread_exit!
+ end
+
+ def signal_dispatch_thread_exit!
+ @mutex.synchronize do
+ logger.debug { "dispatch thread exiting!" }
+ @dispatch_shutdown_cond.broadcast
+ end
+ end
+
def prettify_event(hash)
hash.dup.tap do |h|
# pretty up the event display
Please sign in to comment.
Something went wrong with that request. Please try again.