diff options
author | Xavier Noria <fxn@hashref.com> | 2013-04-13 17:08:00 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2013-04-13 17:09:13 +0200 |
commit | e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b (patch) | |
tree | 4292b3e79af5217e39ea84b56160a805e57f5aa7 /activesupport/lib | |
parent | 67bb49b69e1f10c2b35a53a645ed98a028375f1d (diff) | |
download | rails-e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b.tar.gz rails-e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b.tar.bz2 rails-e5ef3abdd2336c34cd853a1f845f79b8b19fbb1b.zip |
hides the per thread registry instance, and caches singleton methods
Existing code was delegating to the instance with delegate
macro calls, or invoking the instance method to reach
the object and call its instance methods.
But the point is to have a clean class-level interface where
the thread local instance is hidden in the implementation.
References #11c6973.
References #10198.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/notifications.rb | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/per_thread_registry.rb | 48 |
2 files changed, 29 insertions, 23 deletions
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index 5c985601f4..c45358bba9 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -193,10 +193,6 @@ module ActiveSupport class InstrumentationRegistry # :nodoc: extend ActiveSupport::PerThreadRegistry - class << self - delegate :instrumenter_for, to: :instance - end - def initialize @registry = {} end diff --git a/activesupport/lib/active_support/per_thread_registry.rb b/activesupport/lib/active_support/per_thread_registry.rb index 926df8b4cf..79ff61ca90 100644 --- a/activesupport/lib/active_support/per_thread_registry.rb +++ b/activesupport/lib/active_support/per_thread_registry.rb @@ -1,7 +1,11 @@ module ActiveSupport # This module is used to encapsulate access to thread local variables. # - # Given + # Instead of polluting the thread locals namespace: + # + # Thread.current[:connection_handler] + # + # you define a class that extends this module: # # module ActiveRecord # class RuntimeRegistry @@ -11,34 +15,40 @@ module ActiveSupport # end # end # - # <tt>ActiveRecord::RuntimeRegistry</tt> gets an +instance+ class method - # that returns an instance of the class unique to the current thread. Thus, - # instead of polluting +Thread.current+ - # - # Thread.current[:connection_handler] - # - # you write + # and invoke the declared instance accessors as class methods. So # - # ActiveRecord::RuntimeRegistry.instance.connection_handler + # ActiveRecord::RuntimeRegistry.connection_handler = connection_handler # - # A +method_missing+ handler that proxies to the thread local instance is - # installed in the extended class so the call above can be shortened to + # sets a connection handler local to the current thread, and # # ActiveRecord::RuntimeRegistry.connection_handler # - # The instance is stored as a thread local keyed by the name of the class, - # that is the string "ActiveRecord::RuntimeRegistry" in the example above. + # returns a connection handler local to the current thread. + # + # This feature is accomplished by instantiating the class and storing the + # instance as a thread local keyed by the class name. In the example above + # a key "ActiveRecord::RuntimeRegistry" is stored in <tt>Thread.current</tt>. + # The class methods proxy to said thread local instance. # # If the class has an initializer, it must accept no arguments. module PerThreadRegistry - def instance - Thread.current[self.name] ||= new - end - protected - def method_missing(*args, &block) - instance.public_send(*args, &block) + def method_missing(name, *args, &block) + # Caches the method definition as a singleton method of the receiver. + singleton_class.class_eval do + define_method(name) do |*a, &b| + per_thread_registry_instance.public_send(name, *a, &b) + end + end + + send(name, *args, &block) + end + + private + + def per_thread_registry_instance + Thread.current[name] ||= new end end end |