Skip to content

Commit

Permalink
Created a runtime registry for thread local variables in active record.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjohn committed Apr 10, 2013
1 parent e94f024 commit 95ac391
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/connection_handling.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def connection
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:
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/core.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ module Core
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
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/log_subscriber.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ class LogSubscriber < ActiveSupport::LogSubscriber
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
Expand Down
32 changes: 32 additions & 0 deletions activerecord/lib/active_record/runtime_registry.rb
Original file line number Original file line Diff line number Diff line change
@@ -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

0 comments on commit 95ac391

Please sign in to comment.