From c2e2f0c46c8c00b0099109cce594be009bc79e4c Mon Sep 17 00:00:00 2001 From: dj2 Date: Fri, 26 Nov 2010 10:52:09 -0500 Subject: [PATCH] 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",