Merge branch 'em_fiber' of https://github.com/dj2/mysql2 into dj2-em_fiber
This commit is contained in:
commit
66f595a406
@ -23,10 +23,14 @@ module Mysql2
|
||||
end
|
||||
|
||||
def query(sql, opts={})
|
||||
if ::EM.reactor_running?
|
||||
super(sql, opts.merge(:async => true))
|
||||
deferable = ::EM::DefaultDeferrable.new
|
||||
::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true
|
||||
deferable
|
||||
else
|
||||
super(sql, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
29
lib/mysql2/em_fiber.rb
Normal file
29
lib/mysql2/em_fiber.rb
Normal file
@ -0,0 +1,29 @@
|
||||
# encoding: utf-8
|
||||
|
||||
require 'mysql2/em'
|
||||
require 'fiber'
|
||||
|
||||
module Mysql2
|
||||
module EM
|
||||
module Fiber
|
||||
class Client < ::Mysql2::EM::Client
|
||||
def query(sql, opts={})
|
||||
if ::EM.reactor_running?
|
||||
deferable = super(sql, opts)
|
||||
|
||||
fiber = ::Fiber.current
|
||||
deferable.callback do |result|
|
||||
fiber.resume(result)
|
||||
end
|
||||
deferable.errback do |err|
|
||||
fiber.resume(err)
|
||||
end
|
||||
::Fiber.yield
|
||||
else
|
||||
super(sql, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -46,6 +46,7 @@ Gem::Specification.new do |s|
|
||||
"lib/mysql2.rb",
|
||||
"lib/mysql2/client.rb",
|
||||
"lib/mysql2/em.rb",
|
||||
"lib/mysql2/em_fiber.rb",
|
||||
"lib/mysql2/error.rb",
|
||||
"lib/mysql2/result.rb",
|
||||
"mysql2.gemspec",
|
||||
|
22
spec/em/em_fiber_spec.rb
Normal file
22
spec/em/em_fiber_spec.rb
Normal file
@ -0,0 +1,22 @@
|
||||
# encoding: UTF-8
|
||||
if defined? EventMachine && defined? Fiber
|
||||
require 'spec_helper'
|
||||
require 'mysql2/em_fiber'
|
||||
|
||||
describe Mysql2::EM::Fiber::Client do
|
||||
it 'should support queries' do
|
||||
results = []
|
||||
EM.run do
|
||||
Fiber.new {
|
||||
client1 = Mysql2::EM::Fiber::Client.new
|
||||
results = client1.query "SELECT sleep(0.1) as first_query"
|
||||
EM.stop_event_loop
|
||||
}.resume
|
||||
end
|
||||
|
||||
results.first.keys.should include("first_query")
|
||||
end
|
||||
end
|
||||
else
|
||||
puts "Either EventMachine or Fibers not available. Skipping tests that use them."
|
||||
end
|
Loading…
Reference in New Issue
Block a user