em-mysql 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/em-mysql.gemspec +1 -1
- data/lib/em/mysql.rb +45 -14
- data/lib/sequel/async.rb +12 -7
- metadata +1 -1
data/em-mysql.gemspec
CHANGED
data/lib/em/mysql.rb
CHANGED
@@ -15,7 +15,6 @@ class EventedMysql < EM::Connection
|
|
15
15
|
@fd = mysql.socket
|
16
16
|
@opts = opts
|
17
17
|
@current = nil
|
18
|
-
@@queue ||= []
|
19
18
|
@processing = false
|
20
19
|
@connected = true
|
21
20
|
|
@@ -33,6 +32,10 @@ class EventedMysql < EM::Connection
|
|
33
32
|
'Lost connection to MySQL server during query'
|
34
33
|
] unless defined? DisconnectErrors
|
35
34
|
|
35
|
+
def queue
|
36
|
+
opts[:em_mysql].queue
|
37
|
+
end
|
38
|
+
|
36
39
|
def notify_readable
|
37
40
|
log 'readable'
|
38
41
|
if item = @current
|
@@ -73,11 +76,11 @@ class EventedMysql < EM::Connection
|
|
73
76
|
rescue Mysql::Error => e
|
74
77
|
log 'mysql error', e.message
|
75
78
|
if e.message =~ /Deadlock/
|
76
|
-
|
79
|
+
queue << [response, sql, cblk, eblk]
|
77
80
|
@processing = false
|
78
81
|
next_query
|
79
82
|
elsif DisconnectErrors.include? e.message
|
80
|
-
|
83
|
+
queue << [response, sql, cblk, eblk]
|
81
84
|
return close
|
82
85
|
elsif cb = (eblk || @opts[:on_error])
|
83
86
|
cb.call(e)
|
@@ -140,13 +143,13 @@ class EventedMysql < EM::Connection
|
|
140
143
|
log 'mysql sending', sql
|
141
144
|
@mysql.send_query(sql)
|
142
145
|
else
|
143
|
-
|
146
|
+
queue << [response, sql, cblk, eblk]
|
144
147
|
return
|
145
148
|
end
|
146
149
|
rescue Mysql::Error => e
|
147
150
|
log 'mysql error', e.message
|
148
151
|
if DisconnectErrors.include? e.message
|
149
|
-
|
152
|
+
queue << [response, sql, cblk, eblk]
|
150
153
|
return close
|
151
154
|
else
|
152
155
|
raise e
|
@@ -166,7 +169,7 @@ class EventedMysql < EM::Connection
|
|
166
169
|
private
|
167
170
|
|
168
171
|
def next_query
|
169
|
-
if @connected and !@processing and pending =
|
172
|
+
if @connected and !@processing and pending = queue.shift
|
170
173
|
response, sql, cblk, eblk = pending
|
171
174
|
execute(sql, response, cblk, eblk)
|
172
175
|
end
|
@@ -176,6 +179,7 @@ class EventedMysql < EM::Connection
|
|
176
179
|
return unless @opts[:logging]
|
177
180
|
p [Time.now, @fd, (@signature[-4..-1] if @signature), *args]
|
178
181
|
end
|
182
|
+
|
179
183
|
|
180
184
|
public
|
181
185
|
|
@@ -248,30 +252,40 @@ class EventedMysql < EM::Connection
|
|
248
252
|
end
|
249
253
|
end
|
250
254
|
|
251
|
-
class
|
252
|
-
def
|
253
|
-
@
|
255
|
+
class EmMysql
|
256
|
+
def initialize(settings = {})
|
257
|
+
@queue = []
|
258
|
+
@settings = { :connections => 4, :logging => false, :em_mysql => self }.merge settings
|
259
|
+
end
|
260
|
+
|
261
|
+
def queue
|
262
|
+
@queue
|
263
|
+
end
|
264
|
+
|
265
|
+
def settings
|
266
|
+
@settings
|
254
267
|
end
|
255
268
|
|
256
|
-
def
|
269
|
+
def execute query, type = nil, cblk = nil, eblk = nil, &blk
|
257
270
|
unless nil#connection = connection_pool.find{|c| not c.processing and c.connected }
|
258
271
|
@n ||= 0
|
272
|
+
|
259
273
|
connection = connection_pool[@n]
|
260
274
|
@n = 0 if (@n+=1) >= connection_pool.size
|
261
275
|
end
|
262
|
-
|
276
|
+
|
263
277
|
connection.execute(query, type, cblk, eblk, &blk)
|
264
278
|
end
|
265
279
|
|
266
280
|
%w[ select insert update raw ].each do |type| class_eval %[
|
267
281
|
|
268
|
-
def
|
282
|
+
def #{type} query, cblk = nil, eblk = nil, &blk
|
269
283
|
execute query, :#{type}, cblk, eblk, &blk
|
270
284
|
end
|
271
285
|
|
272
286
|
] end
|
273
287
|
|
274
|
-
def
|
288
|
+
def all query, type = nil, &blk
|
275
289
|
responses = 0
|
276
290
|
connection_pool.each do |c|
|
277
291
|
c.execute(query, type) do
|
@@ -281,7 +295,7 @@ class EventedMysql
|
|
281
295
|
end
|
282
296
|
end
|
283
297
|
|
284
|
-
def
|
298
|
+
def connection_pool
|
285
299
|
@connection_pool ||= (1..settings[:connections]).map{ EventedMysql.connect(settings) }
|
286
300
|
# p ['connpool', settings[:connections], @connection_pool.size]
|
287
301
|
# (1..(settings[:connections]-@connection_pool.size)).each do
|
@@ -291,6 +305,23 @@ class EventedMysql
|
|
291
305
|
end
|
292
306
|
end
|
293
307
|
|
308
|
+
class EventedMysql
|
309
|
+
@@proxy_object = EmMysql.new
|
310
|
+
|
311
|
+
def self.databases
|
312
|
+
@@databases ||= {}
|
313
|
+
end
|
314
|
+
|
315
|
+
[:settings, :connection_pool].each do |name|
|
316
|
+
class_eval "def self.#{name}(*args) @@proxy_object.send(:#{name}, *args); end"
|
317
|
+
end
|
318
|
+
|
319
|
+
[:execute, :connection_pool, :select, :insert, :update, :raw].each do |name|
|
320
|
+
class_eval "def self.#{name}(*args, &block) @@proxy_object.send(:#{name}, *args, &block); end"
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
294
325
|
if __FILE__ == $0 and require 'em/spec'
|
295
326
|
|
296
327
|
EM.describe EventedMysql, 'individual connections' do
|
data/lib/sequel/async.rb
CHANGED
@@ -50,38 +50,43 @@ raise 'need Sequel >= 3.2.0' unless Sequel::MAJOR == 3 and Sequel::MINOR >= 2
|
|
50
50
|
|
51
51
|
module Sequel
|
52
52
|
class Dataset
|
53
|
+
STOCK_COUNT_OPTS = {:select => ["COUNT(*)".lit], :order => nil} unless defined? STOCK_COUNT_OPTS
|
54
|
+
def em_mysq
|
55
|
+
EventedMysql.databases[db] || EventedMysql
|
56
|
+
end
|
57
|
+
|
53
58
|
def async_insert *args, &cb
|
54
|
-
|
59
|
+
em_mysq.insert insert_sql(*args), &cb
|
55
60
|
nil
|
56
61
|
end
|
57
62
|
|
58
63
|
def async_insert_ignore *args, &cb
|
59
|
-
|
64
|
+
em_mysq.insert insert_ignore.insert_sql(*args), &cb
|
60
65
|
nil
|
61
66
|
end
|
62
67
|
|
63
68
|
def async_update *args, &cb
|
64
|
-
|
69
|
+
em_mysq.update update_sql(*args), &cb
|
65
70
|
nil
|
66
71
|
end
|
67
72
|
|
68
73
|
def async_delete &cb
|
69
|
-
|
74
|
+
em_mysq.execute delete_sql, &cb
|
70
75
|
nil
|
71
76
|
end
|
72
77
|
|
73
78
|
def async_multi_insert *args, &cb
|
74
|
-
|
79
|
+
em_mysq.execute multi_insert_sql(*args).first, &cb
|
75
80
|
nil
|
76
81
|
end
|
77
82
|
|
78
83
|
def async_multi_insert_ignore *args, &cb
|
79
|
-
|
84
|
+
em_mysq.execute insert_ignore.multi_insert_sql(*args).first, &cb
|
80
85
|
nil
|
81
86
|
end
|
82
87
|
|
83
88
|
def async_fetch_rows sql, iter = :each
|
84
|
-
|
89
|
+
em_mysq.raw(sql) do |m|
|
85
90
|
r = m.result
|
86
91
|
|
87
92
|
i = -1
|