Sha256: 13ea525e26ad31f204e10642d2ad4f588a642986c8a8dae577f7047876998015

Contents?: true

Size: 1.75 KB

Versions: 32

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require "fiber"

module ActiveSupport
  module IsolatedExecutionState # :nodoc:
    @isolation_level = :thread

    Thread.attr_accessor :active_support_execution_state
    Fiber.attr_accessor :active_support_execution_state

    class << self
      attr_reader :isolation_level

      def isolation_level=(level)
        unless %i(thread fiber).include?(level)
          raise ArgumentError, "isolation_level must be `:thread` or `:fiber`, got: `#{level.inspect}`"
        end

        if level != isolation_level
          clear
          singleton_class.alias_method(:current, "current_#{level}")
          singleton_class.send(:private, :current)
          @isolation_level = level
        end
      end

      def unique_id
        self[:__id__] ||= Object.new
      end

      def [](key)
        current[key]
      end

      def []=(key, value)
        current[key] = value
      end

      def key?(key)
        current.key?(key)
      end

      def delete(key)
        current.delete(key)
      end

      def clear
        current.clear
      end

      def share_with(other)
        # Action Controller streaming spawns a new thread and copy thread locals.
        # We do the same here for backward compatibility, but this is very much a hack
        # and streaming should be rethought.
        context = @isolation_level == :thread ? Thread.current : Fiber.current
        context.active_support_execution_state = other.active_support_execution_state.dup
      end

      private
        def current_thread
          Thread.current.active_support_execution_state ||= {}
        end

        def current_fiber
          Fiber.current.active_support_execution_state ||= {}
        end

        alias_method :current, :current_thread
    end
  end
end

Version data entries

32 entries across 31 versions & 8 rubygems

Version Path
activesupport-7.0.8.6 lib/active_support/isolated_execution_state.rb
activesupport-7.0.8.5 lib/active_support/isolated_execution_state.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/isolated_execution_state.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/isolated_execution_state.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/isolated_execution_state.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/isolated_execution_state.rb
activesupport-7.0.8.4 lib/active_support/isolated_execution_state.rb
activesupport-7.0.8.1 lib/active_support/isolated_execution_state.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/isolated_execution_state.rb
activesupport-7.0.8 lib/active_support/isolated_execution_state.rb
activesupport-7.0.7.2 lib/active_support/isolated_execution_state.rb
activesupport-7.0.7.1 lib/active_support/isolated_execution_state.rb
activesupport-7.0.7 lib/active_support/isolated_execution_state.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/isolated_execution_state.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/isolated_execution_state.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/isolated_execution_state.rb
activesupport-7.0.6 lib/active_support/isolated_execution_state.rb
activesupport-7.0.5.1 lib/active_support/isolated_execution_state.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/isolated_execution_state.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/isolated_execution_state.rb