Sha256: 2747f0ea711af77b4ce3362f823dd535b7208637928f03288d3eedaa41be7d6c
Contents?: true
Size: 1.44 KB
Versions: 3
Compression:
Stored size: 1.44 KB
Contents
require "forwardable" module L2meter # This class is a wrapper around Emitter that makes sure that we have a # completely separate clone of Emitter per thread running. It doesn't truly # make Emitter thread-safe, it makes sure that you don't access the same # instance of emitter from different threads. class ThreadSafe extend Forwardable EMITTER_METHODS = %i[ batch configuration count log measure push_context sample silence silence! unique unsilence! with_elapsed with_output ] private_constant :EMITTER_METHODS def initialize(emitter) @emitter = emitter.freeze end def_delegators :receiver, *EMITTER_METHODS def context(*args, &block) value = current_emitter.context(*args, &block) Emitter === value ? clone_with_emitter(value) : value end def disable! @disabled = true end protected attr_writer :emitter private attr_reader :emitter def clone_with_emitter(emitter) self.class.new(emitter).tap { |ts| ts.disable! if @disabled } end def receiver @disabled ? null_emitter : current_emitter end def current_emitter Thread.current[thread_key] ||= emitter.clone end def null_emitter @null_emitter ||= NullObject.new end def thread_key @thread_key ||= "_l2meter_emitter_#{emitter.object_id}".freeze end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
l2meter-0.7.0 | lib/l2meter/thread_safe.rb |
l2meter-0.6.3 | lib/l2meter/thread_safe.rb |
l2meter-0.6.2 | lib/l2meter/thread_safe.rb |