Module: ActiveSupport::PerThreadRegistry

Included in:
Cache::Strategy::LocalCache::LocalCacheRegistry, Notifications::InstrumentationRegistry, SubscriberQueueRegistry
Defined in:
lib/active_support/per_thread_registry.rb

Overview

This module is used to encapsulate access to thread local variables.

Instead of polluting the thread locals namespace:

Thread.current[:connection_handler]

you define a class that extends this module:

module ActiveRecord
  class RuntimeRegistry
    extend ActiveSupport::PerThreadRegistry

    attr_accessor :connection_handler
  end
end

and invoke the declared instance accessors as class methods. So

ActiveRecord::RuntimeRegistry.connection_handler = connection_handler

sets a connection handler local to the current thread, and

ActiveRecord::RuntimeRegistry.connection_handler

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 Thread.current. The class methods proxy to said thread local instance.

If the class has an initializer, it must accept no arguments.

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

:nodoc:


46
47
48
49
50
51
52
53
# File 'lib/active_support/per_thread_registry.rb', line 46

def method_missing(name, *args, &block) # :nodoc:
  # Caches the method definition as a singleton method of the receiver.
  #
  # By letting #delegate handle it, we avoid an enclosure that'll capture args.
  singleton_class.delegate name, to: :instance

  send(name, *args, &block)
end

Class Method Details

.extended(object) ⇒ Object


37
38
39
# File 'lib/active_support/per_thread_registry.rb', line 37

def self.extended(object)
  object.instance_variable_set '@per_thread_registry_key', object.name.freeze
end

Instance Method Details

#instanceObject


41
42
43
# File 'lib/active_support/per_thread_registry.rb', line 41

def instance
  Thread.current[@per_thread_registry_key] ||= new
end