Skip to content
Browse files

queue per connection

  • Loading branch information...
1 parent 0547957 commit 786c2185bf9aa40e5f1a5cb18c4776084276593d @igrigorik committed Feb 13, 2010
Showing with 22 additions and 17 deletions.
  1. +8 −12 lib/em-mysql/connection.rb
  2. +2 −2 lib/em-mysql/mysql.rb
  3. +12 −3 spec/mysql_spec.rb
View
20 lib/em-mysql/connection.rb
@@ -24,7 +24,7 @@ def initialize(mysql, opts)
@fd = mysql.socket
@opts = opts
@current = nil
- @@queue ||= []
+ @queue ||= []
@processing = false
@connected = true
@@ -74,11 +74,11 @@ def notify_readable
rescue Mysql::Error => e
log 'mysql error', e.message
if e.message =~ /Deadlock/ and retries < MAX_RETRIES_ON_DEADLOCKS
- @@queue << [response, sql, cblk, eblk, retries + 1]
+ @queue << [response, sql, cblk, eblk, retries + 1]
@processing = false
next_query
elsif DisconnectErrors.include? e.message
- @@queue << [response, sql, cblk, eblk, retries + 1]
+ @queue << [response, sql, cblk, eblk, retries + 1]
return close
elsif cb = (eblk || @opts[:on_error])
cb.call(e)
@@ -122,23 +122,19 @@ def unbind
end
end
- def execute sql, response = nil, cblk = nil, eblk = nil, retries = 0, &blk
- cblk ||= blk
-
+ def execute(sql, response = nil, cblk = nil, eblk = nil, retries = 0)
begin
- unless @processing or !@connected
+ if not @processing or not @connected
@processing = true
-
- log 'mysql sending', sql
@mysql.send_query(sql)
else
- @@queue << [response, sql, cblk, eblk, retries]
+ @queue << [response, sql, cblk, eblk, retries]
return
end
rescue Mysql::Error => e
log 'mysql error', e.message
if DisconnectErrors.include? e.message
- @@queue << [response, sql, cblk, eblk, retries]
+ @queue << [response, sql, cblk, eblk, retries]
return close
else
raise e
@@ -158,7 +154,7 @@ def close
private
def next_query
- if @connected and !@processing and pending = @@queue.shift
+ if @connected and !@processing and pending = @queue.shift
response, sql, cblk, eblk = pending
execute(sql, response, cblk, eblk)
end
View
4 lib/em-mysql/mysql.rb
@@ -22,9 +22,9 @@ def close
@connection.close
end
- def execute(sql)
+ def execute(sql, &blk)
df = EventMachine::DefaultDeferrable.new
- cb = Proc.new { |r| df.succeed(r) }
+ cb = blk || Proc.new { |r| df.succeed(r) }
eb = Proc.new { |r| df.fail(r) }
@connection.execute(sql, :select, cb, eb)
View
15 spec/mysql_spec.rb
@@ -34,14 +34,23 @@
conn = EventMachine::MySQL.new(:host => 'localhost')
query = conn.execute("select 1")
query.callback { |res|
- p res
- EventMachine.stop
+ p res
+ EventMachine.stop
}
-# EventMachine.stop
+ # EventMachine.stop
}
end
+ it "should accept block as query callback" do
+ EventMachine.run {
+ conn = EventMachine::MySQL.new(:host => 'localhost')
+ conn.execute("select 1") { |res|
+ p res
+ EventMachine.stop
+ }
+ }
+ end
# it "should reconnect when disconnected"
# it "run select queries and return results"

0 comments on commit 786c218

Please sign in to comment.
Something went wrong with that request. Please try again.