diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2013-04-09 21:27:48 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2013-04-09 21:27:48 -0700 |
commit | e0af93dd3a5eeee2e2a67b05f34afb66cc80c00b (patch) | |
tree | 5bcd2ef9f09cc031f54f9475ec28c9f0e6b4c4e1 /activesupport | |
parent | 9039c5038823754f79e04f1e83723e46229dbe05 (diff) | |
parent | e12901e4235d1ece2a17c5419f4420f1931cc6a4 (diff) | |
download | rails-e0af93dd3a5eeee2e2a67b05f34afb66cc80c00b.tar.gz rails-e0af93dd3a5eeee2e2a67b05f34afb66cc80c00b.tar.bz2 rails-e0af93dd3a5eeee2e2a67b05f34afb66cc80c00b.zip |
Merge pull request #10156 from wangjohn/grouping_thread_locals
Grouping thread locals in ActiveRecord
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/notifications.rb | 9 | ||||
-rw-r--r-- | activesupport/lib/active_support/per_thread_registry.rb | 41 |
2 files changed, 45 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index ab2bb5fafe..5c985601f4 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -1,5 +1,6 @@ require 'active_support/notifications/instrumenter' require 'active_support/notifications/fanout' +require 'active_support/per_thread_registry' module ActiveSupport # = Notifications @@ -190,12 +191,10 @@ module ActiveSupport # The instrumenters for multiple notifiers are held in a single instance of # this class. class InstrumentationRegistry # :nodoc: - class << self - delegate :instrumenter_for, to: :current + extend ActiveSupport::PerThreadRegistry - def current - Thread.current[:instrumentation_registry] ||= new - end + class << self + delegate :instrumenter_for, to: :instance end def initialize diff --git a/activesupport/lib/active_support/per_thread_registry.rb b/activesupport/lib/active_support/per_thread_registry.rb new file mode 100644 index 0000000000..b89ce2cbeb --- /dev/null +++ b/activesupport/lib/active_support/per_thread_registry.rb @@ -0,0 +1,41 @@ +module ActiveSupport + # This module creates a local registry class inside each thread. It provides + # basic methods which will store thread locals in a single class. This + # prevents the proliferation of too many thread locals and allows you to + # explicitly keep track of each of the variables you're using as thread + # locals in a class which includes this module. + # + # For example, instead of using a bunch of different thread locals to keep + # track of some variables like so: + # + # Thread.current[:active_record_connection_handler] = connection_handler + # Thread.current[:active_record_sql_runtime] = sql_runtime + # + # You could use the following class which implements the +PerThreadRegistry+ + # module: + # + # class NewRegistry + # extend ActiveSupport::PerThreadRegistry + # + # attr_accessor :connection_handler, :sql_runtime + # end + # + # NewRegistry.instance.connection_handler = connection_handler + # NewRegistry.instance.sql_runtime = sql_runtime + # + # The new way of keeping track of the thread locals will create a new local + # inside of +Thread.current+ with a key which is the name of the extended + # class. Now you can easily access per thread variables by just calling the + # variable name on the registry. + module PerThreadRegistry + def instance + Thread.current[self.name] ||= new + end + + protected + + def method_missing(*args, &block) + instance.send(*args, &block) + end + end +end |