forked from igrigorik/em-synchrony
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fiber aware ActiveRecord which works not only in FiberPool
- Loading branch information
Showing
5 changed files
with
180 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,81 @@ | ||
require 'active_record' | ||
require 'active_record/connection_adapters/abstract/connection_pool' | ||
require 'active_record/patches' | ||
require 'active_record/connection_adapters/abstract_adapter' | ||
require 'active_record/connection_adapters/mysql2_adapter' | ||
require 'em-synchrony/thread' | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
class ConnectionPool | ||
def connection | ||
_fibered_mutex.synchronize do | ||
@reserved_connections[current_connection_id] ||= checkout | ||
end | ||
end | ||
|
||
def _fibered_mutex | ||
@fibered_mutex ||= EM::Synchrony::Thread::Mutex.new | ||
end | ||
end | ||
end | ||
end | ||
|
||
module EM::Synchrony | ||
module ActiveRecord | ||
class Mysql2Client < Mysql2::EM::Client | ||
attr_accessor :open_transactions | ||
attr_accessor :acquired | ||
end | ||
|
||
class Adapter < ::ActiveRecord::ConnectionAdapters::Mysql2Adapter | ||
def configure_connection | ||
nil | ||
end | ||
|
||
def transaction(*args, &blk) | ||
@connection.execute(false) do |conn| | ||
super | ||
end | ||
end | ||
|
||
def real_connection | ||
@connection.connection | ||
end | ||
|
||
def open_transactions | ||
real_connection.open_transactions | ||
end | ||
|
||
def increment_open_transactions | ||
real_connection.open_transactions += 1 | ||
end | ||
|
||
def decrement_open_transactions | ||
real_connection.open_transactions -= 1 | ||
end | ||
end | ||
|
||
class ConnectionPool < EM::Synchrony::ConnectionPool | ||
|
||
# consider connection acquired | ||
def execute(async) | ||
f = Fiber.current | ||
begin | ||
conn = acquire(f) | ||
conn.acquired += 1 | ||
yield conn | ||
ensure | ||
conn.acquired -= 1 | ||
release(f) if !async && conn.acquired == 0 | ||
end | ||
end | ||
|
||
# via method_missing affected_rows will be recognized as async method | ||
def affected_rows(*args, &blk) | ||
execute(false) do |conn| | ||
conn.send(:affected_rows, *args, &blk) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.