From c2e2f0c46c8c00b0099109cce594be009bc79e4c Mon Sep 17 00:00:00 2001 From: dj2 Date: Fri, 26 Nov 2010 10:52:09 -0500 Subject: [PATCH 1/4] add fibered em connection without activerecord --- lib/mysql2/em_fiber.rb | 25 +++++++++++++++++++++++++ mysql2.gemspec | 1 + 2 files changed, 26 insertions(+) create mode 100644 lib/mysql2/em_fiber.rb diff --git a/lib/mysql2/em_fiber.rb b/lib/mysql2/em_fiber.rb new file mode 100644 index 0000000..72b84f0 --- /dev/null +++ b/lib/mysql2/em_fiber.rb @@ -0,0 +1,25 @@ +# encoding: utf-8 + +require 'mysql2/em' +require 'fiber' unless defined? Fiber + +module Mysql2 + module EM + module Fiber + class Client < ::Mysql2::EM::Client + def query(sql, opts={}) + 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 + end + end + end + end +end \ No newline at end of file diff --git a/mysql2.gemspec b/mysql2.gemspec index a13e9b8..b07f0df 100644 --- a/mysql2.gemspec +++ b/mysql2.gemspec @@ -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", From 9d49728c3074d6bc18fbeaa48aa20f233a2f5f9e Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 26 Nov 2010 22:42:09 -0500 Subject: [PATCH 2/4] add em_fiber spec --- spec/em/em_fiber_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 spec/em/em_fiber_spec.rb diff --git a/spec/em/em_fiber_spec.rb b/spec/em/em_fiber_spec.rb new file mode 100644 index 0000000..91b294e --- /dev/null +++ b/spec/em/em_fiber_spec.rb @@ -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 \ No newline at end of file From dac2f02c9b6cfb7362d21373a1ed65ec5fcfb2bd Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 26 Nov 2010 22:42:34 -0500 Subject: [PATCH 3/4] if no reactor use synch query --- lib/mysql2/em.rb | 12 ++++++++---- lib/mysql2/em_fiber.rb | 23 ++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/mysql2/em.rb b/lib/mysql2/em.rb index 98da1e6..bfdde70 100644 --- a/lib/mysql2/em.rb +++ b/lib/mysql2/em.rb @@ -23,10 +23,14 @@ module Mysql2 end def query(sql, opts={}) - super(sql, opts.merge(:async => true)) - deferable = ::EM::DefaultDeferrable.new - ::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true - deferable + 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 diff --git a/lib/mysql2/em_fiber.rb b/lib/mysql2/em_fiber.rb index 72b84f0..7adb42f 100644 --- a/lib/mysql2/em_fiber.rb +++ b/lib/mysql2/em_fiber.rb @@ -1,23 +1,28 @@ # encoding: utf-8 require 'mysql2/em' -require 'fiber' unless defined? Fiber +require 'fiber' module Mysql2 module EM module Fiber class Client < ::Mysql2::EM::Client def query(sql, opts={}) - deferable = super(sql, opts) + if ::EM.reactor_running? + deferable = super(sql, opts) - fiber = ::Fiber.current - deferable.callback do |result| - fiber.resume(result) + fiber = ::Fiber.current + deferable.callback do |result| + fiber.resume(result) + end + deferable.errback do |err| + p err + fiber.resume(err) + end + ::Fiber.yield + else + super(sql, opts) end - deferable.errback do |err| - fiber.resume(err) - end - ::Fiber.yield end end end From 5f6ff0d4a8557094a86f1b2dc5fd77f3f49ee382 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Sat, 27 Nov 2010 15:32:54 -0500 Subject: [PATCH 4/4] forgot to remove debug --- lib/mysql2/em_fiber.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mysql2/em_fiber.rb b/lib/mysql2/em_fiber.rb index 7adb42f..9d793ae 100644 --- a/lib/mysql2/em_fiber.rb +++ b/lib/mysql2/em_fiber.rb @@ -16,7 +16,6 @@ module Mysql2 fiber.resume(result) end deferable.errback do |err| - p err fiber.resume(err) end ::Fiber.yield @@ -27,4 +26,4 @@ module Mysql2 end end end -end \ No newline at end of file +end