Sha256: d78bc0d3799b6975780524d30971082e37f230811bc1d9db627e5ee20538979d
Contents?: true
Size: 1.82 KB
Versions: 18
Compression:
Stored size: 1.82 KB
Contents
# Copyright (c) 2023 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
18 entries across 18 versions & 1 rubygems