Sha256: 54bf4c4116527106d8dc64c7acce810bb2807fa5dde18018100524c2eaadbd8c

Contents?: true

Size: 1.82 KB

Versions: 16

Compression:

Stored size: 1.82 KB

Contents

# Copyright (c) 2022 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. It acts on Thread#[] as that call is
    # fiber-local where as Thread#thread_variables is not.
    class ThreadTracker
      # Get the given key to given value in Thread#[] or return default.
      #
      # @param key [Object] key used to reference the value
      # @param default [Object] value to return if not present in Thread#[]
      # @return [Object]
      def get key, default = nil
        Thread.current[key] || default
      end

      # Set the given key to given value in Thread#[].
      #
      # @param key [Object] key used to reference the value
      # @param value [Object] value to store
      def set key, value
        Thread.current[key] = value
      end

      # Remove the given key from the current Thread#[] by setting it to nil.
      #
      # @param key [Object] key of the value to delete
      def delete key
        Thread.current[key] = nil
      end

      # Wrap the block given with a RequestContext by setting it beforehand and deleting it after.
      #
      # @param context [Contrast::Agent::RequestContext]
      def lifespan context
        set(:current_context, context)
        response = yield(context)
        delete(:current_context)
        response
      end

      # Retrieve the Thread#[] RequestContext
      #
      # @return [Contrast::Agent::RequestContext]
      def current
        get(:current_context)
      end

      # Set the Thread#[] context to the one provided.
      #
      # @param context [Contrast::Agent::RequestContext]
      def update_current_context context
        set(:current_context, context)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
contrast-agent-6.11.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.10.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.9.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.8.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.7.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.6.5 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.6.4 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.6.3 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.6.2 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.6.1 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.6.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.5.1 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.5.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.4.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.3.0 lib/contrast/utils/thread_tracker.rb
contrast-agent-6.2.0 lib/contrast/utils/thread_tracker.rb