Skip to content

Instantly share code, notes, and snippets.

@outerim
Created May 12, 2011 05:19
Show Gist options
  • Save outerim/967978 to your computer and use it in GitHub Desktop.
Save outerim/967978 to your computer and use it in GitHub Desktop.
require 'mysql2/em_fiber'
require 'active_record/connection_adapters/mysql2_adapter'
module ActiveRecord
class Base
def self.mysql2_em_sync_connection(config)
config[:username] = 'root' if config[:username].nil?
if Mysql2::Client.const_defined? :FOUND_ROWS
config[:flags] = Mysql2::Client::FOUND_ROWS
end
client = Mysql2::EM::Fiber::Client.new(config.symbolize_keys)
options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
ConnectionAdapters::Mysql2EMSyncAdapter.new(client, logger, options, config)
end
end
module ConnectionAdapters
class Mysql2EMSyncAdapter < Mysql2Adapter
private
def connect
@connection = Mysql2::EM::Fiber::Client.new(@config)
configure_connection
end
end
class ConnectionPool
private
# we use fibers not threads. need a unique connection per fiber
def current_connection_id
Fiber.current.object_id
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment