Created a runtime registry for thread local variables in active record.
This commit is contained in:
parent
e94f024e14
commit
95ac3913ee
|
@ -50,6 +50,7 @@ module ActiveRecord
|
||||||
autoload :Querying
|
autoload :Querying
|
||||||
autoload :ReadonlyAttributes
|
autoload :ReadonlyAttributes
|
||||||
autoload :Reflection
|
autoload :Reflection
|
||||||
|
autoload :RuntimeRegistry
|
||||||
autoload :Sanitization
|
autoload :Sanitization
|
||||||
autoload :Schema
|
autoload :Schema
|
||||||
autoload :SchemaDumper
|
autoload :SchemaDumper
|
||||||
|
|
|
@ -54,11 +54,11 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection_id
|
def connection_id
|
||||||
Thread.current['ActiveRecord::Base.connection_id']
|
ActiveRecord::RuntimeRegistry.instance.connection_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection_id=(connection_id)
|
def connection_id=(connection_id)
|
||||||
Thread.current['ActiveRecord::Base.connection_id'] = connection_id
|
ActiveRecord::RuntimeRegistry.instance.connection_id = connection_id
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the configuration of the associated connection as a hash:
|
# Returns the configuration of the associated connection as a hash:
|
||||||
|
|
|
@ -80,11 +80,11 @@ module ActiveRecord
|
||||||
class_attribute :default_connection_handler, instance_writer: false
|
class_attribute :default_connection_handler, instance_writer: false
|
||||||
|
|
||||||
def self.connection_handler
|
def self.connection_handler
|
||||||
Thread.current[:active_record_connection_handler] || self.default_connection_handler
|
ActiveRecord::RuntimeRegistry.instance.connection_handler || self.default_connection_handler
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.connection_handler=(handler)
|
def self.connection_handler=(handler)
|
||||||
Thread.current[:active_record_connection_handler] = handler
|
ActiveRecord::RuntimeRegistry.instance.connection_handler = handler
|
||||||
end
|
end
|
||||||
|
|
||||||
self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
|
self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
|
||||||
|
|
|
@ -3,11 +3,11 @@ module ActiveRecord
|
||||||
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
|
IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
|
||||||
|
|
||||||
def self.runtime=(value)
|
def self.runtime=(value)
|
||||||
Thread.current[:active_record_sql_runtime] = value
|
ActiveRecord::RuntimeRegistry.instance.sql_runtime = value
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.runtime
|
def self.runtime
|
||||||
Thread.current[:active_record_sql_runtime] ||= 0
|
ActiveRecord::RuntimeRegistry.instance.sql_runtime ||= 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.reset_runtime
|
def self.reset_runtime
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
require 'active_support/per_thread_registry'
|
||||||
|
|
||||||
|
module ActiveRecord
|
||||||
|
# This is a registry class for storing local variables in active record. The
|
||||||
|
# class allows you to access variables that are local to the current thread.
|
||||||
|
# These thread local variables are stored as attributes in the
|
||||||
|
# +RuntimeRegistry+ class.
|
||||||
|
#
|
||||||
|
# You can access the thread local variables by calling a variable's name on
|
||||||
|
# the +RuntimeRegistry+ class. For example, if you wanted to obtain the
|
||||||
|
# connection handler for the current thread, you would invoke:
|
||||||
|
#
|
||||||
|
# ActiveRecord::RuntimeRegistry.instance.connection_handler
|
||||||
|
#
|
||||||
|
# The +PerThreadRegistry+ module will make a new +RuntimeRegistry+ instance
|
||||||
|
# and store it in +Thread.current+. Whenever you make a call for an attribute
|
||||||
|
# on the +RuntimeRegistry+ class, the call will be sent to the instance that
|
||||||
|
# is stored in +Thread.current+.
|
||||||
|
#
|
||||||
|
# Note that you can also make the following call which would provide an
|
||||||
|
# equivalent result as the previous code:
|
||||||
|
#
|
||||||
|
# ActiveRecord::RuntimeRegistry.connection_handler
|
||||||
|
#
|
||||||
|
# However, this is less performant because it makes a call to +method_missing+
|
||||||
|
# before it sends the method call to the +instance+.
|
||||||
|
class RuntimeRegistry
|
||||||
|
extend ActiveSupport::PerThreadRegistry
|
||||||
|
|
||||||
|
attr_accessor :connection_handler, :sql_runtime, :connection_id
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue