Skip to content

Commit

Permalink
make em_client close more similar to the others
Browse files Browse the repository at this point in the history
also fix a bug in setup_dispatch_thread! where it would try to attach
selectable_io to the reactor after the client it had been closed
(because of EM.schedule).
  • Loading branch information
slyphon committed Jun 16, 2011
1 parent a6ca5e5 commit 5d0c6fa
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions lib/zookeeper/em_client.rb
Expand Up @@ -39,18 +39,22 @@ def close(&block)

logger.debug { "close called, closed? #{closed?} running? #{running?}" }

unless running?
logger.debug { "we are not running, so returning on_close deferred" }
return on_close
end
if running?
@_running = false

# wake_event_loop!

@_running = false
unless closed?
@em_connection.detach if @em_connection
@em_connection = nil

wake_event_loop! unless closed?
@em_connection.detach if @em_connection
selectable_io.close unless selectable_io.closed?
logger.debug { "closing handle" }
close_handle
logger.debug { "closing handle" }
close_handle
selectable_io.close unless selectable_io.closed?
end
else
logger.debug { "we are not running, so returning on_close deferred" }
end

on_close.succeed
on_close
Expand All @@ -60,29 +64,18 @@ def close(&block)
public :dispatch_next_callback

protected
# def really_close
# unless closed?
# logger.debug { "#{self.class.name}: calling close_handle in native driver" }

# close_handle

# selectable_io.close unless selectable_io.closed?

# logger.debug { "#{self.class.name}: calling on_close.succeed" }
# on_close.succeed
# end
# end

# instead of setting up a dispatch thread here, we instead attach
# the #selectable_io to the event loop
def setup_dispatch_thread!
EM.schedule do
begin
@em_connection = EM.watch(selectable_io, ZKConnection, self) { |cnx| cnx.notify_readable = true }
rescue Exception => e
$stderr.puts "caught exception from EM.watch(): #{e.inspect}"
if running? and not closed?
begin
@em_connection = EM.watch(selectable_io, ZKConnection, self) { |cnx| cnx.notify_readable = true }
rescue Exception => e
$stderr.puts "caught exception from EM.watch(): #{e.inspect}"
end
on_attached.succeed
end
on_attached.succeed
end
end
end
Expand All @@ -98,17 +91,9 @@ class ZKConnection < EM::Connection

def initialize(zk_client)
@zk_client = zk_client
# @on_detach = EM::DefaultDeferrable.new
# @on_detach.callback { logger.debug { "#{self.class.name}: on_detach callback fired" } }
@attached = true
end

# called back when we've successfully detached from the EM reactor
# def on_detach(&blk)
# @on_detach.callback(&blk) if blk
# @on_detach
# end

def attached?
@attached
end
Expand Down

0 comments on commit 5d0c6fa

Please sign in to comment.