mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Performance optimizations to handle cases of instrumentors that are not listened to. Also, fix a possible concurrency issue.
This commit is contained in:
@@ -41,10 +41,30 @@ module ActiveSupport
|
||||
autoload :Event, 'active_support/notifications/instrumenter'
|
||||
autoload :Fanout, 'active_support/notifications/fanout'
|
||||
|
||||
@instrumenters = Hash.new { |h,k| h[k] = notifier.listening?(k) }
|
||||
|
||||
class << self
|
||||
attr_writer :notifier
|
||||
delegate :publish, :subscribe, :unsubscribe, :to => :notifier
|
||||
delegate :instrument, :to => :instrumenter
|
||||
delegate :publish, :unsubscribe, :to => :notifier
|
||||
|
||||
def instrument(name, payload = {})
|
||||
if @instrumenters[name]
|
||||
instrumenter.instrument(name, payload) { yield payload if block_given? }
|
||||
else
|
||||
yield payload if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
def subscribe(*args, &block)
|
||||
notifier.subscribe(*args, &block).tap do
|
||||
@instrumenters.clear
|
||||
end
|
||||
end
|
||||
|
||||
def unsubscribe(*args)
|
||||
notifier.unsubscribe(*args)
|
||||
@instrumenters.clear
|
||||
end
|
||||
|
||||
def notifier
|
||||
@notifier ||= Fanout.new
|
||||
|
||||
@@ -9,15 +9,16 @@ module ActiveSupport
|
||||
end
|
||||
|
||||
def subscribe(pattern = nil, block = Proc.new)
|
||||
@listeners_for.clear
|
||||
Subscriber.new(pattern, block).tap do |s|
|
||||
subscriber = Subscriber.new(pattern, block).tap do |s|
|
||||
@subscribers << s
|
||||
end
|
||||
@listeners_for.clear
|
||||
subscriber
|
||||
end
|
||||
|
||||
def unsubscribe(subscriber)
|
||||
@listeners_for.clear
|
||||
@subscribers.reject! {|s| s.matches?(subscriber)}
|
||||
@listeners_for.clear
|
||||
end
|
||||
|
||||
def publish(name, *args)
|
||||
@@ -28,6 +29,10 @@ module ActiveSupport
|
||||
@listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) }
|
||||
end
|
||||
|
||||
def listening?(name)
|
||||
listeners_for(name).any?
|
||||
end
|
||||
|
||||
# This is a sync queue, so there is not waiting.
|
||||
def wait
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@ module ActiveSupport
|
||||
def instrument(name, payload={})
|
||||
begin
|
||||
@started = Time.now
|
||||
yield(payload) if block_given?
|
||||
yield
|
||||
rescue Exception => e
|
||||
payload[:exception] = [e.class.name, e.message]
|
||||
raise e
|
||||
|
||||
Reference in New Issue
Block a user