Permalink
Browse files

use shared queue between all connections

  • Loading branch information...
1 parent b2ac78c commit f523d096acc389685ecdf9fa85949377d8e68629 @tmm1 committed Feb 20, 2009
Showing with 10 additions and 9 deletions.
  1. +10 −9 lib/em/mysql.rb
View
@@ -13,8 +13,8 @@ def initialize mysql, opts
@mysql = mysql
@fd = mysql.socket
@opts = opts
- @queue = []
- @pending = []
+ @current = nil
+ @@queue ||= []
@processing = false
@connected = true
@@ -36,7 +36,8 @@ def connection_completed
def notify_readable
log 'readable'
- if item = @queue.shift
+ if item = @current
+ @current = nil
start, response, sql, cblk, eblk = item
log 'mysql response', Time.now-start, sql
arg = case response
@@ -73,7 +74,7 @@ def notify_readable
rescue Mysql::Error => e
log 'mysql error', e.message
if DisconnectErrors.include? e.message
- @pending << [response, sql, cblk, eblk]
+ @@queue << [response, sql, cblk, eblk]
return close
elsif cb = (eblk || @opts[:on_error])
cb.call(e)
@@ -123,7 +124,7 @@ def execute sql, response = nil, cblk = nil, eblk = nil, &blk
# # log 'mysql errno', @mysql.errno
# rescue
# log 'mysql ping failed'
- # @pending << [response, sql, blk]
+ # @@queue << [response, sql, blk]
# return close
# end
@@ -132,21 +133,21 @@ def execute sql, response = nil, cblk = nil, eblk = nil, &blk
log 'mysql sending', sql
@mysql.send_query(sql)
else
- @pending << [response, sql, cblk, eblk]
+ @@queue << [response, sql, cblk, eblk]
return
end
rescue Mysql::Error => e
log 'mysql error', e.message
if DisconnectErrors.include? e.message
- @pending << [response, sql, cblk, eblk]
+ @@queue << [response, sql, cblk, eblk]
return close
else
raise e
end
end
log 'queuing', response, sql
- @queue << [Time.now, response, sql, cblk, eblk]
+ @current = [Time.now, response, sql, cblk, eblk]
end
def close
@@ -162,7 +163,7 @@ def close
private
def next_query
- if @connected and !@processing and pending = @pending.shift
+ if @connected and !@processing and pending = @@queue.shift
response, sql, cblk, eblk = pending
execute(sql, response, cblk, eblk)
end

0 comments on commit f523d09

Please sign in to comment.