Sha256: 3ae08a4336d0ec8c806eadcfcb68218ca57f6e5c4afec60c5f6d87429e0e51a2

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

# typed: true

module Datadog
  module Core
    module Utils
      # Helper class to execute something only once such as not repeating warning logs, and instrumenting classes
      # only once.
      #
      # Thread-safe when used correctly (e.g. be careful of races when lazily initializing instances of this class).
      #
      # Note: In its current state, this class is not Ractor-safe.
      # In https://github.com/DataDog/dd-trace-rb/pull/1398#issuecomment-797378810 we have a discussion of alternatives,
      # including an alternative implementation that is Ractor-safe once spent.
      class OnlyOnce
        def initialize
          @mutex = Mutex.new
          @ran_once = false
        end

        def run
          @mutex.synchronize do
            return if @ran_once

            @ran_once = true

            yield
          end
        end

        def ran?
          @mutex.synchronize { @ran_once }
        end

        private

        def reset_ran_once_state_for_tests
          @mutex.synchronize { @ran_once = false }
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ddtrace-1.9.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.8.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.7.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.6.1 lib/datadog/core/utils/only_once.rb
ddtrace-1.6.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.5.2 lib/datadog/core/utils/only_once.rb
ddtrace-1.5.1 lib/datadog/core/utils/only_once.rb
ddtrace-1.5.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.4.2 lib/datadog/core/utils/only_once.rb
ddtrace-1.4.1 lib/datadog/core/utils/only_once.rb
ddtrace-1.4.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.3.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.2.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.1.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.0.0 lib/datadog/core/utils/only_once.rb
ddtrace-1.0.0.beta2 lib/datadog/core/utils/only_once.rb