Sha256: 3766727cea895215e5cbd5f4a18282e92d4a91401ed628ced139e770b01bcbc9

Contents?: true

Size: 930 Bytes

Versions: 14

Compression:

Stored size: 930 Bytes

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Utils
    # ThreadTracker allows tracking of singleton objects across threads
    class ThreadTracker
      def initialize; end

      # Note about Ruby -- thread#[] is fiber-local,
      # #thread_variables is not.

      def get key, default = nil
        Thread.current[key] || default
      end

      def set key, value
        Thread.current[key] = value
      end

      def delete key
        Thread.current[key] = nil
      end

      def lifespan obj
        set(:current_context, obj)
        response = yield(obj)
        delete(:current_context)
        response
      end

      def current
        get(:current_context)
      end

      def update_current_context context
        set(:current_context, context)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
contrast-agent-4.4.1 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.4.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.3.2 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.3.1 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.3.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.2.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.1.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-4.0.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-3.16.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-3.15.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-3.14.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-3.13.2 lib/contrast/utils/thread_tracker.rb
contrast-agent-3.13.1 lib/contrast/utils/thread_tracker.rb
contrast-agent-3.13.0 lib/contrast/utils/thread_tracker.rb