Sha256: ae9ae2e8ea1e481c6da23711d258ce61ebf5919ef7100c7a5e6a834f8f034a11

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require_relative "base52"

module Kiev
  class SubrequestHelper
    class << self
      def headers(metadata: false)
        Config.instance.all_http_propagated_fields.map do |key, http_key|
          field = field_value(key, true)
          [metadata ? key : http_key, field.to_s] if field
        end.compact.to_h
      end

      def payload
        Config.instance.all_jobs_propagated_fields.map do |key|
          field = field_value(key, false)
          [key.to_s, field] if field
        end.compact.to_h
      end

      def root_path(synchronous:)
        encode(0, synchronous)
      end

      def subrequest_path(synchronous:)
        current_path + encode(counter, synchronous)
      end

      private

      def field_value(key, synchronous)
        if key == :tree_path
          subrequest_path(synchronous: synchronous)
        else
          request_store = Kiev::RequestStore.store
          request_store.dig(key) || request_store.dig(:payload, key)
        end
      end

      def encode(value, synchronous)
        # this scheme can encode up to 26 consequent requests (synchronous or asynchronous)
        Base52.encode(value * 2 + (synchronous ? 0 : 1))
      end

      def current_path
        RequestStore.store[:tree_path] || ""
      end

      def counter
        if RequestStore.store[:subrequest_count]
          # generally this is not atomic operation,
          # but because RequestStore.store is tied to current thread this is ok
          RequestStore.store[:subrequest_count] += 1
        else
          RequestStore.store[:subrequest_count] = 0
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
kiev-4.9.0 lib/kiev/subrequest_helper.rb
kiev-4.8.3 lib/kiev/subrequest_helper.rb
kiev-4.8.2 lib/kiev/subrequest_helper.rb
kiev-4.8.1 lib/kiev/subrequest_helper.rb
kiev-4.8.0 lib/kiev/subrequest_helper.rb
kiev-4.7.0 lib/kiev/subrequest_helper.rb
kiev-4.6.0 lib/kiev/subrequest_helper.rb
kiev-4.5.0 lib/kiev/subrequest_helper.rb
kiev-4.4.0 lib/kiev/subrequest_helper.rb
kiev-4.3.0 lib/kiev/subrequest_helper.rb
kiev-4.2.0 lib/kiev/subrequest_helper.rb
kiev-4.1.0 lib/kiev/subrequest_helper.rb
kiev-4.0.0 lib/kiev/subrequest_helper.rb
kiev-3.0.0 lib/kiev/subrequest_helper.rb
kiev-2.8.0 lib/kiev/subrequest_helper.rb
kiev-2.7.3 lib/kiev/subrequest_helper.rb