mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
push synchronization in to each method. Reduces method calls and makes
it clear which methods are synchronized.
This commit is contained in:
parent
0e2477b602
commit
c606fe2c6f
1 changed files with 29 additions and 25 deletions
|
@ -1,7 +1,6 @@
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'monitor'
|
require 'monitor'
|
||||||
require 'set'
|
require 'set'
|
||||||
require 'active_support/core_ext/module/synchronization'
|
|
||||||
require 'active_support/core_ext/module/deprecation'
|
require 'active_support/core_ext/module/deprecation'
|
||||||
|
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
|
@ -58,6 +57,8 @@ module ActiveRecord
|
||||||
# * +wait_timeout+: number of seconds to block and wait for a connection
|
# * +wait_timeout+: number of seconds to block and wait for a connection
|
||||||
# before giving up and raising a timeout error (default 5 seconds).
|
# before giving up and raising a timeout error (default 5 seconds).
|
||||||
class ConnectionPool
|
class ConnectionPool
|
||||||
|
include MonitorMixin
|
||||||
|
|
||||||
attr_accessor :automatic_reconnect
|
attr_accessor :automatic_reconnect
|
||||||
attr_reader :spec, :connections
|
attr_reader :spec, :connections
|
||||||
|
|
||||||
|
@ -68,14 +69,14 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
# The default ConnectionPool maximum size is 5.
|
# The default ConnectionPool maximum size is 5.
|
||||||
def initialize(spec)
|
def initialize(spec)
|
||||||
|
super()
|
||||||
|
|
||||||
@spec = spec
|
@spec = spec
|
||||||
|
|
||||||
# The cache of reserved connections mapped to threads
|
# The cache of reserved connections mapped to threads
|
||||||
@reserved_connections = {}
|
@reserved_connections = {}
|
||||||
|
|
||||||
# The mutex used to synchronize pool access
|
@queue = new_cond
|
||||||
@connection_mutex = Monitor.new
|
|
||||||
@queue = @connection_mutex.new_cond
|
|
||||||
@timeout = spec.config[:wait_timeout] || 5
|
@timeout = spec.config[:wait_timeout] || 5
|
||||||
|
|
||||||
# default max pool size to 5
|
# default max pool size to 5
|
||||||
|
@ -121,37 +122,43 @@ module ActiveRecord
|
||||||
|
|
||||||
# Returns true if a connection has already been opened.
|
# Returns true if a connection has already been opened.
|
||||||
def connected?
|
def connected?
|
||||||
@connections.any?
|
synchronize { @connections.any? }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Disconnects all connections in the pool, and clears the pool.
|
# Disconnects all connections in the pool, and clears the pool.
|
||||||
def disconnect!
|
def disconnect!
|
||||||
@reserved_connections = {}
|
synchronize do
|
||||||
@connections.each do |conn|
|
@reserved_connections = {}
|
||||||
checkin conn
|
@connections.each do |conn|
|
||||||
conn.disconnect!
|
checkin conn
|
||||||
|
conn.disconnect!
|
||||||
|
end
|
||||||
|
@connections = []
|
||||||
end
|
end
|
||||||
@connections = []
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Clears the cache which maps classes.
|
# Clears the cache which maps classes.
|
||||||
def clear_reloadable_connections!
|
def clear_reloadable_connections!
|
||||||
@reserved_connections = {}
|
synchronize do
|
||||||
@connections.each do |conn|
|
@reserved_connections = {}
|
||||||
checkin conn
|
@connections.each do |conn|
|
||||||
conn.disconnect! if conn.requires_reloading?
|
checkin conn
|
||||||
end
|
conn.disconnect! if conn.requires_reloading?
|
||||||
@connections.delete_if do |conn|
|
end
|
||||||
conn.requires_reloading?
|
@connections.delete_if do |conn|
|
||||||
|
conn.requires_reloading?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Verify active connections and remove and disconnect connections
|
# Verify active connections and remove and disconnect connections
|
||||||
# associated with stale threads.
|
# associated with stale threads.
|
||||||
def verify_active_connections! #:nodoc:
|
def verify_active_connections! #:nodoc:
|
||||||
clear_stale_cached_connections!
|
synchronize do
|
||||||
@connections.each do |connection|
|
clear_stale_cached_connections!
|
||||||
connection.verify!
|
@connections.each do |connection|
|
||||||
|
connection.verify!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -219,7 +226,7 @@ connection. For example: ActiveRecord::Base.connection.close
|
||||||
# within the timeout period.
|
# within the timeout period.
|
||||||
def checkout
|
def checkout
|
||||||
# Checkout an available connection
|
# Checkout an available connection
|
||||||
@connection_mutex.synchronize do
|
synchronize do
|
||||||
loop do
|
loop do
|
||||||
conn = @connections.find { |c| c.lease }
|
conn = @connections.find { |c| c.lease }
|
||||||
|
|
||||||
|
@ -256,7 +263,7 @@ connection. For example: ActiveRecord::Base.connection.close
|
||||||
# +conn+: an AbstractAdapter object, which was obtained by earlier by
|
# +conn+: an AbstractAdapter object, which was obtained by earlier by
|
||||||
# calling +checkout+ on this pool.
|
# calling +checkout+ on this pool.
|
||||||
def checkin(conn)
|
def checkin(conn)
|
||||||
@connection_mutex.synchronize do
|
synchronize do
|
||||||
conn.run_callbacks :checkin do
|
conn.run_callbacks :checkin do
|
||||||
conn.expire
|
conn.expire
|
||||||
@queue.signal
|
@queue.signal
|
||||||
|
@ -264,9 +271,6 @@ connection. For example: ActiveRecord::Base.connection.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
synchronize :clear_reloadable_connections!, :verify_active_connections!,
|
|
||||||
:connected?, :disconnect!, :with => :@connection_mutex
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def new_connection
|
def new_connection
|
||||||
|
|
Loading…
Reference in a new issue