Skip to content

Commit

Permalink
Use EM.watch in favor of IO/blocking hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Aug 24, 2009
1 parent cdd1648 commit 6f63519
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
4 changes: 2 additions & 2 deletions em-mysql.gemspec
@@ -1,14 +1,14 @@
spec = Gem::Specification.new do |s|
s.name = 'em-mysql'
s.version = '0.3.0'
s.version = '0.4.0'
s.date = '2009-06-23'
s.summary = 'Async MySQL client API for Ruby/EventMachine'
s.email = "em-mysql@tmm1.net"
s.homepage = "http://github.com/tmm1/em-mysql"
s.description = 'Async MySQL client API for Ruby/EventMachine'
s.has_rdoc = false
s.authors = ["Aman Gupta"]
s.add_dependency('eventmachine', '>= 0.12.8')
s.add_dependency('eventmachine', '>= 0.12.9')

# git ls-files
s.files = %w[
Expand Down
26 changes: 7 additions & 19 deletions lib/em/mysql.rb
Expand Up @@ -20,7 +20,8 @@ def initialize mysql, opts
@connected = true

log 'mysql connected'
make_socket_blocking

self.notify_readable = true
EM.add_timer(0){ next_query }
end
attr_reader :processing, :connected, :opts
Expand Down Expand Up @@ -109,7 +110,8 @@ def unbind
@mysql = EventedMysql._connect @opts
@fd = @mysql.socket

@signature = EM.attach_fd @mysql.socket, true, false
@signature = EM.attach_fd @mysql.socket, true
EM.set_notify_readable @signature, true
log 'mysql connected'
EM.instance_variable_get('@conns')[@signature] = self
@connected = true
Expand Down Expand Up @@ -157,25 +159,11 @@ def execute sql, response = nil, cblk = nil, eblk = nil, &blk

def close
@connected = false
# @mysql.close
# IO.pipe
# EM.add_timer(0){ close_connection }
# close_connection
fd = detach
@io.close if @io
@io = nil
log 'detached fd', fd
end

private

def make_socket_blocking
if defined?(Fcntl::F_GETFL)
@io = IO.for_fd(@mysql.socket)
m = @io.fcntl(Fcntl::F_GETFL, 0)
@io.fcntl(Fcntl::F_SETFL, ~Fcntl::O_NONBLOCK & m)
end
end

def next_query
if @connected and !@processing and pending = @@queue.shift
Expand All @@ -192,12 +180,12 @@ def log *args
public

def self.connect opts
unless EM.respond_to?(:attach) and Mysql.method_defined?(:socket)
raise RuntimeError, 'mysqlplus and EM.attach are required for EventedMysql'
unless EM.respond_to?(:watch) and Mysql.method_defined?(:socket)
raise RuntimeError, 'mysqlplus and EM.watch are required for EventedMysql'
end

if conn = _connect(opts)
EM.attach conn.socket, self, conn, opts
EM.watch conn.socket, self, conn, opts
else
EM.add_timer(5){ connect opts }
end
Expand Down

0 comments on commit 6f63519

Please sign in to comment.